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.
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.
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
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?