(3) Stop video playback
Next add stop control for video file playback, open the MainPage.xaml file, and add a Stop button to the grid element to stop the video from playing, as shown in the following code:
<button x:name= "Stopbutton" click= "Stopclick" horizontalalignment= "left" margin= "10,0,10,0 " Content= " Stop "grid.column=" 2 "></Button>
After adding the Stop button, next add the Click event handler Stopclick for this button. Open the Mainpage.xaml.h header file and add the following code to declare the Stopclick function.
Private
???? Stop button
???? void Stopclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e);
After declaring the Stopclick function, open the MainPage.xaml.cpp source file and add the implementation code for the Stopclick function, as shown in the following code:
// Stop button
void Filedemo::mainpage::stopclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e)
{
???? Stop the current video
???? Video->stop ();
???? Set Playback rate
???? video->defaultplaybackrate=1.0;
???? Show Playback status
???? videostate->text= " playback Status:" +video->currentstate.tostring ();
}
In the preceding code, call the Stop function of the MediaElement control to stop the playback of the current video, and then assign a value of 1.0 to the Defaultplaybackrate property of the MediaElement control to change the playback rate of the video to the normal playback rate. Finally, the playback state of the video is obtained using the CurrentState property in the MediaElement control, and the ToString function is called to convert it to a string type, which is displayed in a TextBlock control named "Videostate".
When the Stop button is added, the foreground interface appears as shown in 20-11, and the Stop button appears after the pause button.
Figure 20-11 Stop Playback
(4) Add speed forward and rewind button
Fast forward and rewind is a common feature of video playback, which controls the fast forward and rewind playback of a video by adding a "Fast forward" button and a "Rewind" button. First open the MainPage.xaml file and continue to add the following code based on the original code in the grid element:
<button x:name= "Forwardbutton" click= "Forwardclick" horizontalalignment= "left" margin= "10,0,10,0" Content= " Fast Forward "grid.column=" 3 "></Button>
<button x:name= "Backwardbutton" click= "Backwardclick" horizontalalignment= "left" margin= "10,0,10,0" Content= "Rewind " grid.column= "4" ></Button>
The code above adds two buttons, the Fast Forward button and the rewind button, respectively. Where the Fast Forward button is used for fast forward playback of the video, the rewind button is used to rewind the video.
After adding the Fast forward button and the rewind button, the next step is to add the Click event handler Forwardclick for the Fast Forward button. Open the Mainpage.xaml.h header file and add the following code to declare the Forwardclick function.
Private
???? Fast forward play video
???? void Forwardclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e);
After declaring the Forwardclick function, then open the MainPage.xaml.cpp source file and add the implementation code for the Forwardclick function, as shown in the following code:
// Fast forward play video
void Filedemo::mainpage::forwardclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e)
{
???? Set playback rate to 2.0
???? video->defaultplaybackrate=2.0;
???? Play Video
???? Video->play ();
???? Show Playback status
???? videostate->text= " playback Status:" +video->currentstate.tostring ();
}
In the Forwardclick function, the Defaultplaybackrate property of the MediaElement control is assigned a value of 2.0, which means that the video file in the MediaElement control is played at twice times the normal playback, enabling fast-forward functionality. Then call the MediaElement control's play function to play the video. You then use the CurrentState property in the MediaElement control to get the video's playback state, and call the ToString function to convert it to a string type that appears in a TextBlock control named "Videostate."
After you have added the implementation code for the Forwardclick function, next add the Click event handler Backwardclick to the rewind button. Add the following code to the Mainpage.xaml.h header file to declare the Backwardclick function.
Private
???? Rewind Play Video
???? void Backwardclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e);
After declaring the Backwardclick function, add the implementation code for the Backwardclick function in the MainPage.xaml.cpp source file, as shown in the following code:
// Rewind Play Video
void Filedemo::mainpage::backwardclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e)
{
???? Set playback rate to -2.0
???? video->defaultplaybackrate=-2.0;
???? Play Video
???? Video->play ();
???? Show Playback status
???? videostate->text= " playback Status:" +video->currentstate.tostring ();
}
In the Backwardclick function, assign a value of-2.0 to the MediaElement control's Defaultplaybackrate property to rewind the video at twice times the normal playback speed. Then call the MediaElement control's play function to play the video. You then use the CurrentState property in the MediaElement control to get the video's playback state, and call the ToString function to convert it to a string type that appears in a TextBlock control named "Videostate."
After adding the "Fast forward" button and the "Rewind" button, the foreground interface appears as shown in 20-12, and the Fast Forward button and the rewind button appear after the Stop button.
Figure 20-12 Rewind and fast Forward button
WIN10 Series: VC + + Media playback control 2