Why is the MediaElement not playing a MediaStreamSource (in SilverLight)?

4.4k Views Asked by At

When I try to stream sound from my microphone, I need to get it through a MediaStreamSource. Therefore I first need to implement a MediaStreamSource for the pcm waveformat I get from my Microphone. There are at least two methods I think I need to implement. At first

protected override void OpenMediaAsync() {
   // Create description
   Dictionary<MediaStreamAttributeKeys, string> streamAttributes = new Dictionary<MediaStreamAttributeKeys, string>();
   streamAttributes[MediaStreamAttributeKeys.CodecPrivateData] = output.CodecPrivateData;
   audioDesc = new MediaStreamDescription(MediaStreamType.Audio, streamAttributes);

   // register stream
   Dictionary<MediaSourceAttributesKeys, string> sourceAttributes = new Dictionary<MediaSourceAttributesKeys, string>();
   List<MediaStreamDescription> availableStreams = new List<MediaStreamDescription>();

   availableStreams.Add(audioDesc);
   sourceAttributes[MediaSourceAttributesKeys.Duration] = TimeSpan.FromMinutes(0).Ticks.ToString(); // whatever I put here I get the same result.
   sourceAttributes[MediaSourceAttributesKeys.CanSeek] = false.ToString();

   ReportOpenMediaCompleted(sourceAttributes, availableStreams);
}

This works very well. My CodecPrivateData is '01000100401F0000803E0000020010000000' (PCM 1ch 16Bits 8kHz). This method gets called by setting the source as here:

WaveMediaStreamSource WaveStream = new WaveMediaStreamSource(output);
mediaElement.SetSource(WaveStream);
mediaElement.Play(); 

After Play() absoultely nothing happens. I would suggest the mediaElement should call at least once the method GetSampleAsync() of the MediaStreamSource. But it doesn't. I've noticed that the MediaElement doesn't make any call to the MediaStreamSource anymore.

While OpenMediaAsync the mediaElement.CurrentState is Opening. After that it turns to Playing but it doesn't play. And then it do not change anymore and remains Playing.

Any Ideas?

3

There are 3 best solutions below

8
On

To get to the bottom of this you need to check MediaElement.CurrentState - it will tell you at which step of the interaction with the MediaStreamSource the MediaElement is stuck. This in turn will tell which of your MediaStreamSource methods should be impemented differently...

For a comprehensive walkthrough including essential information on the buffering part see http://msdn.microsoft.com/en-us/library/hh180779%28v=vs.95%29.aspx

1
On

Some things to try...

Try setting CanSeek to "0" and try a duration greater than zero, any hard coded value is fine to at least try to get it working. Also double check your CodecPrivateData string and make sure it's correct.

You also may want to try dropping in the Mp3MediaStreamSource from the ManagedMediaHelpers project and get that working first to make sure everything else in your app is set up properly then switch back to your custom MediaStreamSource.

1
On

When developing a mediaElement for Windows Phone (WP7.5 and WP8), for a reason that is completely beyond me, the debugger will not break on any breakpoints in the GetSampleAsync callback, the first time the callback is called !

The debugger will break the next time the breakpoint(s) will be reached. Try replacing your GetSampleAsync with this:

protected override void GetSampleAsync(MediaStreamType mediaStreamType)
    {
        System.Diagnostics.Debug.WriteLine("Yay!");

        MediaStreamSample msSamp = new MediaStreamSample(
          _videoDesc, _frameStream, _frameStreamOffset,
          _frameBufferSize, _currentTime, _emptySampleDict);

        ReportGetSampleCompleted(msSamp);
    }