[英]Windows 8 xaudio2 playing wav file in multitouch app
我正在學習如何使用xAudio2。 在Windows 8的Visual Studio 2012 Express中制作了一個簡單的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 Simulator中測試。 如果我按一根手指,那么一切都很好。 當我單擊按鈕時-放開手指時聲音開始播放-聲音停止。
如果我用兩根手指單擊並松開兩只手指,聲音會繼續播放。 結果就是混疊。
稱為兩個事件:btnAPointerPressed和兩個事件:btnAPointerReleased,但聲音繼續播放。 好像音頻流凍結並繼續播放。 好像音頻流凍結並繼續播放。 我想了解haudio2的問題嗎? 還是我沒有正確地做某事?
當您再次調用Play()
時,先前的SourceVoice
將在SoundEffect
替換為新的SourceVoice
,但您永遠不會停止舊的SourceVoice
。 您應該為每次觸摸創建一個新的SourceVoice,但要使它們都與指針ID關聯,以便在釋放關聯的指針時可以停止它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.