WIN10 Series: VC + + Media playback

Source: Internet
Author: User

Media playback includes video playback and audio playback, and you can use the MediaElement control to play video files and audio files during the development of a Windows Store app. This section provides a concrete example of how to use the MediaElement control to play a video file, and how to control the playback of the video in the MediaElement control.

20.6.1 Video Loading

First look at how to read a video file and load it into the MediaElement control, create a blank application project for the Visual C + + Windows Store in Visual Staudio 2012, and name it Filedemo. Next open the MainPage.xaml file and add the following code to the grid element for the layout foreground interface.

<StackPanel>

<button x:name= "Filebutton" content= " Open File "fontsize=" "margin=" 114,30,0,0 "click=" Filebuttonclick "width=" 113 "height=" ></Button>

<mediaelement x:name= "Video" horizontalalignment= "left" height= "All" margin= "114,10,0,0" verticalalignment= "Top" Width= "780" autoplay= "False"/>

</StackPanel>

In the code above, add a MediaElement control and an open File button. Name the MediaElement control video, and use this control to play back the loaded movie file. The Open File button lets you select a video file by opening the picker with a file.

After the foreground interface is laid out, open the MainPage.xaml.cpp source file and refer to the following header file and namespace:

#include "Ppltasks.h"

using namespace Windows::storage;

using namespace Windows::storage::streams;

Using namespace Windows::storage::P ickers;

using namespace Concurrency;

using namespace Windows::system;

In the preceding code, use the Include keyword to refer to a Ppltasks.h header file, and then use the using directive to reference the namespace Windows::storage, Windows::storage::streams, Windows:: Storage::P ickers, concurrency, and windows::system, the classes that are defined within these header files and namespaces are used in the process of reading the video files later.

After referencing the above header files and namespaces, next add the Click event handler Filebuttonclick for the Open File button. Open the Mainpage.xaml.h header file and add the following code to declare the Filebuttonclick function and the member variable LocalFile.

Private

???? loading video files

???? void Filebuttonclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e);

Private

???? declaring member variables localfile

???? windows::storage::storagefile^ LocalFile;

After declaring the Filebuttonclick function, add the implementation code for the Filebuttonclick function in the MainPage.xaml.cpp source file, as shown in the following code:

// Loading video files

void Filedemo::mainpage::filebuttonclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e)

{

???? New File selector

???? fileopenpicker^ openpicker =ref New Fileopenpicker ();

???? set view as thumbnail

???? Openpicker->viewmode = Pickerviewmode::thumbnail;

???? Set the initial location for access to the video library

???? Openpicker->suggestedstartlocation = pickerlocationid::videoslibrary;

???? sets the type of file that can be selected, the type of file allowed to play

???? Openpicker->filetypefilter->append (". mp4");

???? Openpicker->filetypefilter->append (". avi");

???? Show file selector, select File

???? Create_task (Openpicker->picksinglefileasync ()). Then ([This] (storagefile^ file)

???? {

???????? Determine if a file has been selected

???????? if (file)

???????? {

???????????? LocalFile = file;

???????????? writes a file to the irandomaccessstream Stream

???? Task<irandomaccessstream^> (File->openasync (Fileaccessmode::read)). Then (

???????????????? [This] (irandomaccessstream^ Stream)

???????????? {

???????????????? if (stream)

???????????????? {

???????????????????? reading video files to MediaElement Controls

???????????????????? Video->setsource (stream, Localfile->contenttype);

????????????????}

????????????});

????????}

????});

}

In the Filebuttonclick function, first create an object Openpicker of the Fileopenpicker class, The enumeration member thumbnail in the Pickerviewmode enumeration is assigned to the ViewMode property of the Openpicker object, and the view mode of the file-open picker is set to thumbnail mode. The enumeration member Videoslibrary in the Pickerlocationid enumeration is then assigned to the Suggestedstartlocation property of the Openpicker object, and the initial position of the set file Open Picker is the video library. You then use the Filetypefilter property of the Openpicker object to get the file type collection displayed by the file open picker, and call the Append function to file the type ". mp4" and ". avi"is added to this collection.

Next, call the Picksinglefileasync function of the Openpicker object to pick a single file and get an object file of type Storagefile. The Openasync function of the file object is then called to get an object stream of type Irandomaccessstream. When the stream object is not empty, call the SetSource function of the MediaElement control to load the video file into this control.

Run the project, click the "Open File" button, and select a video file in the File Open Picker interface, the foreground interface is shown in 20-8.

Figure 20-8 Loading video files

WIN10 Series: VC + + Media playback

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.