swift - MPMusicPlayerController.play() not working after killing iOS music app -


i have app allows search title of song in music library , play it. play selected song using code:

func playsongbypersistentid(id: int) { //id persistent id of chosen song     let predicate = mpmediapropertypredicate(value: id, forproperty: mpmediaitempropertypersistentid)     let songquery = mpmediaquery()     songquery.addfilterpredicate(predicate)      if songquery.items?.count < 1 {         print("could not find song") // make alert         return     } else {         print("gonna play \(songquery.items?[0].title)")     }      musicplayer.preparetoplay()     musicplayer.setqueuewithitemcollection(songquery.collections![0])     musicplayer.play() } 

the above function gets called in tableview(:didselectrowatindexpath). have confirmed correct id , song title retrieved when song selected.

here problem. if go app , select song play after killing ios music app, song not play. if choose different song, different song plays no problem. if choose same song on , on again never plays.

musicplayer systemmusicplayer declared in class.

is ios bug? have no idea going on.

here workaround found.

what set timer once try start playing music. timer calls function tests if music playing. if music playing, timer invalidated. if not, re-queues item want play (a collection of items in example) , tries play again.

i've pulled code app , abstracted it, may not compile gets point across. i'm going file bug apple (after creating small sample project) , recommend same.

func playmusic() {         musicplayer = mpmusicplayercontroller.applicationmusicplayer()      musicplayer.setqueuewithitemcollection(mpmediaitemcollection(items: songsforplayingwithmusicplayer))      musicplayer.play()      testformusicplayingtimer = nstimer.scheduledtimerwithtimeinterval(nstimeinterval(1), target: self, selector: "testformusicplaying", userinfo: nil, repeats: false) }  func testformusicplaying() {     if musicplayer.playbackstate != .playing     {         testformusicplayingtimer.invalidate()          musicplayer = mpmusicplayercontroller.applicationmusicplayer()          musicplayer.setqueuewithitemcollection(mpmediaitemcollection(items: songsforplayingwithmusicplayer))          musicplayer.play()          testformusicplayingtimer = nstimer.scheduledtimerwithtimeinterval(nstimeinterval(1), target: self, selector: "testformusicplaying", userinfo: nil, repeats: false)     }     else     {         testformusicplayingtimer.invalidate()          } } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -