The Background Audio Streamer sample demonstrates how to create an app that uses a MediaStreamSource to stream audio content from a AudioStreamingAgent. This agent will run in the background and under the lock screen where you can control the audio using the Universal Volume Control (UVC). You can also control audio playback directly from the UI of the app, when it is the foreground app. The sample uses a simple sine wave generator to simulate an audio stream so it doesn’t depend on a network connection. For more info about background audio, see Background audio overview for Windows Phone.

本例子演示了如何从“AudioStreamingAgent” 代理创建一个“MediaStreamSource”,该代理将会后台运行,同时锁屏时你也可以通过 UVC(Universal volume Control)控制音频的播放。

本例子中,当app 前台运行时,仅仅使用一个简单的Wave(.wav)生成器来模拟一个音频流,因此它其实并不需要网络连接。

  • 计时器的使用

1.  DispatcherTimer:      集成到按指定时间间隔和指定优先级处理的 System.Windows.Threading.Dispatcher 队列中的计时器。

DispatcherTimer _timer;

           // New instance
_timer = new DispatcherTimer();
// Set interval
_timer.Interval = TimeSpan.FromSeconds(0.5);
// Set delegate
_timer.Tick += new EventHandler(UpdateState);

2. 事件 处理

每个0.5s 更新一次界面, 播放器相关进度,时间,状态等信息,从BackgroundAudioPlayer获取。

        private void UpdateState(object sender, EventArgs e)
{
txtState.Text = string.Format("State: {0}", BackgroundAudioPlayer.Instance.PlayerState); if (BackgroundAudioPlayer.Instance.Track != null)
{
txtTrack.Text = string.Format("Track: {0}", BackgroundAudioPlayer.Instance.Track.Title); // Set the current position on the ProgressBar.
positionIndicator.Value = BackgroundAudioPlayer.Instance.Position.TotalSeconds; // Update the current playback position.
TimeSpan position = new TimeSpan();
position = BackgroundAudioPlayer.Instance.Position;
textPosition.Text = String.Format("{0:d2}:{1:d2}:{2:d2}", position.Hours, position.Minutes, position.Seconds); // Update the time remaining digits.
TimeSpan timeRemaining = new TimeSpan();
timeRemaining = BackgroundAudioPlayer.Instance.Track.Duration - position;
textRemaining.Text = String.Format("-{0:d2}:{1:d2}:{2:d2}", timeRemaining.Hours, timeRemaining.Minutes, timeRemaining.Seconds);
}
}
  • BackgroundAudioPlayer的使用

1. 相关操作方法

BackgroundAudioPlayer.Instance.SkipPrevious();  //前一个

BackgroundAudioPlayer.Instance.Play(); //播放

BackgroundAudioPlayer.Instance.Pause(); //暂停

BackgroundAudioPlayer.Instance.SkipNext(); //后一个

2. 两种类型的后台音频应用程序:

一种类型实现简单的播放列表并将一个包含媒体文件地址的URI传递给Zune媒体队列以设置当前曲目。URI可以是手机的本地或远程地址。

另一种类型的后台音频应用程序使用MediaStreamSource实现音频流的播放,此音频流的格式则可以是任何格式。这是因为由MediaStreamSource派生的类实现了对音频流的处理和解码。

3. 实现AudioStreamingAgent

必须实现其虚函数 OnBeginStreaming, 这个方法将会在新的音频需要解码时被调用。

这里使用SineMediaStreamSource 模拟了一个音频流 用作返回。

    public class AudioTrackStreamer : AudioStreamingAgent
{
/// <summary>
/// Called when a new track requires audio decoding
protected override void OnBeginStreaming(AudioTrack track, AudioStreamer streamer)
{
// Set the source of streamer to a media stream source
double freq = Convert.ToDouble(track.Tag); // Use sine wave audio generator to simulate a streaming audio feed
SineMediaStreamSource mss = new SineMediaStreamSource(freq, 1.0, TimeSpan.FromSeconds()); // Event handler for when a track is complete or the user switches tracks
mss.StreamComplete += new EventHandler(mss_StreamComplete); // Set the source
streamer.SetSource(mss);
}

4. 实现AudioPlayerAgent

    // 摘要:
// Microsoft.Phone.BackgroundAgent 的实现,专门设计为在后台播放收音机。
public class AudioPlayerAgent : BackgroundAgent
{
// 摘要:
// 创建 Microsoft.Phone.BackgroundAudio.AudioPlayerAgent 的新实例。
public AudioPlayerAgent(); // 摘要:
// 当播放出现错误(如未正确下载音频曲目)时调用。
//
// 参数:
// player:
// Microsoft.Phone.BackgroundAudio.BackgroundAudioPlayer。
//
// track:
// 发生错误的曲目。
//
// error:
// 发生的错误。
//
// isFatal:
// 如果为 true,则播放无法继续并且此曲目的播放也会停止。
protected virtual void OnError(BackgroundAudioPlayer player, AudioTrack track, Exception error, bool isFatal);
//
// 摘要:
// 当播放状态发生更改时调用,错误状态除外。
//
// 参数:
// player:
// Microsoft.Phone.BackgroundAudio.BackgroundAudioPlayer。
//
// track:
// 播放状态更改时播放的曲目。
//
// playState:
// 播放器的新状态。
protected virtual void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState);
//
// 摘要:
// 当用户通过某些应用程序提供的 UI 或通用卷控件 (UVC) 请求操作并且应用程序已请求该操作的通知时调用。
//
// 参数:
// player:
// Microsoft.Phone.BackgroundAudio.BackgroundAudioPlayer。
//
// track:
// 用户操作时播放的曲目。
//
// action:
// 用户已请求的操作。
//
// param:
// 与请求的操作关联的数据。
protected virtual void OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param);
}

