與眾不同 windows phone (14) - Media(媒體)之音頻播放器, 視頻播放器, 與 Windows Phone 的音樂和視頻中心集成
作者:webabcd
介紹
與眾不同 windows phone 7.5 (sdk 7.1) 之媒體
- 音頻播放器
- 視頻播放器
- 與 Windows Phone 的音樂和視頻中心集成
示例
1、演示音頻播放器
Audio.xaml
<phone:PhoneApplicationPage x:Class="Demo.Media.Audio" 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" d:DesignHeight="768" d:DesignWidth="480" shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent"> <StackPanel Orientation="Vertical"> <MediaElement x:Name="mediaElement" Source="Assets/SuperMario.mp3" AutoPlay="False" /> <Button x:Name="btnPlay" Content="播放" Click="btnPlay_Click" /> <Button x:Name="btnPause" Content="暫停" Click="btnPause_Click" /> <TextBlock x:Name="lblStatus" /> </StackPanel> </Grid> </phone:PhoneApplicationPage>
Audio.xaml.cs
/* * MediaElement - 用於播放視頻或音頻,本地地址或遠程地址均可 * 支持的編碼格式參見:http://msdn.microsoft.com/en-us/library/ff462087(v=vs.92) * * MediaElement 的詳細說明參見:http://www.cnblogs.com/webabcd/archive/2008/12/01/1344632.html * Launcher 方式參見:http://www.cnblogs.com/webabcd/archive/2012/06/14/2548776.html 中的 MediaPlayerLauncher * * XNA 播放音頻參見:http://www.cnblogs.com/webabcd/archive/2011/07/11/2102713.html */ using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; namespace Demo.Media { public partial class Audio : PhoneApplicationPage { public Audio() { InitializeComponent(); mediaElement.CurrentStateChanged += new RoutedEventHandler(mediaElement_CurrentStateChanged); } void mediaElement_CurrentStateChanged(object sender, RoutedEventArgs e) { // 顯示 MediaElement 的當前狀態 lblStatus.Text = mediaElement.CurrentState.ToString(); } private void btnPlay_Click(object sender, RoutedEventArgs e) { // 播放 mediaElement.Play(); } private void btnPause_Click(object sender, RoutedEventArgs e) { // 暫停 mediaElement.Pause(); } } }
2、演示視頻播放器
Video.xaml
<phone:PhoneApplicationPage x:Class="Demo.Media.Video" 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" d:DesignHeight="768" d:DesignWidth="480" shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent"> <StackPanel Orientation="Vertical"> <MediaElement x:Name="mediaElement" Source="Assets/Demo.mp4" AutoPlay="False" /> <Button x:Name="btnPlay" Content="播放" Click="btnPlay_Click" /> <Button x:Name="btnPause" Content="暫停" Click="btnPause_Click" /> <TextBlock x:Name="lblStatus" /> </StackPanel> </Grid> </phone:PhoneApplicationPage>
Video.xaml.cs
/* * MediaElement - 用於播放視頻或音頻,本地地址或遠程地址均可 * 支持的編碼格式參見:http://msdn.microsoft.com/en-us/library/ff462087(v=vs.92) * * MediaElement 的詳細說明參見:http://www.cnblogs.com/webabcd/archive/2008/12/01/1344632.html * Launcher 方式參見:http://www.cnblogs.com/webabcd/archive/2012/06/14/2548776.html 中的 MediaPlayerLauncher */ using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; namespace Demo.Media { public partial class Video : PhoneApplicationPage { public Video() { InitializeComponent(); mediaElement.CurrentStateChanged += new RoutedEventHandler(mediaElement_CurrentStateChanged); } void mediaElement_CurrentStateChanged(object sender, RoutedEventArgs e) { // 顯示 MediaElement 的當前狀態 lblStatus.Text = mediaElement.CurrentState.ToString(); } private void btnPlay_Click(object sender, RoutedEventArgs e) { // 播放 mediaElement.Play(); } private void btnPause_Click(object sender, RoutedEventArgs e) { // 暫停 mediaElement.Pause(); } } }
3、演示如何與 Windows Phone 的音樂和視頻中心集成
IntegrateWithTheMusicAndVideoHub.xaml
<phone:PhoneApplicationPage x:Class="Demo.Media.IntegrateWithTheMusicAndVideoHub" 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" d:DesignHeight="768" d:DesignWidth="480" shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent"> <StackPanel Orientation="Vertical"> <TextBlock TextWrapping="Wrap"> <Run>本 app 會出現在“音樂視頻中心”中的“應用程序”下</Run> <LineBreak /> <Run>操作完成后,請去“音樂視頻中心”看效果</Run> </TextBlock> <Button x:Name="btnNow" Content="設置“音樂視頻中心”中的“正在播放磁貼”" Click="btnNow_Click" /> <Button x:Name="btnRecent" Content="向“音樂視頻中心”中的“歷史記錄”添加新的磁貼" Click="btnRecent_Click" /> <Button x:Name="btnAcquired" Content="向“音樂視頻中心”中的“最新上市”添加新的磁貼" Click="btnAcquired_Click" /> </StackPanel> </Grid> </phone:PhoneApplicationPage>
IntegrateWithTheMusicAndVideoHub.xaml.cs
/* * 本例演示如何將 app 集成進“音樂視頻中心” * * MediaHistoryItem - 出現在“音樂視頻中心”中的磁貼對象(包括“正在播放”,“歷史記錄”和“最新上市”) * ImageStream - 磁貼上需要顯示的背景圖片流 * Title - 磁貼的標題 * PlayerContext - key/value 對集合,用戶點擊“歷史記錄”或“最新上市”中的某個磁貼會進入到 app 的主頁面,同時也會將對應的 key/value 數據一同帶過去,參見 MainPage.xaml.cs * * MediaHistory - 管理 MediaHistoryItem 的類 * Instance - 獲得 MediaHistory 實例 * NowPlaying - 指定“正在播放”對象,MediaHistoryItem 類型 * WriteRecentPlay(MediaHistoryItem item) - 在“歷史記錄”中增加一個指定的 MediaHistoryItem 對象 * WriteAcquiredItem(MediaHistoryItem item) - 在“最新上市”中增加一個指定的 MediaHistoryItem 對象 * * * 注意: * 1、“正在播放”磁貼大小為 358 * 358,背景圖不能大於 75 KB * 2、“歷史記錄”和“最新上市”磁貼大小為 173 * 173 * 3、app 提交到商店審核時,如果認證程序檢測到 app 調用了 MediaHistory 和 MediaHistoryItem,則此 app 就會出現在“音樂視頻中心”的應用程序中 * 4、出於測試目的,如果想在 app 提交商店前使其出現在“音樂視頻中心”的應用程序中的話,需要修改 manifest,在 <App /> 中增加 HubType="1" * 5、MediaHistoryItem 的 PlayerContext 指定的 key/value 對集合是 MediaHistoryItem 的上下文數據,用戶在“歷史記錄”或“最新上市”單擊某個磁貼對象時,會跳轉到 app 的主頁面,同時將對應的 key/value 數據一同帶過去,其可以在主頁面通過 NavigationContext.QueryString[key] 獲取到,參見 MainPage.xaml.cs */ using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using Microsoft.Devices; using System.IO; using System.Windows.Resources; using System.IO.IsolatedStorage; using Microsoft.Phone.BackgroundAudio; namespace Demo.Media { public partial class IntegrateWithTheMusicAndVideoHub : PhoneApplicationPage { public IntegrateWithTheMusicAndVideoHub() { InitializeComponent(); PlayAudio(); } // 播放一個音頻 private void PlayAudio() { // 由於播放本地音頻時只能從獨立存儲中播放,所以此處把示例用音頻文件從程序包中復制到獨立存儲 using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) { if (!storage.FileExists("SuperMario.mp3")) { StreamResourceInfo resource = Application.GetResourceStream(new Uri("Assets/SuperMario.mp3", UriKind.Relative)); using (IsolatedStorageFileStream file = storage.CreateFile("SuperMario.mp3")) { int chunkSize = 4096; byte[] bytes = new byte[chunkSize]; int byteCount; while ((byteCount = resource.Stream.Read(bytes, 0, chunkSize)) > 0) { file.Write(bytes, 0, byteCount); } } } } BackgroundAudioPlayer.Instance.Play(); } private void btnNow_Click(object sender, RoutedEventArgs e) { StreamResourceInfo sri = Application.GetResourceStream(new Uri("Assets/TileBackgroundRed.png", UriKind.Relative)); MediaHistoryItem mediaHistoryItem = new MediaHistoryItem(); mediaHistoryItem.ImageStream = sri.Stream; mediaHistoryItem.Title = "正在播放"; mediaHistoryItem.PlayerContext.Add("keyNow", "正在播放的音樂"); MediaHistory.Instance.NowPlaying = mediaHistoryItem; } private void btnRecent_Click(object sender, RoutedEventArgs e) { StreamResourceInfo sri = Application.GetResourceStream(new Uri("Assets/TileBackgroundGreen.png", UriKind.Relative)); MediaHistoryItem mediaHistoryItem = new MediaHistoryItem(); mediaHistoryItem.ImageStream = sri.Stream; mediaHistoryItem.Title = "最近播放"; mediaHistoryItem.PlayerContext.Add("keyRecent", "最近播放的音樂"); MediaHistory.Instance.WriteRecentPlay(mediaHistoryItem); } private void btnAcquired_Click(object sender, RoutedEventArgs e) { StreamResourceInfo sri = Application.GetResourceStream(new Uri("Assets/TileBackgroundBlue.png", UriKind.Relative)); MediaHistoryItem mediaHistoryItem = new MediaHistoryItem(); mediaHistoryItem.ImageStream = sri.Stream; mediaHistoryItem.Title = "最新上市"; mediaHistoryItem.PlayerContext.Add("keyAcquired", "最新上市的音樂"); MediaHistory.Instance.WriteAcquiredItem(mediaHistoryItem); } } }
OK
[源碼下載]