4

I am using the AxWindowsMediaPlayer control in a Winforms C# application and I would like to set the player to pause on the last frame when the video is finished playing. I already handle the fact that the video is finished to display another control with a different content over the player, but I want to avoid the blank frame that shows up in between.

Is it possible to configure the player to pause on the last frame? Or is there an event I could handle to set the video to pause on the last last frame or the last few milliseconds?

PS. The PositionChange event seems to be raised only when the position is set by the user or in code so I can't use it. The MarkerHit event seemed promising, but I can't find any way to set a marker in the video so that didn't work either.

Kevin Coulombe
  • 1,517
  • 1
  • 17
  • 35

3 Answers3

1

There is a special event in MediaPlayer, where you can follow the playing status cahanges. This event is PlayStateChange. Just subscripe to thes event and the you can do the following:

    private void MediaPlayer_PlayStateChange( object sender, _WMPOCXEvents_PlayStateChangeEvent e )
    {
        switch( e.newState )
        {
        case 0:    // Undefined
            break;
        case 1:    // Stopped
            // keep last frame
            View.MediaPlayer.Ctlcontrols.currentPosition = View.MediaPlayer.currentMedia.duration - .001;
            View.MediaPlayer.Ctlcontrols.play();
            View.MediaPlayer.Ctlcontrols.pause();
            break;
        case 2:    // Paused
            break;
        case 3:    // Playing
            break;
        case 4:    // ScanForward
            break;
        case 5:    // ScanReverse
            break;
        case 6:    // Buffering
            break;
        case 7:    // Waiting
            break;
        case 8:    // MediaEnded
            break;
        case 9:    // Transitioning
            break;
        case 10:   // Ready
            break;
        case 11:   // Reconnecting
            break;
        case 12:   // Last
            break;
        }
    }
György Gulyás
  • 1,290
  • 11
  • 37
0

It's not quite as simple as Nadhmi (@user5330265) makes out. The video may finish playing and reset to a blank frame between the timer ticks, in which case the pause is never executed.

I've found the following works, at the cost of missing the last iInterval msec of the video. tmrVideo timer interval is set to 1 msec. On my system it seems to run about every 15 msec, but diagnostics slow it down. It doesn't seem to add to the overall CPU load. I use a value of 250 for iInterval - currentPosition never seems to reach duration. Smaller values mean less lost video, but a greater chance of the blank frame appearing.

currentPosition is zero while the video is loading, which is why the test needs to be added to the condition.

tmrVideo need to be enabled after every axWindowsMediaPlayer1.Ctlcontrols.play() command.

private void tmrVideo_Tick(object sender, EventArgs e)
    {
        if ((axWindowsMediaPlayer1.currentMedia.duration - axWindowsMediaPlayer1.Ctlcontrols.currentPosition)*1000 < 
            iInterval) && 
            axWindowsMediaPlayer1.Ctlcontrols.currentPosition!= 0)
        {
            axWindowsMediaPlayer1.Ctlcontrols.pause();
            tmrVideo.Enabled = false;
        }                           
    }
SimonKravis
  • 553
  • 1
  • 3
  • 24
  • Just mentioning... The same solution might work with a smaller value of interval with a MultimediaTimer (at the cost of some CPU usage) : https://stackoverflow.com/a/24843946/410075 – Kevin Coulombe Jan 14 '21 at 16:57
-1

Hi

Its as simple as this.

First, you need to add a timer. Now handle the Timer1_Tick event:

if (AxWindowsMediaPlayer1.Ctlcontrols.currentPosition == AxWindowsMediaPlayer1.currentMedia.duration) {
    AxWindowsMediaPlayer1.Ctlcontrols.pause();
}

That's it

PS: make sure to start the timer.

Nadhmi.