BackgroundAudioPlayer是“播放”,但没有要求GetSampleAsync() [英] BackgroundAudioPlayer is 'Playing' but not calling GetSampleAsync()

查看:224
本文介绍了BackgroundAudioPlayer是“播放”,但没有要求GetSampleAsync()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个后台代理网络使用自定义MediaStreamSource的流媒体音乐。在良好的网络条件下能正常工作,但是当网络连接是参差不齐一个奇怪的问题表面上。

I am streaming music from the web in a background agent using a custom MediaStreamSource. In good network conditions this works fine, but when network connectivity is spotty a strange problem surfaces.

在轨道开始播放,一切顺利了通过第一次调用MediaStreamSource的.GetSampleAsync()。因为连接是参差不齐,如果没有足够的数据可用源调用ReportGetSampleProgress(双),并返回不报告的样本。这是根据MSDN文档和代码示例。

When a track starts playback, all goes well up through the first call to MediaStreamSource.GetSampleAsync(). Because the connection is spotty, if not enough data is available the source calls ReportGetSampleProgress(double) and returns without reporting a sample. This is in accordance with MSDN documentation and code samples.

什么是好奇的是,有以GetSampleAsync没有进一步的来电了!作为缓冲继续,源继续ReportGetSampleProgress直到样品准备,当它调用 ReportGetSampleProgress(1.0)来表示一个完整的缓冲区。

What's curious is that there are no further calls to GetSampleAsync at all! As buffering continues, the source continues to ReportGetSampleProgress until a sample is ready, when it calls ReportGetSampleProgress(1.0) to indicate a full buffer.

我试过几种方法,其中包括:

I've tried several approaches, including:


  • ReportGetSampleCompleted 当缓冲完毕;这种失败,因为下载的事件都在任意的线程,这种方法是既要调用线程以及是否GetSampleAsync的调用是在栈上明显敏感;无效的调用的情况下导致COM错误

  • 在精确的错误情况,停止和启动BackgroundAudioPlayer:这个无法重新启动流

  • ReportGetSampleCompleted when buffering is complete; this fails because download events come in on arbitrary threads and this method is evidently sensitive both to the calling thread and whether a call to GetSampleAsync is on the stack; invalid calling circumstances result in COM errors.
  • In the precise error condition, stop and start the BackgroundAudioPlayer: this fails to restart streaming.

我怎样才能流又往前走,一旦最初的读取故障样本挂东西呢?

How can I get streaming going again once the initial failure to read a sample hangs things?

推荐答案

碰巧的是,解决方案似乎是打破由顾名思义合约 GetSampleAsync ,当没有足够的数据已经被缓存在该方法块。然后该流回调可以脉冲的锁定的对象,和样品的读取可以重试。像这样的东西效果很好:

As it happens, a solution appears to be to break the contract suggested by the name GetSampleAsync, and block in that method when not enough data has been buffered. The stream callbacks can then pulse the locked object, and the sample read can be retried. Something like this works well:

private void OnMoreDataDownloaded(object sender, EventArgs e)
{
    // We're on an arbitrary thread, so instead of reporting
    // a sample here we should just pulse.
    lock (buffering_lock) {
        is_buffering = false;
        Monitor.Pulse(buffering_lock);
    }
}

protected override void GetSampleAsync()
{
    while (we_need_more_data) {
        lock (buffering_lock) {
            is_buffering = true;
            while (is_buffering) {
                Monitor.Wait(buffering_lock);
            }
    }

    // code code code
    ReportGetSampleCompleted(sample);
}



这似乎是在一个异步方法阻止可能并不明智,但在设备上运行该代码的经验表明并非如此。每的http:/ /msdn.microsoft.com/en-us/library/system.windows.media.mediastreamsource.getsampleasync(v=vs.95).aspx ,阻塞可以阻止其它数据流不会被读出。作为流媒体音乐应用程序,我们永远只能成为一个流的时间,所以在这种情况下,它似乎就好了。

It would seem that blocking in an Async method might not be wise, but the experience of running this code on a device suggests otherwise. Per http://msdn.microsoft.com/en-us/library/system.windows.media.mediastreamsource.getsampleasync(v=vs.95).aspx, blocking could prevent other streams from being read. As a streaming music app, we only ever serve one stream at a time, so in this case it seems that we're OK.

我希望我知道的通用解决方案在这里,虽然,因为这显然是欺骗。

I wish I knew a general solution here, though, because this clearly is cheating.

这篇关于BackgroundAudioPlayer是“播放”,但没有要求GetSampleAsync()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