WPF中播放声音媒体文件
2013-06-02 13:54
225 查看
这段时间我们小组要给部门的Annual Meeting準备一个WPF的抽奖程序,为了增加程序的有趣性,我们在程序中需要播放背景音乐等。由於对之前从未使用过WPF,所以对其中的声音等媒体文件播放不是很清楚,对一些简单的问题也花了相对较长的时间去解决,现在将其总结在下麵,以供大家参考。
1,使用SoundPlayer类
SoundPlayer类位於System.Media命名空间下,它只能播放.wav格式的声音文件。其使用方法简单如下:
[C#]
view plaincopyprint?
using(SoundPlayer player = new SoundPlayer())
{
string location=System.Environment.CurrentDirectory+"//Sounds//explosion.wav";
player.SoundLocation=location;
player.Play();
}
上面的代码段即是播放主程序文件夹内下的Sounds文件夹下的explosion.wav声音文件。如果你的声音文件比较小,可以直接作為资源嵌入到应用程序中,这裡的Location属性使用相对路径即可。
除了上面提到的文件格式限制外,这个类还有个缺陷,就是你只能同时播放一个声音文件,即便你实例化几个不同的类,在我的程序中最初考虑一个背景音乐文件一直循环播放,可是当我把光标放置於另外一个我自己定制的UserControl上,会播放一个声音,而之前的背景音乐就会消失,於是我不得不用其它的方法。
2,使用MediaPlayer类
MediaPlayer类位於System.Windows.Media命名空间下,关於此类详情参考http://msdn.microsoft.com/en-us/library/system.windows.media.mediaplayer(v=VS.100).aspx。
如下示例:
[C#]
view plaincopyprint?
MediaPlayer player = new MediaPlayer(); player.Open(new Uri(@"sampleMedia/xbox.wmv", UriKind.Relative)); VideoDrawing aVideoDrawing = new VideoDrawing(); aVideoDrawing.Rect = new Rect(0, 0, 100, 100); aVideoDrawing.Player = player; player.Play();
3,在XAML中使用MediaPlayer元素
MediaPlayer元素可以方便的在XAML中直接使用MediaPlayer,如下示例:
[XML]
view plaincopyprint?
<MediaElement Name="MyMediaElement">
<MediaElement.Triggers>
<EventTrigger RoutedEvent="MediaElement.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<MediaTimeline x:Name="mediatiemline" Source=<span style="color: rgb(163, 21, 21);">"pack://siteoforigin:,,,/Sounds/test.mp3"</span> Storyboard.TargetName="MyMediaElement"
RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</MediaElement.Triggers>
</MediaElement>
以上示例是在加载时MediaElement循环播放音乐文件。
请注意Source="pack://siteoforigin:,,,/Sounds/test.mp3" 这裡的路径表示方法,亦可以直接在Code-behind中直接设置:
我们可以发现,通过MediaPlayer可以解决文件格式限制问题,还可以解决不能同时播放几个声音的问题。
[C#]
view plaincopyprint?
this.mediatiemline.Source=new Uri(<span style="background-color: rgb(255, 255, 0);">Environment.CurrentDirectory </span>+ <a><span style="color: rgb(76, 125, 8);">//Sounds//test.mp3</span></a>);
[C#]
view plaincopyprint?
ps:<span style="background-color: rgb(255, 255, 0);"><span style="color: rgb(255, 0, 0);">Environment.CurrentDirectory:<span style="background-color: rgb(255, 255, 255);">获取或设置当前工作目录的完全限定路径。这个就解决了相对路径的问题。</span></span></span>
4,使用MediaPlayer(包括MediaElement)的注意事项
我们在测试的时候发现,由於电脑上Windows Media Player的版本比较低,导致在播放一些音乐文件的时候出问题,没有声音,我们花费了很长时间解决代码的问题,最终发现问题是在Windows Media Player版本上,所以如果大家遇到类似问题请记得更新你的Windows Media Player為较高版本,XP系统可以在这裡下载11.0版本:
http://www.microsoft.com/downloads/details.aspx?familyid=1D224714-E238-4E45-8668-5166114010CA&displaylang=en
为了您的安全,请只打开来源可靠的网址
打开网站 取消
来自: http://hi.baidu.com/1987raymond/blog/item/782177fb3c0eb56a024f5644.html
1,使用SoundPlayer类
SoundPlayer类位於System.Media命名空间下,它只能播放.wav格式的声音文件。其使用方法简单如下:
[C#]
view plaincopyprint?
using(SoundPlayer player = new SoundPlayer())
{
string location=System.Environment.CurrentDirectory+"//Sounds//explosion.wav";
player.SoundLocation=location;
player.Play();
}
using(SoundPlayer player = new SoundPlayer()) { string location=System.Environment.CurrentDirectory+"//Sounds//explosion.wav"; player.SoundLocation=location; player.Play(); }
上面的代码段即是播放主程序文件夹内下的Sounds文件夹下的explosion.wav声音文件。如果你的声音文件比较小,可以直接作為资源嵌入到应用程序中,这裡的Location属性使用相对路径即可。
除了上面提到的文件格式限制外,这个类还有个缺陷,就是你只能同时播放一个声音文件,即便你实例化几个不同的类,在我的程序中最初考虑一个背景音乐文件一直循环播放,可是当我把光标放置於另外一个我自己定制的UserControl上,会播放一个声音,而之前的背景音乐就会消失,於是我不得不用其它的方法。
2,使用MediaPlayer类
MediaPlayer类位於System.Windows.Media命名空间下,关於此类详情参考http://msdn.microsoft.com/en-us/library/system.windows.media.mediaplayer(v=VS.100).aspx。
如下示例:
[C#]
view plaincopyprint?
MediaPlayer player = new MediaPlayer(); player.Open(new Uri(@"sampleMedia/xbox.wmv", UriKind.Relative)); VideoDrawing aVideoDrawing = new VideoDrawing(); aVideoDrawing.Rect = new Rect(0, 0, 100, 100); aVideoDrawing.Player = player; player.Play();
MediaPlayer player = new MediaPlayer(); player.Open(new Uri(@"sampleMedia/xbox.wmv", UriKind.Relative)); VideoDrawing aVideoDrawing = new VideoDrawing(); aVideoDrawing.Rect = new Rect(0, 0, 100, 100); aVideoDrawing.Player = player; player.Play();
3,在XAML中使用MediaPlayer元素
MediaPlayer元素可以方便的在XAML中直接使用MediaPlayer,如下示例:
[XML]
view plaincopyprint?
<MediaElement Name="MyMediaElement">
<MediaElement.Triggers>
<EventTrigger RoutedEvent="MediaElement.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<MediaTimeline x:Name="mediatiemline" Source=<span style="color: rgb(163, 21, 21);">"pack://siteoforigin:,,,/Sounds/test.mp3"</span> Storyboard.TargetName="MyMediaElement"
RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</MediaElement.Triggers>
</MediaElement>
<MediaElement Name="MyMediaElement"> <MediaElement.Triggers> <EventTrigger RoutedEvent="MediaElement.Loaded"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <MediaTimeline x:Name="mediatiemline" Source="pack://siteoforigin:,,,/Sounds/test.mp3" Storyboard.TargetName="MyMediaElement" RepeatBehavior="Forever" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </MediaElement.Triggers> </MediaElement>
以上示例是在加载时MediaElement循环播放音乐文件。
请注意Source="pack://siteoforigin:,,,/Sounds/test.mp3" 这裡的路径表示方法,亦可以直接在Code-behind中直接设置:
我们可以发现,通过MediaPlayer可以解决文件格式限制问题,还可以解决不能同时播放几个声音的问题。
[C#]
view plaincopyprint?
this.mediatiemline.Source=new Uri(<span style="background-color: rgb(255, 255, 0);">Environment.CurrentDirectory </span>+ <a><span style="color: rgb(76, 125, 8);">//Sounds//test.mp3</span></a>);
this.mediatiemline.Source=new Uri(Environment.CurrentDirectory + //Sounds//test.mp3);
[C#]
view plaincopyprint?
ps:<span style="background-color: rgb(255, 255, 0);"><span style="color: rgb(255, 0, 0);">Environment.CurrentDirectory:<span style="background-color: rgb(255, 255, 255);">获取或设置当前工作目录的完全限定路径。这个就解决了相对路径的问题。</span></span></span>
ps:Environment.CurrentDirectory:获取或设置当前工作目录的完全限定路径。这个就解决了相对路径的问题。
4,使用MediaPlayer(包括MediaElement)的注意事项
我们在测试的时候发现,由於电脑上Windows Media Player的版本比较低,导致在播放一些音乐文件的时候出问题,没有声音,我们花费了很长时间解决代码的问题,最终发现问题是在Windows Media Player版本上,所以如果大家遇到类似问题请记得更新你的Windows Media Player為较高版本,XP系统可以在这裡下载11.0版本:
http://www.microsoft.com/downloads/details.aspx?familyid=1D224714-E238-4E45-8668-5166114010CA&displaylang=en
为了您的安全,请只打开来源可靠的网址
打开网站 取消
来自: http://hi.baidu.com/1987raymond/blog/item/782177fb3c0eb56a024f5644.html
相关文章推荐
- WPF中播放声音媒体文件
- WPF中播放声音媒体文件
- WPF中播放声音媒体文件
- 完美解决Java程序在 MOTO E680i 中声音文件播放
- IOS-使用AVAudioPlayer播放音乐文件无声音
- 用C#播放声音文件
- c#播放声音文件
- 在XNA 3.0 项目添加声音——播放MP3或WMA声音文件
- 幻世(OurDream)2D图形引擎使用教程11——播放媒体文件(1)
- Android录制声音文件(音频),并播放
- 基于WPF的音频文件循环顺序播放
- windows播放wav声音文件--java播放wav完整demo
- C#编写Windows桌面应用程序播放MP3声音文件
- Ubuntu/Linux下安装JMF播放媒体文件
- 媒体文件播放时间的转换,即毫秒转换为hh:mm:ss 。
- 解析WPF实现音频文件循环顺序播放的解决方法
- BlackBerry 10:Qt中使用OpenAL播放wav声音文件
- QQ影音播放MP4、MKV等视频文件有声音没图像,显示绿屏
- WMP不能通过HTTP播放中文名媒体文件的问题
- java播放声音文件