最新国产好看的视频,伊人天堂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)論

新干县| 遂溪县| 女性| 随州市| 泸溪县| 镇赉县| 洛川县| 探索| 扎囊县| 清河县| 五原县| 茂名市| 山东| 武乡县| 潞城市| 阜康市| 军事| 敦化市| 体育| 和硕县| 洛宁县| 绥棱县| 盐亭县| 祁阳县| 辽源市| 澄江县| 和林格尔县| 扶余县| 扶沟县| 通渭县| 敦化市| 开封市| 尼玛县| 舒兰市| 临颍县| 富平县| 曲麻莱县| 肇源县| 周宁县| 拉孜县| 黄龙县|