C#播放背景音乐常用的四种方式
来源:互联网 发布:在家兼职工作招聘淘宝 编辑:程序博客网 时间:2024/05/22 05:20
转自:http://www.cnblogs.com/Microblue/archive/2010/09/21/2406704.html
C#播放背景音乐通常有四种方式:
1.播放系统事件声音
2.使用System.Media.SoundPlayer播放wav
3.使用MCI Command String多媒体设备程序接口播放mp3,avi等
4.使用axWindowsMediaPlayer的COM组件来播放
1.播放系统事件声音
System.Media.SystemSounds.Asterisk.Play();
System.Media.SystemSounds.Beep.Play();
System.Media.SystemSounds.Exclamation.Play();
System.Media.SystemSounds.Hand.Play();
System.Media.SystemSounds.Question.Play();
2.使用System.Media.SoundPlayer播放wav
System.Media.SoundPlayer sp = new SoundPlayer();sp.SoundLocation = @"D:/10sec.wav";
sp.PlayLooping();
3.使用MCI Command String多媒体设备程序接口播放mp3,avi等
using System.Runtime.InteropServices;
public static uint SND_ASYNC = 0x0001;
public static uint SND_FILENAME = 0x00020000;
[DllImport("winmm.dll")]
public static extern uint mciSendString(string lpstrCommand,
string lpstrReturnString, uint uReturnLength, uint hWndCallback);
public void Play()
{
mciSendString(@"close temp_alias", null, 0, 0);
mciSendString(@"open ""E:/Music/青花瓷.mp3"" alias temp_alias", null, 0, 0);
mciSendString("play temp_alias repeat", null, 0, 0);
}
关于mciSendString的详细参数说明,请参见MSDN,或是 http://blog.csdn.net/psongchao/archive/2007/01/19/1487788.aspx 或http://blog.csdn.net/threadroc/article/details/50991435
4.使用axWindowsMediaPlayer的COM组件来播放
a.加载COM组件:ToolBox->Choose Items->COM Components->Windows Media Player如下图:
b.把Windows Media Player控件拖放到Winform窗体中,把axWindowsMediaPlayer1中URL属性设置为MP3或是AVI的文件路径,F5运行。
如何使用Windows Media Player循环播放列表中的媒体文件?
假设我们有一个播放列表,下面的代码可以实现自动循环播放
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e){
if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{
Thread thread = new Thread(new ThreadStart(PlayThread));
thread.Start();
}
}
private void PlayThread()
{
axWindowsMediaPlayer1.URL = @"E:/Music/SomeOne.avi";
axWindowsMediaPlayer1.Ctlcontrols.play();
}
MCI Command String和Windows Media Player都有非常丰富的功能接口,这里不能一一介绍,可以参考MSDN中的具体描述.
- C#播放背景音乐常用的四种方式
- C#播放背景音乐常用的四种方式(转载)
- C#播放背景音乐常用的四种方式
- C#播放背景音乐常用的四种方式
- C#播放背景音乐常用的四种方式
- C#播放背景音乐常用的四种方式
- \t\tC#播放背景音乐常用的四种方式(转载)
- C#播放背景音乐的五种方法
- C#中播放背景音乐几种的方法
- C#中播放背景音乐几种的方法
- 使用C#播放MP3/WAV文件的四种方式
- C#播放背景音乐
- 如何使用C#播放背景音乐
- 如何使用C#播放背景音乐
- android 背景音乐的播放
- C#播放声音的四种方法
- C#播放声音的四种方法
- C#播放声音的四种方法
- smarty的安装
- 内部排序的总结
- 【09类和模块】——1类和原型
- wampserver You don't have permission to access / on this server. 解决 方法
- linux下PHP7环境搭建
- C#播放背景音乐常用的四种方式
- TTS怎么使文字发音
- POJ 1062 昂贵的聘礼(限制的最短路)
- 数据结构之位图
- 文章标题
- 树莓派瞎玩~1~系统的安装.md
- MCISendString详细参数
- 指针和引用的区别
- jQuery异步框架探究3:jQuery.when方法