Binding Slider to MediaPlayer's properties

Topics: Conventions
Sep 7, 2012 at 1:12 PM

Dear experts,

 

I have a WPF application using CM. I have a slider in my view, and in the viewmodel I create a MediaPlayer object. I need to bind it to the slider to be able to see the audio progress and move back and forth as needed. 

I tried this in my View:

            <Slider  Value="{Binding  PlayerPosition, Mode=TwoWay}" MinWidth="200" VerticalAlignment="Center" Minimum="0" Maximum= "{Binding Player.NaturalDuration.TimeSpan.TotalMilliseconds}"/>

And in the ViewModel:

       public double PlayerPosition
       {
           get { return Player.Position.Milliseconds; }
           set
           {
               if (Math.Abs(Player.Position.Milliseconds - value) < Double.Epsilon)
                   return;
               Player.Position = TimeSpan.FromMilliseconds(value);
               NotifyOfPropertyChange(() => PlayerPosition);
           }
       }

But this doesn't work. The slider is not moving as the audio is playing, and when I move it with the mouse, the audio starts playing from the beginning. 

Could you please help me with this? 

Thanks.