温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中如何使用DirectX.DirectSound播放语音

发布时间:2022-03-28 09:12:17 来源:亿速云 阅读:443 作者:iii 栏目:开发技术

这篇文章主要介绍“C#中如何使用DirectX.DirectSound播放语音”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C#中如何使用DirectX.DirectSound播放语音”文章能帮助大家解决问题。

DirectX.DirectSound的特点

1、可同时播放多条语音

2、可分左右声道进行播放

3、可随时释放正在播放的语音

此组件处理流程:

1、创建播放线程

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);//设置缓冲区为默认播放 
                }
                IsCreate = true;
                IsPlayVoice = false;
            }
            catch (Exception ex)
            {
                LogHelper.Debug(ex);
     }
}

左右声道通过secBuffer.Pan属性进行控制,值含义见下图:

a、Center中心通道,左右通道同时播放,默认值0

b、Right右通道,值10000

c、Right左通道,值-10000

C#中如何使用DirectX.DirectSound播放语音

5、清除播放中音频 ,播放中的音频可以通过Dispose()方法进行释放

public void ClearPlay()
  {
  if (secBuffer != null)
   {
    soundlist.Clear();
    secBuffer.Dispose();
    IsCreate = false;
     }
}

6、定义

/// <summary>
/// 播放设备
/// </summary>
private Device secDev = new Device();
 
/// <summary>
/// 播放缓冲区
/// </summary>
private SecondaryBuffer secBuffer = null;
 
/// <summary>
/// 可视化组件
/// </summary>
private Control control;
 
/// <summary>
/// 是否被创建
/// </summary>
private bool IsCreate = false;

关于“C#中如何使用DirectX.DirectSound播放语音”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI