C#播放背景音乐的方法小结
本文实例总结了C#播放背景音乐的方法。分享给大家供大家参考。具体分析如下: 最经在写winform程序,其中有用到播放背景音乐 特此收集了一些网上的教程: 1、调用非托管的dll using System.Runtime.InteropServices; //DllImport命名空间的引用 class test //提示音 { [DllImport("winmm.dll")] public static extern bool PlaySound(String Filename,int Mod,int Flags); public void Main() { PlaySound(@"d:/qm.wav",1); //把1替换成9,可连续播放 } } 2、播放系统自带声音 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(); 3、使用System.Media.SoundPlayer播放wav System.Media.SoundPlayer sp = new SoundPlayer(); sp.SoundLocation = @"D:10sec.wav"; sp.PlayLooping(); 4、使用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); mciSendString(@"open ""E:Music青花瓷.mp3"" alias temp_alias",0); mciSendString("play temp_alias repeat",0); } 关于mciSendString的详细参数说明,请参见MSDN,或是 http://blog.csdn.net/psongchao/archive/2007/01/19/1487788.aspx 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:MusicSomeOne.avi"; axWindowsMediaPlayer1.Ctlcontrols.play(); } 希望本文所述对大家的C#程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 极速开发框架dhroid之sqlite优化
- ruby-on-rails – 在点击标签名称时Rails best_in_place ge
- ReactNative源码篇:代码调用
- c# – 如何从HttpResponseMessage读取MultipartContent?
- swift – 在tableview单元格中嵌入视频
- ruby-on-rails – attr_accessor强类型的Ruby on Rails
- 编程时候非常有用的正则表达式记录
- FL2440的U-boot-2010.09移植(六)NAND Flash启动支持
- 那些年,我使用过的轮子
- 如果使用正则表达式连续出现1个以上的字符串,如何从字符串中