5. 清单文件中添加后台音频Task

    <Tasks>
<DefaultTask Name="_default" NavigationPage="MainPage.xaml" />
<ExtendedTask Name="BackgroundTask">
<BackgroundServiceAgent Specifier="AudioPlayerAgent" Name="AudioPlaybackAgent1" Source="AudioPlaybackAgent1" Type="AudioPlaybackAgent1.AudioPlayer" />
<BackgroundServiceAgent Specifier="AudioStreamingAgent" Name="AudioStreamAgent1" Source="AudioStreamAgent1" Type="AudioStreamAgent1.AudioTrackStreamer" />
</ExtendedTask>
</Tasks>

例子:Background Audio Streamer Sample的更多相关文章

  1. 例子:Background Transfer Service Sample

    本例演示了如何使用后台传输服务来进行后台文件下载,也就是说及时App已经停止运行,同样可以通过后台代理进行文件的下载操作. 对于后台文件传输一下知识点必须注意: 1. 通过使用 BackgroundT ...

  2. 例子:RSS Reader Sample

    本例演示了Rss xml信息的获取,以及如何使用SyndicationFeed来进行符合Rss规范的xml进行解析. SyndicationFeed 解析完成后 可以得到SyndicationItem ...

  3. 例子:Execution Model Sample - 应用状态保存

    WP中,当你的应用被切换到后台 后,就进入了休眠状态,然后当一个应用从墓碑恢复时,如何恢复相应的状态,该例子就演示了如何保存和恢复UI以及APP相关状态. 这里有一篇很好的文章,请参见: http:/ ...

  4. [UE4] C++实现Delegate Event实例(例子、example、sample)

    转自:http://aigo.iteye.com/blog/2301010 虽然官方doc上说Event的Binding方式跟Multi-Cast用法完全一样,Multi-Cast论坛上也有很多例子, ...

  5. 例子:Basic Lens sample

    本例演示了如何自己扩展一个Camera Lens. 1. UI界面是一个MediaViewer <controls:MediaViewer x:Name="MediaViewer&qu ...

  6. Windows Phone background Audio 后台音频

    Windows Phone 后台音频的确不是什么新鲜的话题了,但发现目前在WP平台的音频播放应用多多少少会有一些瑕疵,所以在此给大家在此介绍下这个功能给有需要的朋友们. 首先介绍下我们的应用在后台播放 ...

  7. [Z] Windows 8/10 audio编程

    都是些网上搜到的比较不错的文章.关于这块儿的内容网上帖子不多.出去下面列的最主要的还有参考MSDN. WASAPI使用介绍: https://blogs.windows.com/buildingapp ...

  8. AVAudioSession(3):定制 Audio Session 的 Category

    本文转自:AVAudioSession(3):定制 Audio Session 的 Category | www.samirchen.com 本文内容主要来源于 Working with Catego ...

  9. A Tutorial on Using the ALSA Audio API

    A Tutorial on Using the ALSA Audio API This document attempts to provide an introduction to the ALSA ...

随机推荐

  1. java中使用队列:java.util.Queue (转)

    Queue接口与List.Set同一级别,都是继承了Collection接口.LinkedList实现了Queue接 口.Queue接口窄化了对LinkedList的方法的访问权限(即在方法中的参数类 ...

  2. ireport5.6+jasperreport6.3开发(五)--以javabean为基准的报表开发(action关联)

    这里的是定方法主要参照sturts2-jasperreport-plugin的完成方法(其实就是抄的) PDF的样子是这样的两页的pdf 然后action的配置是这样的(不要在意格式) @Parent ...

  3. C语言课程学习的总结

    C语言课程学习的总结 学习C程序这门课一年了,这是我们学的第一门专业课.在大学里,C语言不但是计算机专业的必修课程而且也是非计算机专业学习计算机基础的一门必修课程.所以作为我这个计算机专业的学生来说当 ...

  4. 2015项目timeline

    1. app签到  http://h5.a.rongyi.com/html/app/sign/index.html (pc无效果.app端 20160105) 2.圣诞活动--砍价 http://h5 ...

  5. BIND的进程一:DNS简单配置与的主从配置

    DNS的简单配置和DNS的主从配置   摘要:DNS(Domain-Name Server) ,DNS的服务起到的作用就是名称解析,在网络通讯来说计算机与计算机是通过IP地址相互通信的, 当是IP地址 ...

  6. 结构struct

    1.结构变量 1)定义结构类型 struct student { char *name; int age; int score[3]; }; 2)定义结构变量 struct student stu1, ...

  7. css中vw,vh单位对于UC的兼容性问题

    vw,vh单位在移动端浏览器不兼容,在网上找半天也没找到什么官方的解决方法:我就试了一下在使用到vh的单位之前添加一个用px定义的样式: 如: 当浏览器不是别100vw单位的时候 就会赋给px单位的样 ...

  8. 239. Sliding Window Maximum *HARD* -- 滑动窗口的最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  9. IT求职中,笔试、面试的算法准备

    PS:此文章为转载,源地址:http://www.newsmth.net/nForum/#!article/CoderInterview/849     作者应该是在美国进行的笔试面试,感觉面试的的公 ...

  10. [原创]导出CSV文件,特殊字符处理。

    CSV文件格式 1.CSV文件默认以英文逗号(,)做为列分隔符,换行符(\n)作为行分隔符.2.CSV默认认为由""括起来的内容是一个栏位,这时不管栏位内容里有除"之外字 ...