8.0 Media: New features in the Music Center, new features in the image Center, new features in background music playback
Introduced
Different Windows Phone 8.0 media
Add music to Music Center, remove music from Music Center
New features related to the photo center
New features for Backgroundaudioplayer
Example
1. Show how to add music to the Music Center and how to remove music from the Music Center
Musicmedialibrary/musicmedialibrary.xaml
<phone:phoneapplicationpage x:class= "Demo.Media.MusicMediaLibrary" xmlns= "http://schemas.microsoft.com/winfx/ 2006/xaml/presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:phone=" Clr-namespace: Microsoft.phone.controls;assembly=microsoft.phone "Xmlns:shell=" clr-namespace:microsoft.phone.shell;assembly= Microsoft.phone "xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 "xmlns:mc=" http:// schemas.openxmlformats.org/markup-compatibility/2006 "fontfamily=" {StaticResource PhoneFontFamilyNormal} "FontSiz E= ' {StaticResource phonefontsizenormal} ' foreground= ' {StaticResource Phoneforegroundbrush} ' SupportedOrientations= "Portrait" orientation= "Portrait" mc:ignorable= "D" shell:systemtray.isvisible= "True" > <grid Backgro und= "Transparent" > <stackpanel orientation= "Vertical" > <button x:name= "Btnadd" Conten T= "Add music to Music Center" click= "btnAdd_Click"/> <Button X:name= "Btndelete" content= "Remove music from Music Center" click= "Btndelete_click"/> </StackPanel> </grid > </phone:PhoneApplicationPage>
Musicmedialibrary/musicmedialibrary.xaml.cs
</p>
* * Demonstrates how to add music to the Music Center and how to remove music from the Music Center * * * NEW Microsoft.Xna.Framework.Media.PhoneExtensions.MediaLibraryExtensi in WP8 ONS, which extends the Medialibrary class * Medialibrary.savesong (Uri filename, songmetadata songmetadata, savesongoperation operation) -Save music to Music Center, return song Object * Uri filename-The music file that needs to be added to the Music Center must be under IsolatedStorage * Songmetadata Songmetadat A-metadata * savesongoperation operation-copytolibrary copy music file; movetolibrary Mobile Music file * Medialibrary.delete (song song)-delete data from Music Center according to song Object * * * NOTE: * 1, need to increase the configuration in the manifest <capability name= "Id_cap_medialib_audio"/> * 2, The music file only supports MP3 and WMA, and must be under IsolatedStorage * 3, the music cover file only supports JPG, and must be under isolatedstorage * * * * * * 1, play music or video, you need to be in manifest Add configuration <capability name= "Id_cap_medialib_playback"/> * 2, in addition to playing music with MediaElement, can also play with MediaPlayer (XNA), see: http
://www.cnblogs.com/webabcd/archive/2011/07/11/2102713.html * * using System;
Using System.Collections.Generic;
Using System.Windows; Using MicroSoft.
Phone.controls;
Using Microsoft.Xna.Framework.Media;
Using Microsoft.Xna.Framework.Media.PhoneExtensions;
Using System.IO.IsolatedStorage;
Using System.IO;
Using Windows.Storage.Streams;
Using Windows.storage;
Using System.Threading.Tasks; namespace Demo.media {public partial class Musicmedialibrary:phoneapplicationpage {private Random _ran
Dom = new Random ();
Public Musicmedialibrary () {InitializeComponent (); Private async void btnAdd_Click (object sender, RoutedEventArgs e) {//copy related files to Applica Tiondata The local directory under await Copyfiletoapplicationdata (new Uri ("Ms-appx:///assets/demo.mp3", Urikind.absolute),
"Demo.mp3");
Await Copyfiletoapplicationdata (new Uri ("Ms-appx:///assets/son.jpg", Urikind.absolute), "son.jpg"); Copy the related files to the root directory of IsolatedStorage//Copyfiletoisolatedstorage (New Uri ("Assets/demo.mp3", urikind.relative), " Demo.mp3 ");
Copyfiletoisolatedstorage (New Uri ("Assets/son.jpg", urikind.relative), "son.jpg"); Music files that need to be added to the Music Center only support MP3 and WMA, and must be under ApplicationData, the following format will be found in the local directory first, not found in the Local/isolatedstorage directory mus
Icuri = new Uri ("Demo.mp3", urikind.relative); The music cover file that needs to be added to the Music Center only supports JPG, and must be under ApplicationData, the following format will be found in the local directory first, can not find the Local/isolatedstorage directory to find the Uri picur
i = new Uri ("Son.jpg", urikind.relative);
Construct Songmetadata Object//If the Songmetadata object is set as follows, the path to the music file's save in the Music Center is: WEBABCD/WEBABCD album/music Xxxx.mp3
Songmetadata sm = new Songmetadata (); Sm.
Albumname = "WEBABCD album"; Sm.
Artistname = "WEBABCD"; Sm.
Genrename = "Rock"; Sm. Name = "Music" + _random. Next (1000, 10000).
ToString (); Sm.
Artistbackgrounduri = Picuri; Sm.
Albumarturi = Picuri; Sm.
Albumartistbackgrounduri = Picuri; MedialibraryLibrary = new Medialibrary (); try {//Add a music file to the Music Center song song = Library.
Savesong (Musicuri, SM, savesongoperation.copytolibrary);
catch (Exception ex) {//If the file already exists, a System.InvalidOperationException exception is thrown MessageBox.Show (ex.
message);
} private void Btndelete_click (object sender, RoutedEventArgs e) {/* * medialibrary-Media Library * songs-back to Music Center Songcollection * albums-back to Music Center Albumcollec
tion * Artists-return to the Music Center of artistcollection * genres-back to the Music Center Genrecollection
* Playlists-Return to the Music Center playlists/medialibrary Library = new Medialibrary (); Through the Medialibrary traverse the music file in the Music Center foreach (song Song in Library.)
Songs) {//delete music files from Music Center if (song. Artist.name = = "WEBABCD") library.
Delete (song); }///Copy files from Package to IsolatedStorage's root directory private void Copyfiletoisolatedstorage (Uri Sourceuri, String targetfilename) {IsolatedStorageFile ISF = Isolatedstoragefile.getuserstoreforappl
Ication (); using (Stream input = Application.getresourcestream (Sourceuri). Stream) {using (IsolatedStorageFileStream output = ISF).
CreateFile (TargetFileName)) {byte[] Readbuffer = new byte[4096];
int bytesread =-1; while (bytesread = input. Read (readbuffer, 0, readbuffer.length)) > 0) {output.
Write (readbuffer, 0, bytesread); }}//Copy files from Package to ApplicationData local directory private A Sync Task CopyFiletoapplicationdata (Uri Sourceuri, String targetfilename) {Storagefolder Applicationfolder = applic
AtionData.Current.LocalFolder;
Storagefile sourcefile = await storagefile.getfilefromapplicationuriasync (Sourceuri);
Await Sourcefile.copyasync (Applicationfolder, TargetFileName, namecollisionoption.replaceexisting); }
}
}