在 UWP 中使用 NAudio 播放多个音频文件
Playback of multiple Audiofiles with NAudio in UWP
我正在开发一个可以同时播放多个音频流的通用应用程序。我需要能够分别更改每个流的音量。我尝试使用 MixinSampleProvider,如 Mark Heath 的 blog entry 所示,但在开始播放时出现 com 异常(在 MediaFoundationReaderUniversal 中)。我对 NAudio 还很陌生,所以我有点迷路了。我已经研究了很多,但关于 UWP 的样本非常少。以下是我已经拥有的。我应该使用什么是正确的 way/which class?
这是我目前所拥有的:
IStorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(path, UriKind.Absolute));
var stream = await file.OpenAsync(FileAccessMode.Read);
var player = new WasapiOutRT(AudioClientShareMode.Shared, 200);
player.Init(() => { return new MediaFoundationReaderUniversal(stream); });
player.Play();
我可以用 UWP 兼容的方式做我想做的事吗?
编辑:
这是我得到的例外,它有一半是德语,但重要的部分是英语。最后一部分说 "interface not supported":
Unable to cast COM object of type 'System.__ComObject' to interface type 'NAudio.MediaFoundation.IMFSourceReader'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{70AE66F2-C809-4E4F-8915-BDCB406B7993}' failed due to the following error: Schnittstelle nicht unterstützt (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
in MediaFoundationReader.Read pReader.ReadSample(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, out actualStreamIndex, out dwFlags, out timestamp, out pSample);
好的,我自己解决了,问题是当我为 player.Init() 函数使用闭包时,像这样:
IStorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(path, UriKind.Absolute));
var stream = await file.OpenAsync(FileAccessMode.Read);
var waveChannel32 = new WaveChannel32(new MediaFoundationReaderUniversal(stream));
var mixer = new MixingSampleProvider(new ISampleProvider[] { waveChannel32.ToSampleProvider() });
_audioOutput = new WasapiOutRT(AudioClientShareMode.Shared, 200);
_audioOutput.Init(() =>
{
return mixer.ToWaveProvider();
});
_audioOutput.Play();
当我将上面的代码更改为此时,没有抛出 COM 异常并且代码运行正常:
IStorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(path, UriKind.Absolute));
var stream = await file.OpenAsync(FileAccessMode.Read);
_audioOutput = new WasapiOutRT(AudioClientShareMode.Shared, 200);
_audioOutput.Init(() =>
{
var waveChannel32 = new WaveChannel32(new MediaFoundationReaderUniversal(stream));
var mixer = new MixingSampleProvider(new ISampleProvider[] { waveChannel32.ToSampleProvider() });
return mixer.ToWaveProvider();
});
_audioOutput.Play();
我正在开发一个可以同时播放多个音频流的通用应用程序。我需要能够分别更改每个流的音量。我尝试使用 MixinSampleProvider,如 Mark Heath 的 blog entry 所示,但在开始播放时出现 com 异常(在 MediaFoundationReaderUniversal 中)。我对 NAudio 还很陌生,所以我有点迷路了。我已经研究了很多,但关于 UWP 的样本非常少。以下是我已经拥有的。我应该使用什么是正确的 way/which class?
这是我目前所拥有的:
IStorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(path, UriKind.Absolute));
var stream = await file.OpenAsync(FileAccessMode.Read);
var player = new WasapiOutRT(AudioClientShareMode.Shared, 200);
player.Init(() => { return new MediaFoundationReaderUniversal(stream); });
player.Play();
我可以用 UWP 兼容的方式做我想做的事吗?
编辑:
这是我得到的例外,它有一半是德语,但重要的部分是英语。最后一部分说 "interface not supported":
Unable to cast COM object of type 'System.__ComObject' to interface type 'NAudio.MediaFoundation.IMFSourceReader'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{70AE66F2-C809-4E4F-8915-BDCB406B7993}' failed due to the following error: Schnittstelle nicht unterstützt (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
in MediaFoundationReader.Read pReader.ReadSample(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, out actualStreamIndex, out dwFlags, out timestamp, out pSample);
好的,我自己解决了,问题是当我为 player.Init() 函数使用闭包时,像这样:
IStorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(path, UriKind.Absolute));
var stream = await file.OpenAsync(FileAccessMode.Read);
var waveChannel32 = new WaveChannel32(new MediaFoundationReaderUniversal(stream));
var mixer = new MixingSampleProvider(new ISampleProvider[] { waveChannel32.ToSampleProvider() });
_audioOutput = new WasapiOutRT(AudioClientShareMode.Shared, 200);
_audioOutput.Init(() =>
{
return mixer.ToWaveProvider();
});
_audioOutput.Play();
当我将上面的代码更改为此时,没有抛出 COM 异常并且代码运行正常:
IStorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(path, UriKind.Absolute));
var stream = await file.OpenAsync(FileAccessMode.Read);
_audioOutput = new WasapiOutRT(AudioClientShareMode.Shared, 200);
_audioOutput.Init(() =>
{
var waveChannel32 = new WaveChannel32(new MediaFoundationReaderUniversal(stream));
var mixer = new MixingSampleProvider(new ISampleProvider[] { waveChannel32.ToSampleProvider() });
return mixer.ToWaveProvider();
});
_audioOutput.Play();