码迷,mamicode.com
首页 > Windows程序 > 详细

Winform_播放声音文件

时间:2015-10-08 18:00:58      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

1、调用非托管的dll

 

[csharp] view plaincopy
 
  1.        using System.Runtime.InteropServices;  //DllImport命名空间的引用     
  2. class test  //提示音  
  3. {  
  4.     [DllImport("winmm.dll")]  
  5.     public static extern bool PlaySound(String Filename,int Mod,int Flags);  
  6.   
  7.     public void Main()  
  8.     {  
  9.         PlaySound(@"d:/qm.wav",0,1);   //把1替换成9,可连续播放  
  10.     }  
  11.        }  

 

2、播放系统自带声音

 

[csharp] view plaincopy
 
  1.   System.Media.SystemSounds.Asterisk.Play();   
  2. System.Media.SystemSounds.Beep.Play();   
  3. System.Media.SystemSounds.Exclamation.Play();   
  4. System.Media.SystemSounds.Hand.Play();   
  5. System.Media.SystemSounds.Question.Play();  

3、使用System.Media.SoundPlayer播放wav

 

[csharp] view plaincopy
 
  1. System.Media.SoundPlayer sp = new SoundPlayer();   
  2.   sp.SoundLocation = @"D:\10sec.wav";   
  3.   sp.PlayLooping();  

4、使用MCI Command String多媒体设备程序接口播放mp3,avi等

 

 

[csharp] view plaincopy
 
  1. using System.Runtime.InteropServices;   
  2.   public static uint SND_ASYNC = 0x0001;   
  3.   public static uint SND_FILENAME = 0x00020000;   
  4.   [DllImport("winmm.dll")]   
  5.   public static extern uint mciSendString(string lpstrCommand,   
  6.   string lpstrReturnString, uint uReturnLength, uint hWndCallback);   
  7.   public void Play()   
  8.   {   
  9.   mciSendString(@"close temp_alias", null, 0, 0);   
  10.   mciSendString(@"open ""E:\Music\青花瓷.mp3"" alias temp_alias", null, 0, 0);   
  11.   mciSendString("play temp_alias repeat", null, 0, 0);   
  12.   }  

关于mciSendString的详细参数说明,请参见MSDN,或是 http://blog.csdn.net/psongchao/archive/2007/01/19/1487788.aspx

5、使用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循环播放列表中的媒体文件?

  假设我们有一个播放列表,下面的代码可以实现自动循环播放

[csharp] view plaincopy
 
    1. private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)   
    2.   {   
    3.   if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)   
    4.   {   
    5.   Thread thread = new Thread(new ThreadStart(PlayThread));   
    6.   thread.Start();   
    7.   }   
    8.   }   
    9.   private void PlayThread()   
    10.   {   
    11.   axWindowsMediaPlayer1.URL = @"E:\Music\SomeOne.avi";   
    12.   axWindowsMediaPlayer1.Ctlcontrols.play();   
    13.   }  

Winform_播放声音文件

标签:

原文地址:http://www.cnblogs.com/ingstyle/p/4861481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!