MPMusicPlayerController setQueueWithStoreIDs 播放索引? [英] MPMusicPlayerController setQueueWithStoreIDs playing index?

查看:27
本文介绍了MPMusicPlayerController setQueueWithStoreIDs 播放索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

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

        }

   }
}

这篇关于MPMusicPlayerController setQueueWithStoreIDs 播放索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