MPMusicPlayerController setQueueWithStoreIDs播放索引?

时间:2017-09-03 10:16:06

标签: swift xcode audio-player

我遇到了音乐播放器的问题,当我使用商店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()

2 个答案:

答案 0 :(得分:1)

我发现解决方案可能会帮助其他人,我应该使用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()

        }

   }
}

答案 1 :(得分:0)

这就是我解决这个问题的方法.. 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
                    {

                    }
                })



            }