与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成...
生活随笔
收集整理的這篇文章主要介紹了
与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文:與眾不同 windows phone (14) - Media(媒體)之音頻播放器, 視頻播放器, 與 Windows Phone 的音樂和視頻中心集成
[索引頁]
[源碼下載]
作者:webabcd
介紹
與眾不同 windows phone 7.5 (sdk 7.1) 之媒體
- 音頻播放器
- 視頻播放器
- 與 Windows Phone 的音樂和視頻中心集成
示例
1、演示音頻播放器
Audio.xaml
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
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
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
[源碼下載]
總結
以上是生活随笔為你收集整理的与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一道嚼烂的面试题
- 下一篇: 真实世界:使用WCF扩展记录服务调用时间