網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
最近在做項(xiàng)目時(shí),需要進(jìn)行音頻文件的即時(shí)播放,并且要求同時(shí)播放多條語(yǔ)音,之前C#程序中語(yǔ)音播放一直使用System.Media類(lèi)庫(kù)的SoundPlayer類(lèi)進(jìn)行播放,但是這個(gè)播放類(lèi)有個(gè)弊端,就是在播放時(shí)不能搶占式播放語(yǔ)音,經(jīng)過(guò)查找資料DirectX.DirectSound可同時(shí)播放多條語(yǔ)音。
DirectX.DirectSound的特點(diǎn)
1、可同時(shí)播放多條語(yǔ)音
2、可分左右聲道進(jìn)行播放
3、可隨時(shí)釋放正在播放的語(yǔ)音
此組件處理流程:
1、創(chuàng)建播放線(xiàn)程
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、釋放播放線(xiàn)程
public void StopDirectXSoundThread() { ? IsStart = false; ?}
3、判斷是否播放中,通過(guò)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("無(wú)" + _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ū)為默認(rèn)播放? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? IsCreate = true; ? ? ? ? ? ? ? ? IsPlayVoice = false; ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? LogHelper.Debug(ex); ? ? } }
左右聲道通過(guò)secBuffer.Pan屬性進(jìn)行控制,值含義見(jiàn)下圖:
a、Center中心通道,左右通道同時(shí)播放,默認(rèn)值0
b、Right右通道,值10000
c、Right左通道,值-10000
5、清除播放中音頻 ,播放中的音頻可以通過(guò)Dispose()方法進(jìn)行釋放
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;
原文鏈接:https://blog.csdn.net/evint888/article/details/122387024
相關(guān)推薦
- 2022-07-28 pytest使用parametrize將參數(shù)化變量傳遞到fixture_python
- 2021-11-12 C++?路徑中./、../、/代表的含義_C 語(yǔ)言
- 2021-12-15 CentOS下更新SQLite版本_SQLite
- 2023-03-29 nginx部署前端項(xiàng)目的超級(jí)詳細(xì)步驟記錄_nginx
- 2022-08-20 python?memory_profiler庫(kù)生成器和迭代器內(nèi)存占用的時(shí)間分析_python
- 2022-04-09 Nginx 提示10013: An attempt was made to access a soc
- 2022-09-21 Python安裝和配置uWSGI的詳細(xì)過(guò)程_python
- 2022-02-10 antd table 增加底部合計(jì)行統(tǒng)計(jì)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支