【问题标题】:MPMusicPlayerController setQueueWithStoreIDs playing index?MPMusicPlayerController setQueueWithStoreIDs 播放索引?
【发布时间】:2017-09-03 10:16:05
【问题描述】:

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

【问题讨论】:

    标签: swift xcode audio-player


    【解决方案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()
    
            }
    
       }
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 您能否解释一下代码并在其周围添加更多细节以使其对未来的读者更有用?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多