MPMusicPlayerController setQueueWithStoreIDs 播放索引?

MPMusicPlayerController setQueueWithStoreIDs playing index?

我在使用音乐播放器时遇到问题,当我使用商店 ID 设置队列并调用方法 play() 播放第一首曲目时,如何让它播放队列的 X 索引?

    var ids:[String] = []
    for song in self.queue
    {
       ids.append(String(song.epf_song_id))
    }
    print("ids \(ids.count)")
    applicationMusicPlayer.setQueueWithStoreIDs(ids)
applicationMusicPlayer.play()

我发现解决方案可能会对其他人有所帮助,我应该使用 MPMusicPlayerStoreQueueDescriptor

func playByStoreID( storeIds:[String] )
{

    DispatchQueue.main.async {
        //Prepare before play ios 10.1 and above
        if #available(iOS 10.1, *)
        {

            var ids:[String] = []

            for i in self.queue
            {
               ids.append(String(i.epf_song_id))
            }

            let descriptor:MPMusicPlayerStoreQueueDescriptor = MPMusicPlayerStoreQueueDescriptor(storeIDs: ids)
                descriptor.startItemID =  storeIds[0]

           self.applicationMusicPlayer.setQueue(with: descriptor)
            self.applicationMusicPlayer.prepareToPlay { (error) in

                //Wait 5 seconds
                if (error != nil)
                {
                    let errorCode:Int =  (error! as NSError).code

                    print("[MUSIC PLAYER] Error \(String(describing: error))")

                    if  errorCode == 4 && self.currectPlaying.failed == 0
                    {
                        print("[MUSIC PLAYER] Error Load track will play again after 5 seconds")
                        self.applicationMusicPlayer.stop()
                        self.currectPlaying.failed += 1

                        DispatchQueue.main.asyncAfter(deadline: .now() + 5 , execute: {
                            self.play_from_apple( false )
                        })

                    }else
                    {
                        //Inform player to play with another streamer
                        self.fullFailure()
                        print("[MUSIC PLAYER] Error preparing : \(String(describing: error))")
                    }

                    return
                }else
                {
                    self.applicationMusicPlayer.play()
                    self.playedBy = .apple
                }
            }

        }else
        //Play directly ios below version 10.1
        {
            self.applicationMusicPlayer.setQueue(with: storeIds)
            self.applicationMusicPlayer.play()

        }

   }
}

这就是我解决这个问题的方法.. MPMusicPlayerController.systemMusicPlayer.prepareToPlay 从未返回任何值... 所以我做了一个玩播放器的技巧,然后这个函数开始返回值... b/c 如果出现错误,我必须导航到另一个屏幕,如果没有错误,我必须导航到播放器视图

            if #available(iOS 10.1, *)
            {
               let floatVersion = (UIDevice.current.systemVersion as NSString).floatValue
                MPMusicPlayerController.systemMusicPlayer.setQueue(with: "YourSongID");
               if #available(iOS 11.1, *)
                {
                   MPMusicPlayerController.systemMusicPlayer.play()
                }

                MPMusicPlayerController.systemMusicPlayer.prepareToPlay(completionHandler: {
                    (Eroror) in
                    if(Eroror == nil)
                    {
                        if(floatVersion < 11.1)
                        {
                           MPMusicPlayerController.systemMusicPlayer.pause()
                        }

                    }
                    else
                    {

                    }
                })



            }