原文:与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能

[源码下载]

与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能

作者:webabcd

介绍
与众不同 windows phone 8.0 之 媒体

  • 添加音乐到音乐中心,从音乐中心删除音乐
  • 与图片中心相关的新增功能
  • BackgroundAudioPlayer 的新增功能

示例
1、演示如何添加音乐到音乐中心,以及如何从音乐中心删除音乐
MusicMediaLibrary/MusicMediaLibrary.xaml

<phone:PhoneApplicationPagex: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}"FontSize="{StaticResource PhoneFontSizeNormal}"Foreground="{StaticResource PhoneForegroundBrush}"SupportedOrientations="Portrait" Orientation="Portrait"mc:Ignorable="d"shell:SystemTray.IsVisible="True"><Grid Background="Transparent"><StackPanel Orientation="Vertical"><Button x:Name="btnAdd" Content="添加音乐到音乐中心" Click="btnAdd_Click" /><Button x:Name="btnDelete" Content="从音乐中心删除音乐" Click="btnDelete_Click" /></StackPanel></Grid></phone:PhoneApplicationPage>

MusicMediaLibrary/MusicMediaLibrary.xaml.cs

/** 演示如何添加音乐到音乐中心,以及如何从音乐中心删除音乐* * * 在 wp8 中新增了 Microsoft.Xna.Framework.Media.PhoneExtensions.MediaLibraryExtensions,其扩展了 MediaLibrary 类*     MediaLibrary.SaveSong(Uri filename, SongMetadata songMetadata, SaveSongOperation operation) - 保存音乐到音乐中心,返回 Song 对象*         Uri filename - 需要添加到音乐中心的音乐文件,必须在 IsolatedStorage 下*         SongMetadata songMetadata - 元数据*         SaveSongOperation operation - CopyToLibrary 拷贝音乐文件;MoveToLibrary 移动音乐文件*     MediaLibrary.Delete(Song song) - 根据 Song 对象从音乐中心删除数据* * * 注:* 1、需要在 manifest 中增加配置 <Capability Name="ID_CAP_MEDIALIB_AUDIO" />* 2、音乐文件只支持mp3和wma,且必须在 IsolatedStorage 下* 3、音乐的封面文件只支持jpg,且必须在 IsolatedStorage 下* * * 另:* 1、播放音乐或视频的话,需要在 manifest 中增加配置 <Capability Name="ID_CAP_MEDIALIB_PLAYBACK" />* 2、除了用 MediaElement 播放音乐外,还可以用 MediaPlayer(xna) 播放,参见: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 _random = new Random();public MusicMediaLibrary(){InitializeComponent();}private async void btnAdd_Click(object sender, RoutedEventArgs e){// 将相关文件复制到 ApplicationData 的 Local 目录下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");// 将相关文件复制到 IsolatedStorage 的根目录下// CopyFileToIsolatedStorage(new Uri("Assets/Demo.mp3", UriKind.Relative), "Demo.mp3");// CopyFileToIsolatedStorage(new Uri("Assets/Son.jpg", UriKind.Relative), "Son.jpg");// 需要添加到音乐中心的音乐文件仅支持mp3和wma,且必须在 ApplicationData 下,以下格式会先在 Local 目录下找,找不到再到 Local/IsolatedStorage 目录下找Uri musicUri = new Uri("Demo.mp3", UriKind.Relative);// 需要添加到音乐中心的音乐封面文件仅支持jpg,且必须在 ApplicationData 下,以下格式会先在 Local 目录下找,找不到再到 Local/IsolatedStorage 目录下找Uri picUri = new Uri("Son.jpg", UriKind.Relative);// 构造 SongMetadata 对象// 如果按以下内容设置 SongMetadata 对象,则音乐文件在音乐中心的保存路径为:webabcd/webabcd album/music xxxx.mp3SongMetadata 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;MediaLibrary library = new MediaLibrary();try{// 添加音乐文件到音乐中心Song song = library.SaveSong(musicUri, sm, SaveSongOperation.CopyToLibrary);}catch (Exception ex){// 如果文件已存在,则会抛出 System.InvalidOperationException 异常
                MessageBox.Show(ex.Message);}}private void btnDelete_Click(object sender, RoutedEventArgs e){/** MediaLibrary - 媒体库*     Songs - 返回音乐中心的 SongCollection*     Albums - 返回音乐中心的 AlbumCollection*     Artists - 返回音乐中心的 ArtistCollection*     Genres - 返回音乐中心的 GenreCollection*     Playlists - 返回音乐中心的 Playlists*/MediaLibrary library = new MediaLibrary();// 通过 MediaLibrary 遍历音乐中心中的音乐文件foreach (Song song in library.Songs){// 从音乐中心删除音乐文件if (song.Artist.Name == "webabcd")library.Delete(song);}}// 将文件从 Package 复制到 IsolatedStorage 的根目录下private void CopyFileToIsolatedStorage(Uri sourceUri, string targetFileName){IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();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);}}}}// 将文件从 Package 复制到 ApplicationData 的 Local 目录下private async Task CopyFileToApplicationData(Uri sourceUri, string targetFileName){StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;StorageFile sourceFile = await StorageFile.GetFileFromApplicationUriAsync(sourceUri);await sourceFile.CopyAsync(applicationFolder, targetFileName, NameCollisionOption.ReplaceExisting);}}
}

