C#使用DirectX.DirectSound播放語音
最近在做項目時,需要進行音頻文件的即時播放,并且要求同時播放多條語音,之前C#程序中語音播放一直使用System.Media類庫的SoundPlayer類進行播放,但是這個播放類有個弊端,就是在播放時不能搶占式播放語音,經(jīng)過查找資料DirectX.DirectSound可同時播放多條語音。
DirectX.DirectSound的特點
1、可同時播放多條語音
2、可分左右聲道進行播放
3、可隨時釋放正在播放的語音
此組件處理流程:
1、創(chuàng)建播放線程
public void StartDirectXSoundThread(Control _con)
?{
? ? IsStart = true;
? ? if (control == null) control = _con;
? ? Task task = new Task(() =>
? ? ? {
? ? ? ? while (true)
? ? ? ? ? ? {
? ? ? ? ? ? ?? try
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (!IsStart) break;
? ? ? ? ? ? ? ? if (!IsPlaying())
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (soundlist.Count > 0)
? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (!IsPlayVoice)
? ? ? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ?IsPlayVoice = true;
? ? ? ? ? ? ? ? ? ? ? ? ? ?control.Invoke((MethodInvoker)delegate
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? SoundPlay(soundlist[0]);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? soundlist.RemoveAt(0);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ?catch (Exception ex)
? ? ? ? {
? ? ? ? ? LogHelper.Debug(ex);
? ? ? ? }
? ? ? ? ? finally
? ? ? ? {
? ? ? ? ? }
? ? ? ? ? Thread.Sleep(100);
? ? ? ? }
? ? });
task.Start();
}2、釋放播放線程
public void StopDirectXSoundThread()
{
? IsStart = false;
?}3、判斷是否播放中,通過PlayPosition!=0和播放緩沖是否null的條件判斷是否播放
private bool IsPlaying()
? {
? ? bool Ret = false;
? ? ? ?try
? ? ? ? ?{
? ? ? ? ? if (IsCreate)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (secBuffer != null)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (secBuffer.PlayPosition != 0)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? Ret = true;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ?catch (Exception ex)
? ? ? {
? ? ? ? ? ? LogHelper.Debug(ex);
? ? ? ? }
? ? ? return Ret;
}4、播放音頻
public void SoundPlay(string _wavpath)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (_wavpath.IndexOf("\\") < 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? _wavpath = SoundPath + _wavpath;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (_wavpath.IndexOf(".wav") < 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? _wavpath += ".wav";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (!File.Exists(_wavpath))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? LogHelper.Info("無" + _wavpath + "文件!");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? secDev.SetCooperativeLevel(control, CooperativeLevel.Normal);
? ? ? ? ? ? ? ? ? ? BufferDescription buffdes = new BufferDescription()
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? GlobalFocus = true
? ? ? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? ? ? secBuffer = new SecondaryBuffer(_wavpath, buffdes, secDev);
? ? ? ? ? ? ? ? ? ? secBuffer.Play(0, BufferPlayFlags.Default);//設(shè)置緩沖區(qū)為默認播放?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? IsCreate = true;
? ? ? ? ? ? ? ? IsPlayVoice = false;
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? LogHelper.Debug(ex);
? ? }
}左右聲道通過secBuffer.Pan屬性進行控制,值含義見下圖:
a、Center中心通道,左右通道同時播放,默認值0
b、Right右通道,值10000
c、Right左通道,值-10000

5、清除播放中音頻 ,播放中的音頻可以通過Dispose()方法進行釋放
public void ClearPlay()
? {
? if (secBuffer != null)
? ?{
? ? soundlist.Clear();
? ? secBuffer.Dispose();
? ? IsCreate = false;
? ? ?}
}6、定義
/// <summary> /// 播放設(shè)備 /// </summary> private Device secDev = new Device(); ? /// <summary> /// 播放緩沖區(qū) /// </summary> private SecondaryBuffer secBuffer = null; ? /// <summary> /// 可視化組件 /// </summary> private Control control; ? /// <summary> /// 是否被創(chuàng)建 /// </summary> private bool IsCreate = false;
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
中秋國慶節(jié)眼看到跟前了,很多商城都借此機會搞促銷活動,什么滿200減80送優(yōu)惠券等活動,基于后臺程序是怎么實現(xiàn)的呢?下面腳本之家小編帶領(lǐng)大家一起學(xué)習吧2015-09-09
C#中構(gòu)造函數(shù)和析構(gòu)函數(shù)用法實例詳解
這篇文章主要介紹了C#中構(gòu)造函數(shù)和析構(gòu)函數(shù)用法,結(jié)合實例形式詳細分析了C#中構(gòu)造函數(shù)與析構(gòu)函數(shù)的原理、定義、使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-06-06
C# 利用IRawPixels接口遍歷柵格數(shù)據(jù)
本文主要介紹了利用IRawPixels接口遍歷柵格數(shù)據(jù)。具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02
C# 中的 IReadOnlyDictionary 和 IReadOnlyLis
C# 中的IReadOnlyDictionary和IReadOnlyList是接口,用于表示只讀的字典和只讀的列表,這些接口提供了對集合的只讀訪問權(quán)限,即不允許對集合進行修改操作,這篇文章主要介紹了C# 中的 IReadOnlyDictionary 和 IReadOnlyList實例詳解,需要的朋友可以參考下2024-03-03

