最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c# 播放聲音的四種方法

 更新時(shí)間:2020年11月24日 17:16:00   作者:搜源網(wǎng)  
這篇文章主要介紹了c# 播放聲音的四種方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下

第一種是利用DirectX

1.安裝了DirectX SDK(有9個(gè)DLL文件)。這里我們只用到MicroSoft.DirectX.dll 和 Microsoft.Directx.DirectSound.dll
2.引入DirectX 的DLL文件的名字空間:

 using Microsoft.DirectX;
 using Microsoft.DirectX.DirectSound;

3.建立設(shè)備

Device dv=new Device();

4.設(shè)置CooperativeLevel。因?yàn)閣indows是多任務(wù)的系統(tǒng),設(shè)備不是獨(dú)占的

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

5.開辟緩沖區(qū)

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

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

 buf.Play(0,BufferPlayFlags.Looping);

第二種是利用Microsoft speech object Library

/// <summary

  /// 播放聲音文件

  /// </summary>

  /// <param name="FileName">文件全名</param>

  public void PlaySound(string FileName)

  {//要加載COM組件:Microsoft speech object Library

   if (!System.IO.File.Exists(FileName))

   {

    return;

   }

   SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();

 

   SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();

 

   spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);

   SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;

   pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);

   spFs.Close();

  }

第三種:引用SoundPlayer

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

sndPlayer.PlayLooping();

第4種:利用Windows Media Player

新建一個(gè)C#的Windows Form工程(Windows應(yīng)用程序),并且定義兩個(gè)菜單按鈕(menuItem1,menuItem2)。
  選擇菜單中的“工具”中的“自定義工具箱(添加/移除工具箱項(xiàng))”,在自定義工具箱的窗口中,點(diǎn)擊展開“COM 組件”項(xiàng),選中“Window Media Player”選項(xiàng)。確定后在“工具箱”中便會(huì)出現(xiàn)“Windows Media Player”這一項(xiàng),然后再將其拖至Form上,調(diào)整大小,系統(tǒng)在“引用”中自動(dòng)加入了對(duì)此dll的引用,AxMediaPlayer就是我們使用的Namespace與class。
  在屬性欄中設(shè)置好此控件的一些屬性,為了方便,這里我把AutoStart設(shè)置成為true(其實(shí)默認(rèn)是true),只要FileName被設(shè)置(打開了文件),則文件將會(huì)自動(dòng)播放。完整代碼如下:

private void menuItem1_Click(object sender, System.EventArgs e)
{
OpenFileDialog ofDialog = new OpenFileDialog();
ofDialog.AddExtension = true;
ofDialog.CheckFileExists = true;
ofDialog.CheckPathExists = true;



//the next sentence must be in single line
ofDialog.Filter = "VCD文件(*.dat)|*.dat|Audio文件(*.avi)|*.avi
  |WAV文件(*.wav)|*.wav|MP3文件(*.mp3)|*.mp3|所有文件 (*.*)|*.*";



ofDialog.DefaultExt = "*.mp3";
if(ofDialog.ShowDialog() == DialogResult.OK)
{
// 2003一下版本 方法 this.axMediaPlayer1.FileName = ofDialog.FileName;
this.axMediaPlayer1.URL= ofDialog.FileName;//2005用法

}
}

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

以上就是c# 播放聲音的四種方法的詳細(xì)內(nèi)容,更多關(guān)于c# 播放聲音的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

平谷区| 周口市| 台中市| 徐闻县| 白朗县| 定陶县| 塔城市| 尚义县| 龙里县| 西宁市| 天镇县| 南充市| 宝鸡市| 昌宁县| 平江县| 五大连池市| 谷城县| 老河口市| 岗巴县| 若尔盖县| 武汉市| 太仓市| 平顺县| 栾川县| 徐汇区| 长顺县| 和硕县| 醴陵市| 丰都县| 沙雅县| 太保市| 遂平县| 田林县| 平定县| 郑州市| 来宾市| 南郑县| 洛川县| 沽源县| 诏安县| 井冈山市|