• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

MPMusicPlayerController setQueueWithStoreIDs 播放索引?

用户头像
it1352
帮助1

问题说明

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

i am having issue with music player , when i set queue with Store id's and call method play() its play first track , how i can make it play X index of queue ?

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

正确答案

#1

我发现解决方案可能会帮助别人,我应该使用 MPMusicPlayerStoreQueueDescriptor

i found the solution maybe will help someone else , i should use 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()

        }

   }
}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcgabbf
系列文章
更多 icon
同类精品
更多 icon
继续加载