Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 同时播放三个声音#_C#_Audio_Axwindowsmediaplayer - Fatal编程技术网

C# 同时播放三个声音#

C# 同时播放三个声音#,c#,audio,axwindowsmediaplayer,C#,Audio,Axwindowsmediaplayer,我想同时播放三种声音,但第二种声音必须在一秒钟后播放,第三种声音必须在两秒钟后播放。我有以下代码: private void Play() { AxWindowsMediaPlayer player1 = new AxWindowsMediaPlayer(); player1.CreateControl(); AxWindowsMediaPlayer player2 = new AxWindowsMediaP

我想同时播放三种声音,但第二种声音必须在一秒钟后播放,第三种声音必须在两秒钟后播放。我有以下代码:

private void Play()
        {
            AxWindowsMediaPlayer player1 = new AxWindowsMediaPlayer();
            player1.CreateControl();
            AxWindowsMediaPlayer player2 = new AxWindowsMediaPlayer();
            player2.CreateControl();
            AxWindowsMediaPlayer player3 = new AxWindowsMediaPlayer();
            player3.CreateControl();
            player1.URL = "sounds\\1.wav";
            player1.Ctlcontrols.play();
            System.Threading.Thread.Sleep(1000);
            player2.URL = "sounds\\2.wav";
            player2.Ctlcontrols.play();
            System.Threading.Thread.Sleep(1000);
            player3.URL = "sounds\\3.wav";
            player3.Ctlcontrols.play();
为什么所有这些声音在两秒钟后一次播放?

我最终使用了SharpDX(可通过NuGet软件包
SharpDX
SharpDX.XAudio2

可以在我的一个GitHub项目中找到它的用法示例: 你也可以听到不同的声音在这里重叠

播放声音:

var backgroundMusicSound = new AudioClip(@".\Assets\Sounds\Music\Background.wav" /*Sound path*/, 1.0 /* Volumne*/, true /*Loop Forever?*/)

backgroundMusicSound.Play();
public class AudioClip
{
    private XAudio2 _xaudio = new XAudio2();
    private WaveFormat _waveFormat;
    private AudioBuffer _buffer;
    private SoundStream _soundstream;
    private SourceVoice _singleSourceVoice;
    private bool _loopForever;
    private bool _isPlaying = false; //Only applicable when _loopForever == false;
    private bool _isFading;
    private string _wavFilePath; //For debugging.
    private float _initialVolumne;

    public AudioClip(string wavFilePath, float initialVolumne = 1, bool loopForever = false)
    {
        _loopForever = loopForever;
        _wavFilePath = wavFilePath;
        _initialVolumne = initialVolumne;

        var masteringsound = new MasteringVoice(_xaudio); //Yes, this is required.
        var nativefilestream = new NativeFileStream(wavFilePath,
            NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read);

        _soundstream = new SoundStream(nativefilestream);

        _waveFormat = _soundstream.Format;
        _buffer = new AudioBuffer
        {
            Stream = _soundstream.ToDataStream(),
            AudioBytes = (int)_soundstream.Length,
            Flags = BufferFlags.EndOfStream
        };

        if (loopForever)
        {
            _buffer.LoopCount = 100;
        }
    }

    public void Play()
    {
        lock (this)
        {
            if (_loopForever == true)
            {
                if (_isPlaying)
                {
                    if (_isFading)
                    {
                        _isFading = false;
                        _singleSourceVoice.SetVolume(_initialVolumne);
                    }

                    return;
                }
                _singleSourceVoice = new SourceVoice(_xaudio, _waveFormat, true);
                _singleSourceVoice.SubmitSourceBuffer(_buffer, _soundstream.DecodedPacketsInfo);
                _singleSourceVoice.SetVolume(_initialVolumne);

                _singleSourceVoice.Start();
                _isPlaying = true;
                return;
            }
        }

        var sourceVoice = new SourceVoice(_xaudio, _waveFormat, true);
        sourceVoice.SubmitSourceBuffer(_buffer, _soundstream.DecodedPacketsInfo);
        sourceVoice.SetVolume(_initialVolumne);
        sourceVoice.Start();
    }

    public void Fade()
    {
        if (_isPlaying && _isFading == false)
        {
            _isFading = true;
            (new Thread(FadeThread)).Start();
        }
    }

    private void FadeThread()
    {
        float volumne;

        _singleSourceVoice.GetVolume(out volumne);

        while (_isFading && volumne > 0)
        {
            volumne -= 0.25f;
            volumne = volumne < 0 ? 0 : volumne;
            _singleSourceVoice.SetVolume(volumne);
            Thread.Sleep(100);
        }
        Stop();
    }

    public void Stop()
    {
        if (_loopForever == true)
        {
            if (_singleSourceVoice != null && _isPlaying)
            {
                _singleSourceVoice.Stop();
            }
            _isPlaying = false;
            _isFading = false;
        }
        else
        {
            throw new Exception("Cannot stop overlapped audio.");
        }
    }
}
private Dictionary<string, AudioClip> _audioClips { get; set; } = new Dictionary<string, AudioClip>();

public AudioClip GetSoundCached(string wavFilePath, float initialVolumne, bool loopForever = false)
{
    lock (_audioClips)
    {
        AudioClip result = null;

        wavFilePath = wavFilePath.ToLower();

        if (_audioClips.ContainsKey(wavFilePath))
        {
            result = _audioClips[wavFilePath];
        }
        else
        {
            result = new AudioClip(wavFilePath, initialVolumne, loopForever);
            _audioClips.Add(wavFilePath, result);
        }

        return result;
    }
}
我拼凑的班级:

var backgroundMusicSound = new AudioClip(@".\Assets\Sounds\Music\Background.wav" /*Sound path*/, 1.0 /* Volumne*/, true /*Loop Forever?*/)

backgroundMusicSound.Play();
public class AudioClip
{
    private XAudio2 _xaudio = new XAudio2();
    private WaveFormat _waveFormat;
    private AudioBuffer _buffer;
    private SoundStream _soundstream;
    private SourceVoice _singleSourceVoice;
    private bool _loopForever;
    private bool _isPlaying = false; //Only applicable when _loopForever == false;
    private bool _isFading;
    private string _wavFilePath; //For debugging.
    private float _initialVolumne;

    public AudioClip(string wavFilePath, float initialVolumne = 1, bool loopForever = false)
    {
        _loopForever = loopForever;
        _wavFilePath = wavFilePath;
        _initialVolumne = initialVolumne;

        var masteringsound = new MasteringVoice(_xaudio); //Yes, this is required.
        var nativefilestream = new NativeFileStream(wavFilePath,
            NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read);

        _soundstream = new SoundStream(nativefilestream);

        _waveFormat = _soundstream.Format;
        _buffer = new AudioBuffer
        {
            Stream = _soundstream.ToDataStream(),
            AudioBytes = (int)_soundstream.Length,
            Flags = BufferFlags.EndOfStream
        };

        if (loopForever)
        {
            _buffer.LoopCount = 100;
        }
    }

    public void Play()
    {
        lock (this)
        {
            if (_loopForever == true)
            {
                if (_isPlaying)
                {
                    if (_isFading)
                    {
                        _isFading = false;
                        _singleSourceVoice.SetVolume(_initialVolumne);
                    }

                    return;
                }
                _singleSourceVoice = new SourceVoice(_xaudio, _waveFormat, true);
                _singleSourceVoice.SubmitSourceBuffer(_buffer, _soundstream.DecodedPacketsInfo);
                _singleSourceVoice.SetVolume(_initialVolumne);

                _singleSourceVoice.Start();
                _isPlaying = true;
                return;
            }
        }

        var sourceVoice = new SourceVoice(_xaudio, _waveFormat, true);
        sourceVoice.SubmitSourceBuffer(_buffer, _soundstream.DecodedPacketsInfo);
        sourceVoice.SetVolume(_initialVolumne);
        sourceVoice.Start();
    }

    public void Fade()
    {
        if (_isPlaying && _isFading == false)
        {
            _isFading = true;
            (new Thread(FadeThread)).Start();
        }
    }

    private void FadeThread()
    {
        float volumne;

        _singleSourceVoice.GetVolume(out volumne);

        while (_isFading && volumne > 0)
        {
            volumne -= 0.25f;
            volumne = volumne < 0 ? 0 : volumne;
            _singleSourceVoice.SetVolume(volumne);
            Thread.Sleep(100);
        }
        Stop();
    }

    public void Stop()
    {
        if (_loopForever == true)
        {
            if (_singleSourceVoice != null && _isPlaying)
            {
                _singleSourceVoice.Stop();
            }
            _isPlaying = false;
            _isFading = false;
        }
        else
        {
            throw new Exception("Cannot stop overlapped audio.");
        }
    }
}
private Dictionary<string, AudioClip> _audioClips { get; set; } = new Dictionary<string, AudioClip>();

public AudioClip GetSoundCached(string wavFilePath, float initialVolumne, bool loopForever = false)
{
    lock (_audioClips)
    {
        AudioClip result = null;

        wavFilePath = wavFilePath.ToLower();

        if (_audioClips.ContainsKey(wavFilePath))
        {
            result = _audioClips[wavFilePath];
        }
        else
        {
            result = new AudioClip(wavFilePath, initialVolumne, loopForever);
            _audioClips.Add(wavFilePath, result);
        }

        return result;
    }
}
公共类音频剪辑
{
私有XAudio2 _xaudio=新的XAudio2();
专用波形(WaveFormat);;
专用音频缓冲区(U缓冲区),;
私人声流(SoundStream);;
私有SourceVoice singleSourceVoice;
永远的私人住宅;
private bool _isplay=false;//仅当_loopForever==false时适用;
私人住宅正在衰落;
专用字符串_wavFilePath;//用于调试。
私人浮动(初始卷);;
公共音频剪辑(字符串wavFilePath,float initialVolumne=1,bool loopForever=false)
{
_loopForever=loopForever;
_wavFilePath=wavFilePath;
_initialVolumne=initialVolumne;
var masteringsound=new MasteringVoice(xaudio);//是的,这是必需的。
var nativefilestream=新的nativefilestream(wavFilePath,
NativeFileMode.Open,NativeFileAccess.Read,NativeFileShare.Read);
_soundstream=新的soundstream(nativefilestream);
_波形格式=_soundstream.Format;
_缓冲区=新的音频缓冲区
{
Stream=\u soundstream.ToDataStream(),
AudioBytes=(int)_soundstream.Length,
Flags=BufferFlags.EndOfStream
};
如果(永远)
{
_buffer.LoopCount=100;
}
}
公共游戏
{
锁(这个)
{
如果(_loopForever==true)
{
如果(_显示)
{
如果(_正在衰退)
{
_isfalse=false;
_singleSourceVoice.SetVolume(_initialVolume);
}
返回;
}
_singleSourceVoice=新的SourceVoice(_xaudio,_waveFormat,true);
_singleSourceVoice.SubmitSourceBuffer(_buffer,_soundstream.DecodedPacketsInfo);
_singleSourceVoice.SetVolume(_initialVolume);
_singleSourceVoice.Start();
_isplay=true;
返回;
}
}
var sourceVoice=新的sourceVoice(_xaudio,_waveFormat,true);
sourceVoice.SubmitSourceBuffer(_buffer,_soundstream.DecodedPacketsInfo);
sourceVoice.SetVolume(_initialvolume);
sourceVoice.Start();
}
公共空间
{
如果(_isPlaying&&&u islaying==false)
{
_IsWaveling=真;
(新线程(FadeThread)).Start();
}
}
私有void FadeThread()
{
漂浮体积;
_singleSourceVoice.GetVolume(输出音量);
当(_正在衰退且卷>0)
{
体积-=0.25f;
体积=体积<0?0:体积;
_singleSourceVoice.SetVolume(音量);
睡眠(100);
}
停止();
}
公共停车场()
{
如果(_loopForever==true)
{
if(_singleSourceVoice!=null&&u isplay)
{
_singleSourceVoice.Stop();
}
_isplay=false;
_isfalse=false;
}
其他的
{
抛出新异常(“无法停止重叠音频”);
}
}
}
还应注意,加载声音可能是一个繁重的过程,因此如果您经常这样做,那么您可能希望像我一样缓存它们:

var backgroundMusicSound = new AudioClip(@".\Assets\Sounds\Music\Background.wav" /*Sound path*/, 1.0 /* Volumne*/, true /*Loop Forever?*/)

backgroundMusicSound.Play();
public class AudioClip
{
    private XAudio2 _xaudio = new XAudio2();
    private WaveFormat _waveFormat;
    private AudioBuffer _buffer;
    private SoundStream _soundstream;
    private SourceVoice _singleSourceVoice;
    private bool _loopForever;
    private bool _isPlaying = false; //Only applicable when _loopForever == false;
    private bool _isFading;
    private string _wavFilePath; //For debugging.
    private float _initialVolumne;

    public AudioClip(string wavFilePath, float initialVolumne = 1, bool loopForever = false)
    {
        _loopForever = loopForever;
        _wavFilePath = wavFilePath;
        _initialVolumne = initialVolumne;

        var masteringsound = new MasteringVoice(_xaudio); //Yes, this is required.
        var nativefilestream = new NativeFileStream(wavFilePath,
            NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read);

        _soundstream = new SoundStream(nativefilestream);

        _waveFormat = _soundstream.Format;
        _buffer = new AudioBuffer
        {
            Stream = _soundstream.ToDataStream(),
            AudioBytes = (int)_soundstream.Length,
            Flags = BufferFlags.EndOfStream
        };

        if (loopForever)
        {
            _buffer.LoopCount = 100;
        }
    }

    public void Play()
    {
        lock (this)
        {
            if (_loopForever == true)
            {
                if (_isPlaying)
                {
                    if (_isFading)
                    {
                        _isFading = false;
                        _singleSourceVoice.SetVolume(_initialVolumne);
                    }

                    return;
                }
                _singleSourceVoice = new SourceVoice(_xaudio, _waveFormat, true);
                _singleSourceVoice.SubmitSourceBuffer(_buffer, _soundstream.DecodedPacketsInfo);
                _singleSourceVoice.SetVolume(_initialVolumne);

                _singleSourceVoice.Start();
                _isPlaying = true;
                return;
            }
        }

        var sourceVoice = new SourceVoice(_xaudio, _waveFormat, true);
        sourceVoice.SubmitSourceBuffer(_buffer, _soundstream.DecodedPacketsInfo);
        sourceVoice.SetVolume(_initialVolumne);
        sourceVoice.Start();
    }

    public void Fade()
    {
        if (_isPlaying && _isFading == false)
        {
            _isFading = true;
            (new Thread(FadeThread)).Start();
        }
    }

    private void FadeThread()
    {
        float volumne;

        _singleSourceVoice.GetVolume(out volumne);

        while (_isFading && volumne > 0)
        {
            volumne -= 0.25f;
            volumne = volumne < 0 ? 0 : volumne;
            _singleSourceVoice.SetVolume(volumne);
            Thread.Sleep(100);
        }
        Stop();
    }

    public void Stop()
    {
        if (_loopForever == true)
        {
            if (_singleSourceVoice != null && _isPlaying)
            {
                _singleSourceVoice.Stop();
            }
            _isPlaying = false;
            _isFading = false;
        }
        else
        {
            throw new Exception("Cannot stop overlapped audio.");
        }
    }
}
private Dictionary<string, AudioClip> _audioClips { get; set; } = new Dictionary<string, AudioClip>();

public AudioClip GetSoundCached(string wavFilePath, float initialVolumne, bool loopForever = false)
{
    lock (_audioClips)
    {
        AudioClip result = null;

        wavFilePath = wavFilePath.ToLower();

        if (_audioClips.ContainsKey(wavFilePath))
        {
            result = _audioClips[wavFilePath];
        }
        else
        {
            result = new AudioClip(wavFilePath, initialVolumne, loopForever);
            _audioClips.Add(wavFilePath, result);
        }

        return result;
    }
}
private Dictionary\u audioClips{get;set;}=new Dictionary();
公共音频剪辑GetSoundCached(字符串wavFilePath、浮点initialVolumne、bool loopForever=false)
{
锁定(音频剪辑)
{
音频剪辑结果=空;
wavFilePath=wavFilePath.ToLower();
if(_audioClips.ContainsKey(wavFilePath))
{
结果=_音频剪辑[wavFilePath];
}
其他的
{
结果=新音频剪辑(wavFilePath、InitialVolume、loopForever);
_添加(wavFilePath,result);
}
返回结果;
}
}
我最终使用了SharpDX(可通过NuGet软件包
SharpDX
SharpDX.XAudio2

可以在我的一个GitHub项目中找到它的用法示例: 你也可以听到不同的声音在这里重叠

播放声音:

var backgroundMusicSound = new AudioClip(@".\Assets\Sounds\Music\Background.wav" /*Sound path*/, 1.0 /* Volumne*/, true /*Loop Forever?*/)

backgroundMusicSound.Play();
public class AudioClip
{
    private XAudio2 _xaudio = new XAudio2();
    private WaveFormat _waveFormat;
    private AudioBuffer _buffer;
    private SoundStream _soundstream;
    private SourceVoice _singleSourceVoice;
    private bool _loopForever;
    private bool _isPlaying = false; //Only applicable when _loopForever == false;
    private bool _isFading;
    private string _wavFilePath; //For debugging.
    private float _initialVolumne;

    public AudioClip(string wavFilePath, float initialVolumne = 1, bool loopForever = false)
    {
        _loopForever = loopForever;
        _wavFilePath = wavFilePath;
        _initialVolumne = initialVolumne;

        var masteringsound = new MasteringVoice(_xaudio); //Yes, this is required.
        var nativefilestream = new NativeFileStream(wavFilePath,
            NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read);

        _soundstream = new SoundStream(nativefilestream);

        _waveFormat = _soundstream.Format;
        _buffer = new AudioBuffer
        {
            Stream = _soundstream.ToDataStream(),
            AudioBytes = (int)_soundstream.Length,
            Flags = BufferFlags.EndOfStream
        };

        if (loopForever)
        {
            _buffer.LoopCount = 100;
        }
    }

    public void Play()
    {
        lock (this)
        {
            if (_loopForever == true)
            {
                if (_isPlaying)
                {
                    if (_isFading)
                    {
                        _isFading = false;
                        _singleSourceVoice.SetVolume(_initialVolumne);
                    }

                    return;
                }
                _singleSourceVoice = new SourceVoice(_xaudio, _waveFormat, true);
                _singleSourceVoice.SubmitSourceBuffer(_buffer, _soundstream.DecodedPacketsInfo);
                _singleSourceVoice.SetVolume(_initialVolumne);

                _singleSourceVoice.Start();
                _isPlaying = true;
                return;
            }
        }

        var sourceVoice = new SourceVoice(_xaudio, _waveFormat, true);
        sourceVoice.SubmitSourceBuffer(_buffer, _soundstream.DecodedPacketsInfo);
        sourceVoice.SetVolume(_initialVolumne);
        sourceVoice.Start();
    }

    public void Fade()
    {
        if (_isPlaying && _isFading == false)
        {
            _isFading = true;
            (new Thread(FadeThread)).Start();
        }
    }

    private void FadeThread()
    {
        float volumne;

        _singleSourceVoice.GetVolume(out volumne);

        while (_isFading && volumne > 0)
        {
            volumne -= 0.25f;
            volumne = volumne < 0 ? 0 : volumne;
            _singleSourceVoice.SetVolume(volumne);
            Thread.Sleep(100);
        }
        Stop();
    }

    public void Stop()
    {
        if (_loopForever == true)
        {
            if (_singleSourceVoice != null && _isPlaying)
            {
                _singleSourceVoice.Stop();
            }
            _isPlaying = false;
            _isFading = false;
        }
        else
        {
            throw new Exception("Cannot stop overlapped audio.");
        }
    }
}
private Dictionary<string, AudioClip> _audioClips { get; set; } = new Dictionary<string, AudioClip>();

public AudioClip GetSoundCached(string wavFilePath, float initialVolumne, bool loopForever = false)
{
    lock (_audioClips)
    {
        AudioClip result = null;

        wavFilePath = wavFilePath.ToLower();

        if (_audioClips.ContainsKey(wavFilePath))
        {
            result = _audioClips[wavFilePath];
        }
        else
        {
            result = new AudioClip(wavFilePath, initialVolumne, loopForever);
            _audioClips.Add(wavFilePath, result);
        }

        return result;
    }
}
我拼凑的班级:

var backgroundMusicSound = new AudioClip(@".\Assets\Sounds\Music\Background.wav" /*Sound path*/, 1.0 /* Volumne*/, true /*Loop Forever?*/)

backgroundMusicSound.Play();
public class AudioClip
{
    private XAudio2 _xaudio = new XAudio2();
    private WaveFormat _waveFormat;
    private AudioBuffer _buffer;
    private SoundStream _soundstream;
    private SourceVoice _singleSourceVoice;
    private bool _loopForever;
    private bool _isPlaying = false; //Only applicable when _loopForever == false;
    private bool _isFading;
    private string _wavFilePath; //For debugging.
    private float _initialVolumne;

    public AudioClip(string wavFilePath, float initialVolumne = 1, bool loopForever = false)
    {
        _loopForever = loopForever;
        _wavFilePath = wavFilePath;
        _initialVolumne = initialVolumne;

        var masteringsound = new MasteringVoice(_xaudio); //Yes, this is required.
        var nativefilestream = new NativeFileStream(wavFilePath,
            NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read);

        _soundstream = new SoundStream(nativefilestream);

        _waveFormat = _soundstream.Format;
        _buffer = new AudioBuffer
        {
            Stream = _soundstream.ToDataStream(),
            AudioBytes = (int)_soundstream.Length,
            Flags = BufferFlags.EndOfStream
        };

        if (loopForever)
        {
            _buffer.LoopCount = 100;
        }
    }

    public void Play()
    {
        lock (this)
        {
            if (_loopForever == true)
            {
                if (_isPlaying)
                {
                    if (_isFading)
                    {
                        _isFading = false;
                        _singleSourceVoice.SetVolume(_initialVolumne);
                    }

                    return;
                }
                _singleSourceVoice = new SourceVoice(_xaudio, _waveFormat, true);
                _singleSourceVoice.SubmitSourceBuffer(_buffer, _soundstream.DecodedPacketsInfo);
                _singleSourceVoice.SetVolume(_initialVolumne);

                _singleSourceVoice.Start();
                _isPlaying = true;
                return;
            }
        }

        var sourceVoice = new SourceVoice(_xaudio, _waveFormat, true);
        sourceVoice.SubmitSourceBuffer(_buffer, _soundstream.DecodedPacketsInfo);
        sourceVoice.SetVolume(_initialVolumne);
        sourceVoice.Start();
    }

    public void Fade()
    {
        if (_isPlaying && _isFading == false)
        {
            _isFading = true;
            (new Thread(FadeThread)).Start();
        }
    }

    private void FadeThread()
    {
        float volumne;

        _singleSourceVoice.GetVolume(out volumne);

        while (_isFading && volumne > 0)
        {
            volumne -= 0.25f;
            volumne = volumne < 0 ? 0 : volumne;
            _singleSourceVoice.SetVolume(volumne);
            Thread.Sleep(100);
        }
        Stop();
    }

    public void Stop()
    {
        if (_loopForever == true)
        {
            if (_singleSourceVoice != null && _isPlaying)
            {
                _singleSourceVoice.Stop();
            }
            _isPlaying = false;
            _isFading = false;
        }
        else
        {
            throw new Exception("Cannot stop overlapped audio.");
        }
    }
}
private Dictionary<string, AudioClip> _audioClips { get; set; } = new Dictionary<string, AudioClip>();

public AudioClip GetSoundCached(string wavFilePath, float initialVolumne, bool loopForever = false)
{
    lock (_audioClips)
    {
        AudioClip result = null;

        wavFilePath = wavFilePath.ToLower();

        if (_audioClips.ContainsKey(wavFilePath))
        {
            result = _audioClips[wavFilePath];
        }
        else
        {
            result = new AudioClip(wavFilePath, initialVolumne, loopForever);
            _audioClips.Add(wavFilePath, result);
        }

        return result;
    }
}
公共类音频剪辑
{
私有XAudio2 _xaudio=新的XAudio2();
专用波形(WaveFormat);;
专用音频缓冲区(U缓冲区),;
私人声流(SoundStream);;
私有SourceVoice singleSourceVoice;
永远的私人住宅;
private bool _isplay=false;//仅当_loopForever==false时适用;
私人住宅正在衰落;
专用字符串_wavFilePath;//用于调试。
私人浮动(初始卷);;
公共音频剪辑(字符串wavFilePath,float initialVolumne=1,bool loopForever=false)
{
_loopForever=loopForever;
_wavFilePath=wavFilePath;
_initialVolumne=initialVolumne;
var masteringsound=new MasteringVoice(xaudio);//是的,这是必需的。
var nativefilestream=新的nativefilestream(wavFilePath,
NativeFileMode.Open,NativeFileAccess.Read,NativeFileShare.Read);
_soundstream=新的soundstream(nativefilestream);
_波形格式=_soundstream.Format;
_缓冲区=新的音频缓冲区
{
流=_soundstr