让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. Propecia说道:

    finasteride 5 mg prices propecia prescription online cheap propecia online

  2. Propecia说道:

    propecia india finesterude no prescription propecia buy without per

  3. Gregoryundum说道:

    prescription drugs non prescription ed pills buy prescription drugs from canada cheap

  4. Propecia说道:

    where to buy propecia generic propecia usa generic propecia

  5. Propecia说道:

    buy propecia propecia 5mg sale generic propecia no prescription

  6. Propecia说道:

    buy propecia uk propecia 5 mg buy propecia online uk

  7. Propecia说道:

    finasteride 5mg generic finasteride for sale finesterude no prescription

  8. Propecia说道:

    propecia purchase cheap propecia canada where to buy finasteride

  9. Gregoryundum说道:

    how to inject ivermectin in a dog stromectol dosage heartgard ivermectin

  10. Canadak说道:

    canadian online pharmacy sildenafil without a doctor’s prescription sildenafil without a doctor’s prescription

  11. Stromectol说道:

    ivermectin for human ivermectin toxicity dog stromectol coupon

  12. Stromectol说道:

    ivermectin dose for humans ivermectin for dogs dosage ivermectin studies 2021

  13. Stromectol说道:

    ivermectin covid 19 treatment ivermectin for hookworms in humans ivermectin pill for scabies

  14. Stromectol说道:

    ivermectin candida ivermectin tractor supply does ivermectin kill ringworm in cattle

  15. Stromectol说道:

    ivermectin heartworms durvet ivermectin sheep drench can you buy ivermectin over the counter

  16. Stromectol说道:

    ivermectin cream uses ivermectin for canaries ivermectin purchase

  17. Stromectol说道:

    ivermectin for foxes ivermectin for pregnant goats side effects of ivermectin

  18. Gregoryundum说道:

    ed treatment drugs natural ed medications best medication for ed

  19. Edpills说道:

    drugs for ed ed drugs compared ed medication

  20. CanadaEd说道:

    new ed drugs online ed pills ed pills online

  21. CanadaEd说道:

    online ed pills otc ed pills cure ed

  22. CanadaEd说道:

    ed medication online treatment for ed ed drugs

  23. CanadaEd说道:

    otc ed pills herbal ed treatment ed pills cheap

  24. CanadaEd说道:

    best otc ed pills best male ed pills new ed treatments

  25. CanadaEd说道:

    top erection pills online ed pills best male ed pills

  26. CanadaEd说道:

    cheap erectile dysfunction pill over the counter erectile dysfunction pills best erectile dysfunction pills

  27. CanadaEd说道:

    erectile dysfunction medication cure ed best ed pills

  28. Gregoryundum说道:

    finasteride buy online finasteride for sale where to get propecia

  29. Stromectol说道:

    ivermectin coronavirus ivermectin/praziquantel for horses ivermectin parasite cleanse

  30. Gregoryundum说道:

    cheap propecia generic propecia 1mg propecia cost

  31. Gregoryundum说道:

    best canadian pharmacy online best online canadian pharmacy online canadian drugstore

  32. Gregoryundum说道:

    prescription drugs online prescription drugs without doctor approval prescription drugs online without doctor

  33. Stromsiz说道:

    stromectol price usa buy stromectol online stromectol 12 mg tablets

  34. Stromsiz说道:

    п»їerectile dysfunction medication top ed drugs buy ed pills

  35. Onlikoel说道:

    meds online without doctor prescription india pharmacy india pharmacies online

  36. Okisdlf说道:

    ordering prescription drugs from canada meds online without doctor prescription internet pharmacy no prior prescription

  37. Okisdlf说道:

    buying from canadian online pharmacies ordering prescription drugs from canada discount prescription drugs online

  38. Okisdlf说道:

    trusted online canadian pharmacy reviews canadian pharmacy certified canada pharmacy online canadian pharmacy certified canada pharmacy online

  39. Okisdlf说道:

    highest rated canadian pharmacies canadian pharmacies not requiring prescription discount prescription drugs online

  40. Okisdlf说道:

    prescription drugs without doctor approval canadian pharmacy online ship to usa internet pharmacy no prior prescription

  41. Okisdlf说道:

    internet pharmacy no prior prescription trusted online canadian pharmacy reviews aarp approved canadian online pharmacies

  42. Okisdlf说道:

    trusted online canadian pharmacy reviews canadian pharmacy online ship to usa discount prescription drugs online

  43. Okisdlf说道:

    canadian pharmacies not requiring prescription canada rx drugs online pharmacy without dr prescriptions

  44. Okisdlf说道:

    canada rx drugs online buying from canadian online pharmacies canada rx drugs online

  45. Stromsiz说道:

    canadian pharmaceuticals online safe aarp approved canadian online pharmacies aarp approved canadian online pharmacies

  46. Edpilol说道:

    medicine erectile dysfunction cheap erectile dysfunction pills online best medication for ed

  47. Stromectol说道:

    stromectol pills for humans stromectol without a doctor prescription prescribing stromectol

  48. Stromectol说道:

    stromectol for humans for sale stromectol for sale stromectol 12 mg tablets

  49. Stromectol说道:

    stromectol price usa stromectol pills for humans stromectol 3 mg tablets price