让AudioPlayerAgent支持动态播放列表(Windows Phone)
最近做的是有关背景音乐播放(调用系统播放器)的活儿,其中牵扯到Microsoft.Phone.BackgroundAudio.BackgroundAudioPlayer播放本地或远程音频时候的播放列表改动问题。如果希望播放系统Music + Videos中存储的音乐,那么直接在UI利用Microsoft.Xna.Framework.Media传递和播放音乐即可。但如果是BackgroundAudioPlayer和AudioPlayerAgent就不太好办。
可能有人会说这种问题还用得着单写一篇文章吗,AudioPlayerAgent当然支持动态列表了。但很可惜MSDN的官方How-To的例子就是硬编码的列表(还是本地音频),不然也不会在App Hub Forum上出现这样的问题(1、2)。
一般来说将播放列表传递给Agent有这几种方式:
- IsolatedStorageSettings
- 静态方法或属性(作为BackgroundAgent,实例化它没有意义)
- IsolatedStorageFile
实验之后就会发现,前两种方法并不好用:虽然在UI线程中设置了列表或者通过静态方法、属性传递了列表,但启动后的Agent拿不到这些数据。就算是单写一个存储播放列表数据的静态类也是一样。那么留给我们的方法只有通过文件传递。
在上面提到的那个帖子中,一位微软员工Mark Chamberlain给出的方案也是如此(全文请看原帖):
This is how I did it:
In background audio agent code:
OnUserAction.Play/SkipNext/SkipPrevious: Check if BackgroundAudioPlayer.Instance.Track == null, if yes then load playlist from IsoStorage into m_playlist and start playing first track, if no then play current/next/previous song from m_playlist
In the album viewmodel code (each time a user selects an album a new viewmodel gets created for this app):
OnInitialization:…
Another developer who reviewed this suggested that instead of calling ..Instance.Close(), which also releases all the background audio resources, a better approach is to set BackgroundAudioPlayer.Instance.Track = null, which stops and clears, but keeps the background resources allocated.
基本思路即UI存好文件,Agent初始化时候载入。任何和播放相关操作前播放列表如果为null则再次载入。
具体到我手头的例子,文件格式我直接用的Xml序列化,写起来简单。由于需要和UI保持实时,那么每修改一次播放列表(添加、删除、改动)都要保存,同时在Agent一边,任何和播放相关操作前则不仅检查是否列表为null,而是直接全部重新载入。类似于:
protected override void OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param) {
var allTracks = TrackManager.GetAllTracks();
_playList = new List();
foreach ( TrackItem trackItem in allTracks ) {
_playList.Add(
new AudioTrack( null, trackItem.Title, trackItem.Album, null, null, trackItem.Url, EnabledPlayerControls.Pause ) );
}
//...
}
代码写出来非常愚蠢,但目前为止找不到更好的方法。
generic viagra walmart buy viagra online usa over the counter viagra
where to buy viagra buy real viagra online viagra without a doctor prescription
best place to buy generic viagra online viagra online usa viagra from canada
order viagra online where to buy viagra online mexican viagra
how much is viagra online doctor prescription for viagra viagra over the counter
buy generic 100mg viagra online pfizer viagra 100mg price п»їviagra pills
buy viagra online usa online doctor prescription for viagra viagra from canada
how much is viagra buy real viagra online best place to buy generic viagra online
viagra amazon https://viagrasus.com/
where to buy viagra https://viagrasus.com/
viagra over the counter walmart https://viagrasus.com/
buy real viagra online https://viagrasus.com/
mexican viagra https://viagrasus.com/
viagra cost per pill https://viagrasus.com/
100mg viagra https://viagrasus.com/
viagra price https://viagrasus.com/
viagra cost per pill viagra coupon viagra from canada
viagra amazon https://viagrasus.com/
Рї??cialis price of cialis 20 mg generic vs brand name for cialis
Wonderful views on that!
ed. trusted medstore in cialis best price for daily cialis cialis online without perscription
Hey! awesome blog! I happen to be a daily visitor to your site (somewhat more like addict 😛 ) of this website. Just wanted to say I appreciate your blogs and am looking forward for more!
I have been surfing online more than three hours today, yet I never found anything that grabbed my interest as much as this piece.
I’ve been surfing online more than 3 hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my view, if all web owners and bloggers made good content as you did, the net will be much more useful than ever before.
I’d like to be able to write like this, but taking the time and developing articles is hard…. Takes a lot of effort.
vcialis price of cialis 20 mg cialis with dapoxetine uk
You are good writer. Thank you.
I don’t normally comment on blogs.. But nice post! I just bookmarked your site
I had highly recommend this blog to my good friend, it’s so good
Just what I needed to know thank you for this.
I am very happy to look your post. Thanks a lot and i am taking a look ahead to touch you.
Admiring the time and effort you put into your site and detailed info you offer!
by cialis tablets in frankfurt pharmacy generic version of cialis cialis using paypal in australia
You are one talented writer thank you for the post.
Thank you a lot for sharing this with all folks you actually recognize what you’re speaking about! Bookmarked. Please additionally visit my site =). We can have a hyperlink trade contract among us!
cialis 20 mg tablets drugstore1st cialis how much is a cialis prescription with no insurance
viagra or cialis tadalafil 20mg best price where to buy cialis from canada
I do not even know how I ended up here, but I thought this post was good. I do not know who you are but certainly you are going to a famous blogger if you are not already 😉 Cheers!
Our local network of agencies has found your research so helpful.
stromectol liquid buy ivermectin ivermectin 5 mg price
genric cialis generic tadalafil 20mg india
Thanks , I’ve recently been searching for info about this topic for ages and yours is the best I have discovered so far. But, what concerning the bottom line? Are you certain concerning the source?
stromectol for humans cost of ivermectin 1% cream
stromectol online canada ivermectin generic
https://stromectolgf.online/# buy stromectol uk
I saw a similar post on another website but the points were not as well articulated.
perfect
thank you for a very good article
how to get prescription drugs without doctor ed meds online without doctor prescription
https://drwithoutdoctorprescription.com/# canadian drug
buy prescription drugs without doctor comfortis without vet prescription