2017-09-03 106 views
0

我与音乐播放器的问题,当我设置队列与商店ID和调用方法play()其播放第一轨,我怎么可以使它播放队列的X索引?MPMusicPlayerController setQueueWithStoreIDs播放索引?

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

回答

0

我找到了解决办法,也许会帮助别人,我应该使用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() 

     } 

    } 
} 
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 
        { 

        } 
       }) 



      } 
+0

您能解释一下代码并在其周围添加更多的细节,以使其对未来的读者更有用吗? – LW001

相关问题