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

    Is that the best way to solve?

  2. Drugsi说道:

    how to get prescription drugs without doctor viagra without a prescription prescription drugs online without

  3. Drugsi说道:

    anti fungal pills without prescription ambien without a doctor’s prescription buy prescription drugs from india

  4. Drugsi说道:

    meds online without doctor prescription levitra without a doctor prescription meds online without doctor prescription

  5. Drugsi说道:

    viagra without doctor prescription amazon is it illegal to buy prescription drugs online tadalafil without a doctor’s prescription

  6. Drugsi说道:

    viagra vs cialis bodybuilding real viagra without a doctor prescription pet antibiotics without vet prescription

  7. Drugsi说道:

    prices of viagra at walmart comfortis for dogs without vet prescription is it illegal to buy prescription drugs online

  8. Drugsi说道:

    how to get prescription drugs without doctor buy prescription drugs without doctor legal to buy prescription drugs from canada

  9. Drugsi说道:

    ed meds online without doctor prescription discount prescription drugs how to get prescription drugs without doctor

  10. Drugsi说道:

    buy anti biotics without prescription ed meds online prices of viagra at walmart

  11. Stromectol说道:

    stromectol purchase ivermectin for lice ivermectin stock market

  12. Prednisone说道:

    prednisone 5 mg brand name canine prednisone 5mg no prescription prednisone online paypal

  13. Prednisone说道:

    cheapest prednisone no prescription cost of prednisone 5mg tablets buying prednisone on line

  14. Prednisone说道:

    prednisone 2 5 mg prednisone prescription for sale prednisone pharmacy

  15. Jamessow说道:

    prednisone 475 cheap prednisone generic prednisone tablets

  16. Prednisone说道:

    cortisol prednisone buy prednisone canada prednisone pharmacy

  17. Prednisone说道:

    prednisone tablets canada prednisone for sale order prednisone 10 mg tablet

  18. Prednisone说道:

    cost of prednisone in canada prednisone steroids where to buy prednisone without prescription

  19. Prednisone说道:

    buy prednisone online no prescription prednisone tablets india prednisone cream brand name

  20. Prednisone说道:

    prednisone ordering online prednisone 10mg buy online buy prednisone 50 mg

  21. Prednisone说道:

    prednisone 20 mg tablets prednisone 54899 prednisone brand name in usa

  22. Jamessow说道:

    herbal remedies for ed erectile dysfunction medication prescription drugs without doctor approval

  23. Drugst说道:

    prescription drugs online without doctor meds online without doctor prescription generic viagra without a doctor prescription

  24. Stromectol说道:

    ivermectin 1.87 dosage for cats ivermectin pills for dogs ivermectin/pyrantel for dogs

  25. Stromectol说道:

    ivermectin for canaries how to use ivermectin pour-on for cattle ivermectin cream side effects

  26. Stromectol说道:

    ivermectin for pregnant goats moxidectin vs ivermectin stromectol for head lice

  27. Stromectol说道:

    what is stromectol liquid ivermectin scabies ivermectin dosage

  28. Stromectol说道:

    ivermectin petco buy stromectol online ivermectin and permethrin

  29. Stromectol说道:

    stromectol for lice stromectol for chickens how much ivermectin to give a dog for worms

  30. Stromectol说道:

    ivermectin for goats lice topical ivermectin ivermectin for lice on humans

  31. Stromectol说道:

    ivermectin for.covid ivermectin applicator gun ivermectin dose for goats with lice

  32. Stromectol说道:

    pierre khoury ivermectin does ivermectin kill ticks ivermectin overdose

  33. Stromectol说道:

    ivermectin for pregnant goats thomas borody ivermectin ivermax vs ivermectin

  34. Jamessow说道:

    ivermectin dose for dogs ivermectin buy nz durvet ivermectin sheep drench

  35. Jamessow说道:

    aspirin and ed viagra without a doctor prescription pills erectile dysfunction

  36. Jamessow说道:

    generica cialis cialis pills where to get my prescription cialis filled

  37. Jamessow说道:

    ivermectin 0.5% brand name ivermectin pour on tractor supply worming goats with ivermectin

  38. Stromectol说道:

    ivermectin egg withdrawal ivermectin safety ivermectin 1.87 dosage for cats

  39. CialisTad说道:

    cheap cialis professional cheap generic cialis canada generic cialis or tadalafil

  40. CialisTad说道:

    cialis shipping does cialis make you last longer in bed how to take cialis 5mg

  41. Jamessow说道:

    how much is cialis with insurance brand cialis on line can you mix cialis and viagra

  42. CialisTad说道:

    cialis over the counter at walmart cheap cialis uk how to get a prescription for cialis

  43. CialisTad说道:

    cialis free shipping canadian cialis lion what is cialis black

  44. CialisTad说道:

    brand cialis usa ebay cialis find cheapest cialis pills

  45. CialisTad说道:

    cialis online europe best price on cialis 20mg cialis viagara

  46. CialisTad说道:

    cialis w/dapoxetine best place to buy cialis online forum cialis without prescriptions canada

  47. CialisTad说道:

    side effects of cialis and grapefruits cialis viagra mail order uk discrete billing buy cialis online

  48. CialisTad说道:

    genereic cialis cheaper alternative to cialis online cialis no prescription

  49. CialisTad说道:

    generic cipla cialis dapoxetine cialis american express cialis vs.viagra reviews