让AudioPlayerAgent支持动态播放列表(Windows Phone)

最近做的是有关背景音乐播放(调用系统播放器)的活儿,其中牵扯到Microsoft.Phone.BackgroundAudio.BackgroundAudioPlayer播放本地或远程音频时候的播放列表改动问题。如果希望播放系统Music + Videos中存储的音乐,那么直接在UI利用Microsoft.Xna.Framework.Media传递和播放音乐即可。但如果是BackgroundAudioPlayer和AudioPlayerAgent就不太好办。

可能有人会说这种问题还用得着单写一篇文章吗,AudioPlayerAgent当然支持动态列表了。但很可惜MSDN的官方How-To的例子就是硬编码的列表(还是本地音频),不然也不会在App Hub Forum上出现这样的问题(12)。

一般来说将播放列表传递给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 ) );
    }
    //...
}

代码写出来非常愚蠢,但目前为止找不到更好的方法。

261 Responses to “让AudioPlayerAgent支持动态播放列表(Windows Phone)”

  1. tollashi说道:

    বাংলা প্রশ্ন উত্তর সাইট তল্লাশি, আজই আপনার সমস্যা নিয়ে প্রশ্ন করুন , অভিজ্ঞ ব্যক্তিগন আপনার প্রশ্নের উত্তর দিয়ে সহযোগিতা করবে । তাছাড়াও অন্যাদের প্রশ্নের উত্তর দিয়ে, জিতে নিন আকর্ষণীয় পুরস্কার । তল্লাশিতে যেকোনো বিষয়ে প্রশ্ন করতে পারেন।।

  2. RalphAberi说道:

    review of cialis tadalafil 20mg

  3. CameronBoste说道:

    cat antibiotics without pet prescription buy prescription drugs from canada

  4. 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.

  5. I don’t normally comment but I gotta say appreciate it for the post on this one : D.

  6. 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.

  7. How long does it take you to write an article like this?

  8. I had highly recommend this blog to my good friend, it’s so good

  9. Michaeljealp说道:

    п»їviagra pills where to buy viagra

  10. MichaelVuT说道:

    https://sildenafilmg.com/ viagra amazon

  11. Sildenafil说道:

    viagra from canada viagra 100mg price buy viagra online usa

  12. Brandonblona说道:

    buy generic 100mg viagra online viagra amazon

  13. Sildenafil说道:

    when will viagra be generic generic viagra walmart price of viagra

  14. Michaeljealp说道:

    over the counter viagra price of viagra

  15. MichaelVuT说道:

    https://sildenafilmg.com/ mexican viagra

  16. Sildenafil说道:

    viagra from india https://sildenafilmg.online/
    viagra cost per pill

  17. Viagrak说道:

    where to buy viagra online viagra 100mg price generic viagra walmart

  18. Viagrak说道:

    order viagra online where to buy viagra online viagra over the counter

  19. Viagrak说道:

    viagra without a doctor prescription viagra without a doctor prescription cost of viagra

  20. Residential RDP说道:

    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.

  21. Brandonblona说道:

    best place to buy generic viagra online best place to buy viagra online

  22. Viagrak说道:

    when will viagra be generic buy viagra online canada where to buy viagra

  23. Viagrak说道:

    buy viagra online usa how much is viagra best place to buy viagra online

  24. Viagrak说道:

    when will viagra be generic order viagra online online doctor prescription for viagra

  25. Viagrak说道:

    viagra price over the counter viagra where to buy viagra

  26. Viagrak说道:

    best place to buy generic viagra online where to buy viagra viagra online usa

  27. Michaeljealp说道:

    viagra shop viagra price

  28. MichaelVuT说道:

    https://sildenafilmg.online/# viagra from canada

  29. I like what you have to offer. Keep up the good work!

  30. A thoughtful insight and ideas I will use on my blog. You’ve obviously spent some time on this. Congratulations

  31. nicee content keep writing

  32. Just a quick note to express my appreciation. Take care

  33. I want to see your book when it comes out.

  34. Assume you are doing good linking to position you on the first pages of search engines.

  35. Michaeljealp说道:

    viagra shop viagra over the counter walmart

  36. What i discover troublesome is to find a weblog that may capture me for a minute however your blog is different. Bravo.

  37. Well done! Keep up this quality!

  38. wimbledon说道:

    Our community leaders need to read this, and look at developing some of your recommendations.