Can't set MPMusicPlayerController queue with MPMusicPlayerMediaItemQueueDescriptor

前端 未结 3 1823
遇见更好的自我
遇见更好的自我 2021-01-28 06:12

This code results in silence:

let query = MPMediaQuery.songs()
let result = query.items
guard let items = result, items.count > 0 else {return}
let song = ite         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-28 06:22

    "It has been hosed since iOS 10.1 and it remains hosed in iOS 11.1." Seconded.

    I have been in the midst of a protracted fight just trying to a) get MPMusicPlayerController to simply work (finally managed that) and b) get it to play a song from an offset (still have not). And this is in an app that prior to iOS 10 worked 100% as expected.

    Right now, this is code that works for me (at least with regard to getting a song to play):

    let currentPlayer = MPMusicPlayerController.applicationQueuePlayer()
    currentPlayer.repeatMode = .none
    
    self.currentCollection = MPMediaItemCollection(items: [mediaItem])
    currentPlayer.beginGeneratingPlaybackNotifications()
    
    let query = MPMediaQuery.songs()
    let pred = MPMediaPropertyPredicate(value: mediaItem.persistentID, forProperty: MPMediaItemPropertyPersistentID)
    query.addFilterPredicate(pred)
    let desc = MPMusicPlayerMediaItemQueueDescriptor(query: query)
    currentPlayer.setQueueWith(desc)
    
    currentPlayer.prepareToPlay(completionHandler: {[unowned self] (error) in
        MPMusicPlayerController.applicationQueuePlayer().play()
    })
    

    It is a bit convoluted in that I had to use what I would consider the most complicated means based on available APIs. But several other methods cause the completion block to either never get called or get a completely unhelpful error. Right now I have 5 bugs reported to Apple with regard to MPMusicPlayerController so I am not exactly a happy developer at the moment. I will be opening one more related to this code as setStartTime & setEndTime does not appear to work any better than currentPlaybackTime for starting play at a specified offset.

    This code does dependably play synced and downloaded Apple Music items though.

提交回复
热议问题