与众不同 windows phone (15) - Media(媒体)之后台播放音频
[索引页]
[源码下载]
作者:webabcd
介绍
与众不同 windows phone 7.5 (sdk 7.1) 之媒体
- 通过 AudioPlayerAgent 实现在后台播放音频
示例
演示如何通过后台代理的方式来实现音频在后台的播放
1、后台代理
MyAudioPlayerAgent/AudioPlayer.cs
/** 本例演示如何播放后台音频(以 AudioPlayerAgent 为例,另 AudioStreamingAgent 用于流式播放音频)* 建议使用 AudioPlaybackAgent 类型的模板创建此项目* * BackgroundAgent - 后台代理类,抽象类,它是 ScheduledTaskAgent、AudioPlayerAgent 和 AudioStreamingAgent 的基类* NotifyComplete() - 用于通知系统,代理已经完成了当前的任务,调用此方法后,系统才会去准备执行下一次任务* Abort() - 用于通知系统,放弃此次和以后的任务* OnCancel() - 后台代理被取消时所执行的方法(由系统调用,比如后台代理转到休眠状态或终止状态前会调用此方法)* * AudioPlayerAgent - 后台播放音频的代理类* OnError() - 当播放中出现错误时,系统会调用此方法* OnPlayStateChanged() - 当播放状态发生改变时,系统会调用此方法(错误状态除外)* OnUserAction() - 当程序改变播放行为时,系统会调用此方法* * PlayState - Microsoft.Phone.BackgroundAudio.PlayState 枚举* Unknown, Stopped, Paused, Playing, BufferingStarted, BufferingStopped, TrackReady, TrackEnded, Rewinding, FastForwarding, Shutdown, Error* * UserAction - Microsoft.Phone.BackgroundAudio.UserAction 枚举* Stop, Pause, Play, SkipNext, SkipPrevious, FastForward, Rewind, Seek* * AudioTrack - 音频对象* Source - 音频的地址,远程地址或独立存储地址均可* Title - 音频名称* Duration - 音频时长* Album - 专辑名称* AlbumArt - 专辑封面的 Uri 地址,远程地址或独立存储地址均可,如果使用独立存储地址则其目录必须是 Shared/Media* Artist - 艺术家* PlayerControls - 锁屏等状态下的播放器控件显示(Microsoft.Phone.BackgroundAudio.EnabledPlayerControls 枚举)* None = 0, SkipNext = 1, SkipPrevious = 2, FastForward = 4, Rewind = 8, Pause = 16, All = 31* Tag - 任意内容,即为 AudioTrack 绑定一个上下文数据* BeginEdit() - 将 AudioTrack 设置为编辑状态(如果需要修改 AudioTrack 的属性则需要将 AudioTrack 设置为编辑状态)* EndEdit() - 结束 AudioTrack 的编辑状态*/using System; using System.Windows; using Microsoft.Phone.BackgroundAudio;using System.Collections.Generic;namespace MyAudioPlayerAgent {public class AudioPlayer : AudioPlayerAgent{// 播放列表private static List<AudioTrack> _playList = new List<AudioTrack>{new AudioTrack(new Uri("SuperMario.mp3", UriKind.Relative), "title", "artist", "album", null, null, EnabledPlayerControls.Pause),new AudioTrack(new Uri("http://traffic.libsyn.com/wpradio/WPRadio_29.mp3", UriKind.Absolute), "标题", "艺术家", "专辑", null, null, EnabledPlayerControls.All)};// 当前播放的 Track 在整个播放列表中的位置private static int _currentTrackNumber = 0;/** _classInitialized - 用于标记 AudioPlayer 是否已经被初始化* 标记成 volatile 是为了避免编译器认为此字段无外部修改,而将其优化放入寄存器(标记成 volatile 的字段只会放在内存中)* 一般来说,多任务环境下各任务间共享的字段应该被标记为 volatile*/private static volatile bool _classInitialized;public AudioPlayer(){if (!_classInitialized){_classInitialized = true;Deployment.Current.Dispatcher.BeginInvoke(delegate{Application.Current.UnhandledException += AudioPlayer_UnhandledException;});}}private void AudioPlayer_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e){if (System.Diagnostics.Debugger.IsAttached){System.Diagnostics.Debugger.Break();}}// 播放状态发生改变时所调用的方法protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState){switch (playState){case PlayState.TrackReady:player.Play();break;case PlayState.TrackEnded:PlayNextTrack(player);break;case PlayState.Playing:break;case PlayState.Paused:break;case PlayState.Stopped:break;case PlayState.BufferingStarted:break;case PlayState.BufferingStopped:break;case PlayState.Rewinding:break;case PlayState.FastForwarding:break;case PlayState.Shutdown:break;case PlayState.Unknown:break;}NotifyComplete();}// 当程序改变播放行为时所调用的方法protected override void OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param){switch (action){case UserAction.Play:PlayTrack(player);break;case UserAction.SkipPrevious:PlayPreviousTrack(player);break;case UserAction.SkipNext:PlayNextTrack(player);break;case UserAction.Rewind:player.Rewind();break;case UserAction.FastForward:player.FastForward();break;case UserAction.Seek:player.Position = (TimeSpan)param;break;case UserAction.Pause:player.Pause();break;case UserAction.Stop:player.Stop();break;}NotifyComplete();}// 播放下一个 Trackprivate void PlayNextTrack(BackgroundAudioPlayer player){if (++_currentTrackNumber >= _playList.Count)_currentTrackNumber = 0;PlayTrack(player);}// 播放上一个 Trackprivate void PlayPreviousTrack(BackgroundAudioPlayer player){if (--_currentTrackNumber < 0)_currentTrackNumber = _playList.Count - 1;PlayTrack(player);}// 播放当前 Trackprivate void PlayTrack(BackgroundAudioPlayer player){player.Track = _playList[_currentTrackNumber];}// 出现异常时所调用的方法protected override void OnError(BackgroundAudioPlayer player, AudioTrack track, Exception error, bool isFatal){if (isFatal)Abort();elseNotifyComplete();}// 当后台代理转到休眠状态或终止状态前,系统会自动调用此方法,需要在 5 秒内执行完毕protected override void OnCancel(){}} }/** 主程序引用此项目后,会在 manifest 中添加如下信息:* <ExtendedTask Name="BackgroundTask">* <BackgroundServiceAgent Specifier="AudioPlayerAgent" Name="MyAudioPlayerAgent" Source="MyAudioPlayerAgent" Type="MyAudioPlayerAgent.AudioPlayer" />* </ExtendedTask>*/
2、前台调用后台代理播放音频
BackgroundAudio.xaml
<phone:PhoneApplicationPage x: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" d:DesignHeight="768" d:DesignWidth="480"shell:SystemTray.IsVisible="True"><Grid x:Name="LayoutRoot" Background="Transparent"><StackPanel Orientation="Vertical"><Button x:Name="btnPrev" Content="上一首" Click="btnPrev_Click" /><Button x:Name="btnRewind" Content="快退" Click="btnRewind_Click" /><Button x:Name="btnPlay" Content="播放" Click="btnPlay_Click" /><Button x:Name="btnFastForward" Content="快进" Click="btnFastForward_Click" /><Button x:Name="btnNext" Content="下一首" Click="btnNext_Click" /><TextBlock x:Name="lblCurrentTrack" /><TextBlock x:Name="lblPosition" /></StackPanel></Grid></phone:PhoneApplicationPage>
BackgroundAudio.xaml.cs
/** 本例演示如何播放后台音频* * BackgroundAudioPlayer - 提供调用后台播放音频的相关功能* Instance - 返回 BackgroundAudioPlayer 实例* Track - 当前的 AudioTrack 对象。说明参见:MyAudioPlayerAgent/AudioPlayer.cs* PlayerState - 播放器状态(Microsoft.Phone.BackgroundAudio.PlayState 枚举)。说明参见:MyAudioPlayerAgent/AudioPlayer.cs* Position - 当前 AudioTrack 的播放位置* CanPause - 是否可暂停* CanSeek - 是否可以设置 Position 属性* BufferingProgress - 缓冲百分比(0 - 1之间)* Volume - 音量(0 - 1之间,默认值为 0.85)。注:目前 wp 没有提供 uvc(Universal Volume Control) 的接口* * Play - 播放当前 AudioTrack 的当前位置* Pause - 暂停* Stop - 停止* Rewind - 快退* FastForward - 快进 * SkipPrevious - 跳至上一个音频* SkipNext - 跳至下一个音频* Close - 关闭并释放所有资源* * PlayStateChanged - PlayState 发生改变时所触发的事件*/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.Phone.BackgroundAudio; using System.Windows.Navigation; using System.IO.IsolatedStorage; using System.Windows.Resources;namespace Demo.Media {public partial class BackgroundAudio : PhoneApplicationPage{public BackgroundAudio(){InitializeComponent();Init();}protected override void OnNavigatedTo(NavigationEventArgs e){ProcessPlayState();}private void Init(){// 由于播放本地音频时只能从独立存储中播放,所以此处把示例用音频文件从程序包中复制到独立存储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);}}}}CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);BackgroundAudioPlayer.Instance.PlayStateChanged += new EventHandler(Instance_PlayStateChanged);}// 实时更新当前音频播放的位置void CompositionTarget_Rendering(object sender, EventArgs e){if (BackgroundAudioPlayer.Instance != null)lblPosition.Text = BackgroundAudioPlayer.Instance.Position.TotalSeconds.ToString();}void Instance_PlayStateChanged(object sender, EventArgs e){ProcessPlayState();}void ProcessPlayState(){// 指定播放按钮是显示“播放”还是“暂停”switch (BackgroundAudioPlayer.Instance.PlayerState){case PlayState.Playing:btnPlay.Content = "暂停";break;case PlayState.Paused:case PlayState.Stopped:btnPlay.Content = "播放";break;}// 显示音频的 Title 和 Artistif (BackgroundAudioPlayer.Instance.Track != null)lblCurrentTrack.Text = BackgroundAudioPlayer.Instance.Track.Title + " by " + BackgroundAudioPlayer.Instance.Track.Artist;}// 播放或暂停音频private void btnPlay_Click(object sender, RoutedEventArgs e){if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState){BackgroundAudioPlayer.Instance.Pause();}else{BackgroundAudioPlayer.Instance.Play();}}// 跳至下一首音频private void btnNext_Click(object sender, RoutedEventArgs e){BackgroundAudioPlayer.Instance.SkipNext();}// 跳至上一首音频private void btnPrev_Click(object sender, RoutedEventArgs e){BackgroundAudioPlayer.Instance.SkipPrevious();}// 快退private void btnRewind_Click(object sender, RoutedEventArgs e){if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)BackgroundAudioPlayer.Instance.Rewind();}// 快进private void btnFastForward_Click(object sender, RoutedEventArgs e){if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)BackgroundAudioPlayer.Instance.FastForward();}} }
OK
[源码下载]
与众不同 windows phone (15) - Media(媒体)之后台播放音频相关推荐
- 与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成...
原文:与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成 [索引页] [源码下载] 与众不同 wi ...
- AVAudioPlayer 本地音频设置第三弹:后台播放音频
AVAudioPlayer 本地音频设置第三弹:后台播放音频 设备/引擎:Mac(11.6)/cocos 开发工具:Xcode(13.0) 开发语言:Objective-c/c++ 开发需求:满足后台 ...
- 当自己视频APP,遇到别人音频APP后台播放音频时候
当别家音乐APP播放音乐,打开自家视频APP(虽然没有播放视频),但音乐app的音频却暂停了(如果是钉钉语音会议这种情况,会自动退出语音会议). 查了资料:这是音频打断处理问题,举个例子: ...
- iOS- 关于AVAudioSession的使用——后台播放音乐
1.前言 •AVAudioSession是一个单例,无需实例化即可直接使用.AVAudioSession在各种音频环境中起着非常重要的作用 •针对不同的音频应用场景,需要设置不同的音频会话分类 1.1 ...
- IOS后台运行 之 后台播放音乐
IOS后台运行 之 后台播放音乐 iOS 4开始引入的multitask,我们可以实现像ipod程序那样在后台播放音频了.如果音频操作是用苹果官方的AVFoundation.framework实现,像 ...
- 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能...
原文:与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能 [源码下载] 与众不同 windows phone (40 ...
- CSS3 @media媒体查询 适配不同尺寸设备的响应式布局(清晰详解)
随着宽屏的不断普及,CSS3出现了@media媒体查询技术 一.了解@Media 相关知识 1.了解Media Queries Media Queries能在不同的条件下使用不同的样式,使页面在不同在 ...
- 与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器
原文:与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器 [索引页] [源码下载] 与众不同 windows phone (18) - Devic ...
- 与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密...
原文:与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密 [索引页] [源码下载] 与众不同 w ...
最新文章
- LeetCode 17. Letter Combinations of a Phone Number--笔试题--C++,Python解法
- PaddleHub 视频人脸检测
- 基于Redis实现分布式部署单点登录
- MATLAB中unzip出错,node.js里unzip出错的处理
- opencv vs2010 配置
- .Net 2.0 文档生成工具
- 7. Android Basic UI的布局 WidgetDemo基本组件演示
- Java 通过Executors创建线程池的种类
- SSH中常见jar包缺少错误
- 3.RESTful Web APIs中文版 --- 资源和表述
- linux中iso文件怎么安装,linux系统安装iso文件方法
- 谷歌翻译失效,解决网页谷歌翻译的问题-只能解决页面翻译
- 【nn.Parameter】Pytorch特征融合自适应权重设置(可学习权重使用)
- python公式计算_Python数学公式计算
- 一个完整的python文件即是一个模块_004孤荷凌寒从零开始学区块链第四天继续尝试用Python来设计一个区块链网络...
- 有趣的数字(C++)
- cheeta(cheetah mobile官方)
- 视频剪辑软件,AI智能合并随机生成新视频,并设置视频数量以及时长
- import * as 是什么?
- java万年历解析,java万年历