我正在从 wp8 移植一个应用程序,该应用程序需要播放可以重叠的各种声音。到目前为止,我发现它使用 MediaElement 的唯一方法,但这不允许重叠的声音。
问题 - 用于播放重叠音频的最简单和最好的音频引擎是什么?理想情况下,我需要一个小例子来说明如何做到这一点。
我查看了 WASAPI(http://code.msdn.microsoft.com/windowsapps/Windows-Audio-Session-22dcab6b),但它看起来不支持简单播放?
也许我可以包装 MediaFoundation 并从 winrt 调用它?(在 WinRT 上播放 MediaEngine 音频)
这是我现在的代码,但是当我播放新声音时,它会切断以前播放的声音,而不是混合它们。
ThreadUtility.runOnUiThread(
async delegate()
{
// TODO doesn't allow sounds to overlap!
Uri uri = new Uri(R.base_uri, R.raw.URI_PREFIX + resourceId);
StorageFile storageFile =
await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(
uri);
MediaElement element = new MediaElement();
var randomAccessStream = await storageFile.OpenReadAsync();
element.SetSource(randomAccessStream, storageFile.ContentType);
element.Volume = volume;
element.PlaybackRate = pitch;
//TODO element.Pan = pan;
element.Play();
}
);
解决方案(根据菲利普的回答):
在页面类中:
var mediaElements = new LinkedList<MediaElement>();
{
for (int channel = 0; channel < TeacherSoundGroover.NUM_CHANNELS; channel++)
{
var mediaElement = new MediaElement();
mediaElements.add(mediaElement);
// Must be in the tree otherwise it won't overlap!
m_titlePanel.Children.Add(mediaElement);
}
}
m_soundPlayer = new MySoundPlayer(mediaElements);
}
在 MySoundPlayer 类中:
ThreadUtility.runOnUiThread(
async delegate()
{
Uri uri = new Uri(R.base_uri, R.raw.URI_PREFIX + resourceId);
StorageFile storageFile =
await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(
uri);
if(m_mediaElements != null)
{
int count = m_mediaElements.size();
if (count > 0)
{
int channel = m_nextMediaElementToUse % count;
m_nextMediaElementToUse++;
MediaElement element = m_mediaElements.get(channel);
var randomAccessStream = await storageFile.OpenReadAsync();
element.Stop();
element.DefaultPlaybackRate = rate;
element.SetSource(randomAccessStream, storageFile.ContentType);
element.Volume = volume;
element.Balance = pan;
element.Play();
}
}
}
);