In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use DirectX.DirectSound to play voice in C#". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to play voice with DirectX.DirectSound in C#" can help you solve the problem.
Characteristics of DirectX.DirectSound
1. Multiple voice messages can be played at the same time
2. Can be divided into left and right channels for playback.
3. The voice being played can be released at any time.
This component handles the flow:
1. Create a playback thread
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;}}); task.Start ();}
2. Release the playback thread
Public void StopDirectXSoundThread () {IsStart = false;}
3. Determine whether to play or not. Judge whether or not to play by the conditions of PlayPositionplayback null 0 and playback buffer.
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. Play audio
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);//设置缓冲区为默认播放 } IsCreate = true; IsPlayVoice = false; } catch (Exception ex) { LogHelper.Debug(ex); }} 左右声道通过secBuffer.Pan属性进行控制,值含义见下图: a、Center中心通道,左右通道同时播放,默认值0 b、Right右通道,值10000 c、Right左通道,值-100005. Clear the playing audio, and the playing audio can be released by the Dispose () method.
Public void ClearPlay () {if (secBuffer! = null) {soundlist.Clear (); secBuffer.Dispose (); IsCreate = false;}}
6. Definition
/ playback device / private Device secDev = new Device (); / private SecondaryBuffer secBuffer = null; / Visualization component / private Control control; / whether it has been created / private bool IsCreate = false; on "how to use DirectX.DirectSound to play voice in C#" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.