2、演示与图片中心相关的新增功能
MusicMediaLibrary/PictureMediaLibrary.xaml

<phone:PhoneApplicationPagex:Class="Demo.Media.PictureMediaLibrary"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}"FontSize="{StaticResource PhoneFontSizeNormal}"Foreground="{StaticResource PhoneForegroundBrush}"SupportedOrientations="Portrait" Orientation="Portrait"mc:Ignorable="d"shell:SystemTray.IsVisible="True"><Grid Background="Transparent"><StackPanel Orientation="Vertical"><Image Name="img" Width="50" Height="50" /><Image Name="img2" Width="50" Height="50" Margin="0 10 0 0" /><Image Name="img3" Width="50" Height="50" Margin="0 10 0 0" /><Button x:Name="btnGet" Content="获取图片中心的图片的小缩略图和预览图" Click="btnGet_Click" /><Button x:Name="btnShare" Content="共享图片中心的指定的图片" Click="btnShare_Click"  /></StackPanel></Grid></phone:PhoneApplicationPage>

MusicMediaLibrary/PictureMediaLibrary.xaml.cs

/** 演示与图片中心相关的新增功能* * * 在 wp8 中新增了 Microsoft.Xna.Framework.Media.PhoneExtensions.MediaLibraryExtensions,其扩展了 MediaLibrary 类和 Picture 类*     Picture.GetPreviewImage() - 获取预览图(介于缩略图与原图之间)*     Picture.GetPath() - 获取文件路径,可以用于“共享”之类的场景,本例会介绍*     MediaLibrary.GetPathFromToken(fileToken) - 根据 token 获取媒体库文件的路径* * * 注:* 1、需要在 manifest 中增加配置 <Capability Name="ID_CAP_MEDIALIB_PHOTO" />* 2、在 wp8 中,保存在手机上的每个图片,系统都将为其自动创建两种缩略图:小缩略图和预览图*/using System.Linq;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Media.PhoneExtensions;
using Microsoft.Phone;
using Microsoft.Phone.Tasks;namespace Demo.Media
{public partial class PictureMediaLibrary : PhoneApplicationPage{public PictureMediaLibrary(){InitializeComponent();}private void btnGet_Click(object sender, RoutedEventArgs e){/** MediaLibrary - 媒体库*     Pictures - 返回图片中心的全部图片*     SavedPictures - 返回图片中心的已保存图片*     RootPictureAlbum - 返回图片中心的所有根相册*/MediaLibrary library = new MediaLibrary();if (library.Pictures.Count > 0){// 获取图片中心的第一张图片Picture picture = library.Pictures.First();img.Source = PictureDecoder.DecodeJpeg(picture.GetImage()); // 获取原图img2.Source = PictureDecoder.DecodeJpeg(picture.GetThumbnail()); // 获取缩略图img3.Source = PictureDecoder.DecodeJpeg(picture.GetPreviewImage()); // 获取预览图
            }}private void btnShare_Click(object sender, RoutedEventArgs e){MediaLibrary library = new MediaLibrary();if (library.Pictures.Count > 0){Picture picture = library.Pictures.First();// 获取媒体文件路径string picturePath = picture.GetPath();// 由文件启动 app 时会传递过来文件的 token 值,用此方法可以根据 token 获取媒体库文件的路径// string picturePath = library.GetPathFromToken(fileToken);// 根据媒体文件路径共享之ShareMediaTask shareMediaTask = new ShareMediaTask();shareMediaTask.FilePath = picturePath;shareMediaTask.Show();}}}
}

3、演示后台音频播放的新增功能
MusicMediaLibrary/BackgroundAudio.xaml

<phone:PhoneApplicationPagex:Class="Demo.Media.BackgroundAudio"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}"FontSize="{StaticResource PhoneFontSizeNormal}"Foreground="{StaticResource PhoneForegroundBrush}"SupportedOrientations="Portrait" Orientation="Portrait"mc:Ignorable="d"shell:SystemTray.IsVisible="True"><Grid Background="Transparent"><StackPanel Orientation="Vertical"><TextBlock x:Name="lblMsg" TextWrapping="Wrap" Text="BackgroundAudioPlayer 的 PlayStateChanged 事件有了事件参数 PlayStateChangedEventArgs" /></StackPanel></Grid></phone:PhoneApplicationPage>

MusicMediaLibrary/BackgroundAudio.xaml.cs

/** 演示后台音频播放的新增功能* * * 注:* 关于后台音频播放参见:http://www.cnblogs.com/webabcd/archive/2012/07/23/2604457.html*/using System;
using Microsoft.Phone.Controls;
using Microsoft.Phone.BackgroundAudio;namespace Demo.Media
{public partial class BackgroundAudio : PhoneApplicationPage{public BackgroundAudio(){InitializeComponent();// 播放状态发生改变时BackgroundAudioPlayer.Instance.PlayStateChanged += Instance_PlayStateChanged;}void Instance_PlayStateChanged(object sender, EventArgs e){/** 以下是新增功能*/// 将事件参数参数对象转换为 PlayStateChangedEventArgsPlayStateChangedEventArgs newEventArgs = (PlayStateChangedEventArgs)e;// BackgroundAudioPlayer 的当前的 PlayerStatePlayState currentPlayState = newEventArgs.CurrentPlayState;// BackgroundAudioPlayer 在进入当前 PlayerState 时的前一个 PlayerState(如果没有此中间状态则 IntermediatePlayState 等于 CurrentPlayState)PlayState intermediatePlayState = newEventArgs.IntermediatePlayState;  }}
}

OK
[源码下载]

与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能...相关推荐

  1. 与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成...

    原文:与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成 [索引页] [源码下载] 与众不同 wi ...

  2. 与众不同 windows phone (15) - Media(媒体)之后台播放音频

    原文:与众不同 windows phone (15) - Media(媒体)之后台播放音频 [索引页] [源码下载] 与众不同 windows phone (15) - Media(媒体)之后台播放音 ...

  3. 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile

    原文:与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile [源码下载] 与众不同 windows phone (36 ...

  4. Windows SharePoint Services 3.0编码开发工具和技巧(Part 1 of 2)

    转:http://blog.csdn.net/mattwin/article/details/2074984 WSSv3 Technical Articles_Windows SharePoint S ...

  5. IOS 后台运行 播放音乐

    iOS 4开始引入的multitask,我们可以实现像ipod程序那样在后台播放音频了.如果音频操作是用苹果官方的AVFoundation.framework实现,像用AvAudioPlayer,Av ...

  6. IOS 后台执行 播放音乐

    iOS 4開始引入的multitask.我们能够实现像ipod程序那样在后台播放音频了. 假设音频操作是用苹果官方的AVFoundation.framework实现.像用AvAudioPlayer.A ...

  7. 与众不同 windows phone (23) - Device(设备)之硬件状态, 系统状态, 网络状态

    原文:与众不同 windows phone (23) - Device(设备)之硬件状态, 系统状态, 网络状态 [索引页] [源码下载] 与众不同 windows phone (23) - Devi ...

  8. 玩转Windows下40款开源软件 (转)

    玩转Windows下40款开源软件   <script src="http://blog.csdn.net/count.aspx?ID=1789004&Type=Rank&qu ...

  9. SAP 远程连接出错 SAP GUI For Windows 7.40 hostname ‘NiHLGetNodeAddr‘ unknown

    SAP 远程连接出错 SAP GUI For Windows 7.40 hostname 'NiHLGetNodeAddr' unknown 最近重装了下SAP GUI 用的是740,今天有个系统需要 ...

最新文章

  1. Go 语言编程 — 内存分配
  2. java Datasource,数据库连接池
  3. JS 设计模式之初识(一)-单例模式
  4. NLP学习笔记:word2vec
  5. springboot学习笔记(五)
  6. vscode设置键盘快捷键
  7. PHP的各种参数设置ini_set:内存 错误等级 session
  8. Linux Epoll ET模式EPOLLOUT和EPOLLIN触发时刻
  9. 中key的用途_Python中的函数定义与参数使用
  10. EntityFramework 如何查看执行的 SQL 代码?
  11. Rust 2018临近:设法从Rust 2015过渡
  12. c语言图灵机算法,图灵机的时间化简概述(3/4)
  13. 有个码龄 10 年的程序员跟我说:“他编程从来不用鼠标”,我说:
  14. HTTP基础与DNS分析
  15. 俩 AI 约会遭全网围观:再厉害的算法,也逃不了翻车
  16. ME:环境DNA(eDNA)宏条形码技术正在转变我们考察动植物群落的方法
  17. 自动驾驶仿真软件列表
  18. 什么是电商API接口?那如何调取呢?
  19. OSChina 周五乱弹 ——妹子喜欢我的幻觉封印怎么破除
  20. Java中计算1--n的累加和

热门文章

  1. java中四种线程池及poolSize、corePoolSize、maximumPoolSize
  2. 为什么不能根据返回类型来区分重载
  3. Python学习 Day 025 -模块相关
  4. WPF 获取屏幕分辨率(获取最大宽高)等
  5. OC第六节 遍历集合、数组排序
  6. 数据库自动备份还原成新库脚本
  7. linux学习(7)-压缩与解压缩
  8. ASP.NET十分有用的页面间传值方法(转)
  9. vscode Python 运行环境配置
  10. go语言学习(基本数据类型)