【问题标题】:MediaEngine audio playback on WinRTWinRT 上的 MediaEngine 音频播放
【发布时间】:2012-07-03 09:07:53
【问题描述】:

我正在尝试将音乐添加到我在 WinRT 上运行的游戏中。音乐应采用编码格式(mp3、ogg 等),并且应可流式传输并由硬件解码(出于性能原因)。

我查看了示例,发现 MediaEngine 可以做这样的事情(我希望如此)。

但是,我在使它工作时遇到了问题。每次我尝试从IRandomAccessStream 通过MFCreateMFByteStreamOnStreamEx() 创建IMFByteStream 时,我都会收到 ComExceptions。

可能是我没有正确处理任务,因为它们对我来说是一个新的范例。

这是一些代码(与我之前提到的示例非常相似):

void MyMedia::PlayMusic ()
{
    try
    {
        StorageFolder^ installedLocation = Windows::ApplicationModel::Package::Current->InstalledLocation;

        Concurrency::task<StorageFile^> m_pickFileTask = Concurrency::task<StorageFile^>(installedLocation->GetFileAsync("music.mp3"), m_tcs.get_token());

        SetURL(StringHelper::toString("music.mp3"));

        auto player = this;
        m_pickFileTask.then([&player](StorageFile^ fileHandle)
        {
            Concurrency::task<IRandomAccessStream^> fOpenStreamTask = Concurrency::task<IRandomAccessStream^> (fileHandle->OpenAsync(Windows::Storage::FileAccessMode::Read));
            fOpenStreamTask.then([&player](IRandomAccessStream^ streamHandle)
                {
                    try
                    {
                        player->SetBytestream(streamHandle);

                        if (player->m_spMediaEngine)
                        {
                            MEDIA::ThrowIfFailed(
                                player->m_spMediaEngine->Play()
                                );

                        }
                    } catch(Platform::Exception^)
                    {
                        MEDIA::ThrowIfFailed(E_UNEXPECTED);
                    }

                }
            );  
        }
        );

    } catch(Platform::Exception^ ex)
    {
        Printf("error: %s", ex->Message);
    }


}

void MyMedia::SetBytestream(IRandomAccessStream^ streamHandle)
{
    HRESULT hr = S_OK;
    ComPtr<IMFByteStream> spMFByteStream = nullptr; 

    //The following line always throws a ComException
    MEDIA::ThrowIfFailed(
        MFCreateMFByteStreamOnStreamEx((IUnknown*)streamHandle, &spMFByteStream)
        );

    MEDIA::ThrowIfFailed(
        m_spEngineEx->SetSourceFromByteStream(spMFByteStream.Get(), m_bstrURL)
        );  

    return;
}

奖励:如果您知道更好的解决方案来满足我的音频需求,请发表评论。

【问题讨论】:

  • 关于硬件解码:这取决于硬件(有关更完整的概述,请参阅blogs.msdn.com/b/b8/archive/2012/06/08/…
  • 感谢您的链接。我指望所有主要的手持设备都能做到这一点。否则它们就不是可行的游戏平台。

标签: mp3 windows-runtime ogg


【解决方案1】:

我设法解决了这个问题。我发现了两个问题。

  • 媒体基础未初始化

在使用 Media Foundation 之前需要调用MFStartup(MF_VERSION);。我在创建媒体引擎之前添加了这段代码。

  • 引用指针。

m_pickFileTask.then([&amp;player](StorageFile^ fileHandle) 应该是m_pickFileTask.then([player](StorageFile^ fileHandle)This 已经是指向当前类的指针,而&amp; 提供了变量的地址,所以我实际上是在传递指针的指针。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多