自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

四種利用C#播放聲音的方法

開發(fā) 后端
這里我們將介紹四種利用C#播放聲音的方法,希望通過本文能對大家了解C#多媒體機制有所幫助。

本文將介紹利用C#播放聲音的四種方法,希望那個通過本文,大家能在C#播放聲音方面有所突破。本文使用的是微軟的播放器,其他播放器大家可以嘗試。

第一種是利用DirectX

1.安裝了DirectX SDK(有9個DLL文件)。這里我們只用到MicroSoft.DirectX.dll 和 Microsoft.Directx.DirectSound.dll

2.引入DirectX 的DLL文件的名字空間:

  1. using Microsoft.DirectX;  
  2. using Microsoft.DirectX.DirectSound; 

3.建立設(shè)備

Device dv=new Device();

4.設(shè)置CooperativeLevel。因為windows是多任務(wù)的系統(tǒng),設(shè)備不是獨占的

SecondaryBuffer buf=new SecondaryBuffer(@"snd.wav",dv);

5.開辟緩沖區(qū)SecondaryBuffer buf=new SecondaryBuffer(@"snd.wav",dv);

6.接下來就可以播放啦。第一個參數(shù)表示優(yōu)先級別,0是最低的。第2個參數(shù)是播放方式,這里是循環(huán)播放。

buf.Play(0,BufferPlayFlags.Looping);

第二種是利用Microsoft speech object Library

  1. ///   
  2. /// 播放聲音文件  
  3. ///   
  4. /// 文件全名  
  5. public void PlaySound(string FileName)  
  6. {//要加載COM組件:Microsoft speech object Library  
  7. if (!System.IO.File.Exists(FileName))  
  8. {  
  9. return;  
  10. }  
  11. SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();  
  12. SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();  
  13. spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);  
  14. SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;  
  15. pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);  
  16. spFs.Close();  

第三種:引用SoundPlayer

  1. System.Media.SoundPlayer sndPlayer = new System.Media.SoundPlayer(Application.StartupPath+@"/pm3.wav");  
  2. sndPlayer.PlayLooping(); 

第4種:利用Windows Media Player

新建一個C#的Windows Form工程(Windows應(yīng)用程序),并且定義兩個菜單按鈕(menuItem1,menuItem2)。
選擇菜單中的“工具”中的“自定義工具箱(添加/移除工具箱項)”,在自定義工具箱的窗口中,點擊展開“COM 組件”項,選中“Window Media Player”選項。確定后在“工具箱”中便會出現(xiàn)“Windows Media Player”這一項,然后再將其拖至Form上,調(diào)整大小,系統(tǒng)在“引用”中自動加入了對此dll的引用,AxMediaPlayer就是我們使用的Namespace與class。

在屬性欄中設(shè)置好此控件的一些屬性,為了方便,這里我把AutoStart設(shè)置成為true(其實默認是true),只要FileName被設(shè)置(打開了文件),則文件將會自動播放。完整代碼如下:

  1. private void menuItem1_Click(object sender, System.EventArgs e)  
  2. {  
  3. OpenFileDialog ofDialog = new OpenFileDialog();  
  4. ofDialog.AddExtension = true;  
  5. ofDialog.CheckFileExists = true;  
  6. ofDialog.CheckPathExists = true;  
  7. //the next sentence must be in single line  
  8. ofDialog.Filter = "VCD文件(*.dat)|*.dat|Audio文件(*.avi)|*.avi  
  9. |WAV文件(*.wav)|*.wav|MP3文件(*.mp3)|*.mp3|所有文件 (*.*)|*.*";  
  10. ofDialog.DefaultExt = "*.mp3";  
  11. if(ofDialog.ShowDialog() == DialogResult.OK)  
  12. {  
  13. // 2003一下版本 方法 this.axMediaPlayer1.FileName = ofDialog.FileName;  
  14. this.axMediaPlayer1.URL= ofDialog.FileName;//2005用法  
  15. }  

這里使用的是微軟的播放器,大家也可以試試Winamp的控件,如果你只需要播放聲音而不需要顯示,你只要把AxMediaPlayer的Visible屬性設(shè)置為false就可以了。

【編輯推薦】

  1. C#參差數(shù)組初始化概述
  2. C#數(shù)組初始化全面分析
  3. C#一維數(shù)組和多維數(shù)組淺談
  4. C#參差數(shù)組初始化概述
  5. C#動態(tài)數(shù)組實例介紹
責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2013-06-14 17:28:11

Windows PhoWP開發(fā)播放聲音

2009-08-05 14:09:04

C#日期轉(zhuǎn)換

2009-09-17 16:55:58

C#組件設(shè)計

2009-08-26 15:04:35

C#轉(zhuǎn)換

2009-08-20 09:52:31

C#參數(shù)類型

2012-12-27 14:29:38

Android開發(fā)流媒體

2024-10-24 08:04:00

2009-09-08 17:20:01

C#排序算法

2011-07-22 15:59:15

iPhone 聲音 文件

2024-06-24 01:00:00

2023-08-30 23:41:16

AI框架項目

2010-10-19 17:40:30

SqlServer主鍵

2023-02-10 11:13:42

網(wǎng)絡(luò)功耗無線網(wǎng)絡(luò)設(shè)備

2010-01-11 17:30:40

VB.NET播放聲音

2009-12-09 11:03:45

安裝Linux

2024-05-29 13:18:12

線程Thread?方式

2009-08-27 15:00:55

C#線程控制

2014-03-17 09:22:43

Linux命令

2022-09-02 14:29:01

JavaScrip數(shù)組屬性

2019-08-13 09:00:24

REST API身份認證密鑰
點贊
收藏

51CTO技術(shù)棧公眾號