0

我正在学习如何使用 xAudio2。在 Visual Studio 2012 Express For Windows 8 中制作了一个简单的应用程序 Windows 8。简单的 xAudio2 播放器类:

public class SoundEffect 
{
    readonly XAudio2 _xaudio;
    readonly WaveFormat _waveFormat;
    readonly AudioBuffer _buffer;
    readonly SoundStream _soundstream;
    SourceVoice sourceVoice;

    public SoundEffect(string soundFxPath)
    {
        _xaudio = new XAudio2();
        var masteringsound = new MasteringVoice(_xaudio);

        var nativefilestream = new NativeFileStream(
        soundFxPath,
        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
        };
        //sourceVoice = null;


    }

    public void Play()
    {
            sourceVoice = new SourceVoice(_xaudio, _waveFormat, true);

            if (sourceVoice != null)
            {
                sourceVoice.FlushSourceBuffers();
                sourceVoice.SubmitSourceBuffer(_buffer, _soundstream.DecodedPacketsInfo);

                sourceVoice.Start();
            }
    }
    public void Stop()
    {
        sourceVoice.Stop();
    }
}

和 Xaml:

<Border Background="Gray" MinHeight="150" MinWidth="150" Margin="10,10,0,0" x:Name="A"  PointerPressed="btnAPointerPressed" PointerReleased="btnAPointerReleased" />

在此处输入图像描述

和:

private SoundEffect shotEffect = new SoundEffect(@"sounds\mywav.wav");
        private void btnAPointerPressed(object sender, PointerRoutedEventArgs e)
    {
        bool _hasCapture = ((Border)sender).CapturePointer(e.Pointer);
        shotEffect.Play();
    }

    private void btnAPointerReleased(object sender, PointerRoutedEventArgs e)
    {
        ((Border)sender).ReleasePointerCapture(e.Pointer);
        shotEffect.Stop();
    }

在 Windows 8 模拟器中测试。如果我按一根手指,那么一切都很好。当我点击按钮时——放开手指时会播放声音——声音停止。

在此处输入图像描述

如果我用两根手指单击并松开两根手指,声音会继续播放。结果是混叠。

在此处输入图像描述

调用了两个事件:btnAPointerPressed 和两个事件:btnAPointerReleased 但声音继续播放。好像音频流冻结并继续播放。好像音频流冻结并继续播放。我想了解问题 hadio2?或者我没有正确地做某事?

4

1 回答 1

1

当您Play()再次致电时 - 以前的电话SourceVoice会被您的新电话替换SoundEffect,但您永远不会停止旧电话。您应该在每次触摸时创建一个新的 SourceVoice,但要让它们都与指针 ID 相关联,以便我们可以在释放关联的指针时停止它们中的每一个。

于 2015-01-26T06:46:15.900 回答