How to Use mediaelement to play local media files (using C #/Vb/C ++ and XAML windows App Store applications)

Source: Internet
Author: User
ArticleDirectory
    • Description
    • Complete example
    • Related Topics
Important APIs
    • Mediaelement
    • Fileopenpicker
    • Openasync
Step 1:

In Windows App Store applications that use C ++, C #, or Visual BasicMediaelement Class for audio and video playback.SourceAttribute specifies the media file to be played. It can be files and applications on the network.ProgramAttached files or files on the local system. For files on the network or files embedded in the application, you only needSourceSet the property to the file path. To open files on the local system, you can useFileopenpicker.

This topic describes how to useFileopenpickerClass to open and play a local media file.

Step 2: Set the Function

To enable access to the media repository on the local system, the application must includeMusic Library AccessFunction.

    1. In Microsoft Visual Studio express 2012 for Windows 8, double-clickPackage. appxmanifestTo open the Application List designer.
    2. Click functions ".
    3. Select "video library access" or "music library access.
Step 3: Create a mediaelement

CreateMediaelementObject and provide itName. After providing a name for the object, you can easilyCodeFile.

 
<MediaelementName= "Mediacontrol"Height= "400" />

 

Step 4: Use openfilepicker to obtain files

UseFileopenpickerClass: select a media file from the user's video library. InFileopenpickerAboveSuggestedstartlocationAndFiletypefilterAttribute. CallPicksinglefileasyncYou can start the file selector and obtain the file.

 1   VaR Openpicker = New  Windows. Storage. pickers. fileopenpicker ();  2 Openpicker. suggestedstartlocation = 3   Windows. Storage. pickers. pickerlocationid. videoslibrary;  4 Openpicker. filetypefilter. Add ( "  . Wmv "  );  5 Openpicker. filetypefilter. Add ( "  . MP4  "  );  6   VaR File = Await Openpicker. picksinglefileasync ();

 

Step 5: Set the source and play the media

To Mediaelement OfSourceSet to slaveFileopenpickerReturnedStoragefile, We need to open the stream.StoragefileOnOpenasyncMethod return can be passed inMediaelement. setsource. Then callMediaelementOnPlayTo start the media.

 
1 VaRStream =AwaitFile. openasyn (Windows. Storage. fileaccessmode. Read );2 //Mediacontrol is a mediaelement defined in XAML3 Mediacontrol. setsource (stream, file. contenttype );4Mediacontrol. Play ();

 

Complete example

The following example shows how to select a file from the user's video library and set itMediaelement OfSourceComplete code list.

 1   Async   Private   Void  Setlocalmedia ()  2   {  3   VaR Openpicker = New  Windows. Storage. pickers. fileopenpicker ();  4 Openpicker. suggestedstartlocation = 5   Windows. Storage. pickers. pickerlocationid. videoslibrary;  6 Openpicker. filetypefilter. Add ( "  . Wmv  "  );  7 Openpicker. filetypefilter. Add ( "  . MP4  "  );  8   VaR File =Await  Openpicker. picksinglefileasync ();  9   VaR Stream = Await  File. openasync (Windows. Storage. fileaccessmode. Read );  10   //  Mediacontrol is a mediaelement defined in XAML  11   Mediacontrol. setsource (stream, file. contenttype );  12   Mediacontrol. Play ();  13 }
Related Topics
Create a roadmap for Windows App Store applications using C #, C ++, or VB
XAML media playback example
Windows. Media
Mediaelement

 

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.