Windows 8 xaudio2 在多点触控应用程序中播放 wav 文件
Windows 8 xaudio2 playing wav file in multitouch app
我正在学习如何使用 xAudio2。在 Visual Studio 2012 Express For Windows 8 中做了一个简单的应用程序 Windows 8。
简单的 xAudio2 播放器 class:
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 但声音继续播放。就好像音频流冻结并继续播放一样。就好像音频流冻结并继续播放一样。
我想了解haudio2的问题?还是我做的不对?
当您再次调用 Play()
时 - 之前的 SourceVoice
会在您的 SoundEffect
中被新的替换,但您永远不会停止旧的。您应该为每次触摸创建一个新的 SourceVoice,但让它们都与指针 ID 相关联,以便我们可以在释放相关指针时停止它们中的每一个。
我正在学习如何使用 xAudio2。在 Visual Studio 2012 Express For Windows 8 中做了一个简单的应用程序 Windows 8。 简单的 xAudio2 播放器 class:
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 但声音继续播放。就好像音频流冻结并继续播放一样。就好像音频流冻结并继续播放一样。 我想了解haudio2的问题?还是我做的不对?
当您再次调用 Play()
时 - 之前的 SourceVoice
会在您的 SoundEffect
中被新的替换,但您永远不会停止旧的。您应该为每次触摸创建一个新的 SourceVoice,但让它们都与指针 ID 相关联,以便我们可以在释放相关指针时停止它们中的每一个。