简体   繁体   English

同时播放三个声音 c#

[英]Play three sounds simultaneously c#

I want to play three sounds simultaneously, but second sound must play after one seconds, third sound after two seconds.我想同时播放三个声音,但第二个声音必须在一秒钟后播放,第三个声音在两秒钟后播放。 I have this code:我有这个代码:

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();

Why all this sounds are playing in one time after two seconds?为什么所有这些声音都在两秒后播放?

I ended up using SharpDX (available via NuGet packages SharpDX and SharpDX.XAudio2 .我最终使用了 SharpDX(可通过 NuGet 包SharpDXSharpDX.XAudio2

An example of its usage can be found in one of my GitHub projects: 2DAI You can hear the various sounds overlapping in this screen recording as well.可以在我的 GitHub 项目之一中找到其用法示例: 2DAI您也可以在此屏幕录制中听到重叠的各种声音。

Playing a sound:播放声音:

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

backgroundMusicSound.Play();

The class that I pieced together:我拼凑的课程:

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.");
        }
    }
}

It should also be notes that loading the sounds can be a heavy process, so if you are doing it a lot then you might want to cache them as I did:还应该注意的是,加载声音可能是一个繁重的过程,所以如果你经常这样做,那么你可能想像我一样缓存它们:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM