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.
- In Microsoft Visual Studio express 2012 for Windows 8, double-clickPackage. appxmanifestTo open the Application List designer.
- Click functions ".
- 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