Media Element

There is a good MSDN MediaElement sample here. I had some problems with MediaElement control.

1. There is no media position has been changed message.

I solved this problem by creating my personal timer.

/// <summary>
/// If position changed we raise the event.
/// </summary>        
void timer_Tick(object sender, EventArgs e)
    if (this.mediaElement.Position != this.oldPosition && this.OnPositionChanged != null)

2. Media element HasVideo and HasAudio properties available only after video has been loaded.

Use this properties after media has been opened.

3. Video is not changing while position is changing.

Set ScrabbingEnabled property to true.

4. Media Element has a bug. It doens't play audio correctly ( ScrabbingEnbaled play audio bug )

Sometimes MediaElement played streams without sound. I coludn't understand why ? It was bug, I found answer here Sometimes MediaElement plays streams without sound. Bug ?

In WPF 3.0 and 3.5, there is a bug that prevents playing audio-only streams when scrubbing is enabled. The work-around is to disable scrubbing for audio-only streams. This property should not have any effect on audio-only streams anyways, since scrubbing is about video content. Please try this out and let us know if it works for you.

So if ScrabbingEnabled property is true we have audio bug and if ScrabbingEnabled property is false we haven't audio bug. That's why I determinate is it audio, and if it is I change ScrabbingEnabled property.

private void mediaElement_MediaOpened(object sender, RoutedEventArgs e)
    //Hide window if it is a music

    if (!this.mediaElement.HasVideo && this.mediaElement.HasAudio)
    else if (this.mediaElement.HasVideo && !this.IsVisible)
        //this.mediaElement.ScrubbingEnabled = true;

    if (this.mediaElement.HasVideo)
        this.mediaElement.ScrubbingEnabled = true;

    //Set played media duration.        

    if (this.OnMediaOpened != null)

Last edited Sep 18, 2010 at 2:38 PM by Denis_Yakimenko, version 26


No comments yet.