让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 ) );
}
//...
}
代码写出来非常愚蠢,但目前为止找不到更好的方法。
বাংলা প্রশ্ন উত্তর সাইট তল্লাশি, আজই আপনার সমস্যা নিয়ে প্রশ্ন করুন , অভিজ্ঞ ব্যক্তিগন আপনার প্রশ্নের উত্তর দিয়ে সহযোগিতা করবে । তাছাড়াও অন্যাদের প্রশ্নের উত্তর দিয়ে, জিতে নিন আকর্ষণীয় পুরস্কার । তল্লাশিতে যেকোনো বিষয়ে প্রশ্ন করতে পারেন।।
review of cialis tadalafil 20mg
cat antibiotics without pet prescription buy prescription drugs from canada
Good post. I study something more difficult on different blogs everyday. It’s going to always be stimulating to learn content material from other writers and observe a little bit one thing from their store. I’d prefer to use some with the content material on my blog whether you don’t mind. Natually I’ll give you a link in your web blog. Thanks for sharing.
how can i order prescription drugs without a doctor prescription drugs online without doctor
buy prescription drugs online meds online without doctor prescription
I don’t normally comment but I gotta say appreciate it for the post on this one : D.
whoah this weblog is great i really like studying your articles. Stay up the great work! You already know, lots of persons are looking round for this information, you can aid them greatly.
How long does it take you to write an article like this?
I had highly recommend this blog to my good friend, it’s so good
п»їviagra pills where to buy viagra
https://sildenafilmg.com/ viagra amazon
viagra from canada viagra 100mg price buy viagra online usa
can i buy viagra from canada
canada pharmacy
does viagra work
buy generic 100mg viagra online viagra amazon
viagra online
viagra pill for men
when will viagra be generic generic viagra walmart price of viagra
over the counter viagra price of viagra
online pharmacy review
cheap online pharmacy
online ed drugs no prescription
https://sildenafilmg.com/ mexican viagra
viagra from india https://sildenafilmg.online/
viagra cost per pill
where to buy viagra online viagra 100mg price generic viagra walmart
order viagra online where to buy viagra online viagra over the counter
viagra without a doctor prescription viagra without a doctor prescription cost of viagra
I’ve read several good stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to make such a magnificent informative site.
best place to buy generic viagra online best place to buy viagra online
when will viagra be generic buy viagra online canada where to buy viagra
buy viagra online usa how much is viagra best place to buy viagra online
when will viagra be generic order viagra online online doctor prescription for viagra
viagra price over the counter viagra where to buy viagra
best place to buy generic viagra online where to buy viagra viagra online usa
viagra shop viagra price
https://sildenafilmg.online/# viagra from canada
I like what you have to offer. Keep up the good work!
viagra 100mg price generic viagra walmart
A thoughtful insight and ideas I will use on my blog. You’ve obviously spent some time on this. Congratulations
nicee content keep writing
Just a quick note to express my appreciation. Take care
I want to see your book when it comes out.
Assume you are doing good linking to position you on the first pages of search engines.
viagra shop viagra over the counter walmart
viagra cost cheap viagra online
What i discover troublesome is to find a weblog that may capture me for a minute however your blog is different. Bravo.
Well done! Keep up this quality!
Our community leaders need to read this, and look at developing some of your recommendations.