0

Trying to play song using MPMusicPlayerController using the following code

let musicPlayerController = MPMusicPlayerController.systemMusicPlayer

func beginPlayback(itemID: String) {

   let descripter = MPMusicPlayerStoreQueueDescriptor(storeIDs: [itemID])
    descripter.startItemID = itemID

    musicPlayerController.setQueue(with: descripter)

    musicPlayerController.prepareToPlay(completionHandler: {(error : Error?) in
        print()
        self.musicPlayerController.play()
    })


}

However, in the console I frequently see

[MPMusicPlayerController prepareToPlay] timeout

and the either nothing happens or the player will start with the wrong song, probably something local from my device.

I'm trying to play one song from my Apple Music Library, I use their API to get my playlist, and then the songs, the track response looks like this:

attributes =     {
    albumName = Villains;
    artistName = "Queens of the Stone Age";
    artwork =         {
        height = 1200;
        url = "https://is3-ssl.mzstatic.com/image/thumb/Music117/v4/8b/19/86/8b19867d-f500-5396-bd6f-e89c0a69adcd/dj.tvdpbelw.jpg/{w}x{h}bb.jpg";
        width = 1200;
    };
    durationInMillis = 341825;
    name = "Feet Don\U0092't Fail Me";
    playParams =         {
        id = "i.B0VN4GGf7Eldk";
        isLibrary = 1;
        kind = song;
    };
    trackNumber = 1;
};
href = "/v1/me/library/songs/i.B0VN4GGf7Eldk";
id = "i.B0VN4GGf7Eldk";
type = "library-songs";

}

I'm using the "id" here as the itemID playback parameter to beginPlayback (i.B0VN4GGf7Eldk). Is that right or should it be something else? Trying to figure out why this won't play.

Edit:

Also seeing this in the console

[SDKPlayback] -[MPMusicPlayerController prepareToPlayWithCompletionHandler:] completed error: Error Domain=MPCPlayerRequestErrorDomain Code=1 "No commands provided." UserInfo={NSDebugDescription=No commands provided.} [SDKPlayback] -[MPMusicPlayerController play] completed error: Error Domain=MPCPlayerRequestErrorDomain Code=1000 "Failed to send command 0" UserInfo={NSDebugDescription=Failed to send command 0, NSUnderlyingError=0x1c8045b20 {Error Domain=MPCPlayerRequestErrorDomain Code=1000 "Failed to send command 0 (MRMediaRemoteCommandHandlerStatus = 1)" UserInfo={NSDebugDescription=Failed to send command 0 (MRMediaRemoteCommandHandlerStatus = 1), MPCPlayerErrorKeyMediaRemoteCommandHandlerStatus=1}}}

NickDK
  • 999
  • 10
  • 24
  • Is the error variable in the `prepareToPlay` callback set to anything? Try printing it out to see if there is an error object being created. – jeremyms May 20 '18 at 18:00
  • @jeremyms . The error is actually nil however, there are a bunch of errors in the console . Adding to the post. – NickDK May 21 '18 at 02:32
  • 1
    @NickDK I'm also stuck with this issue now. Did you ever find a solution to this? – Saru Oct 15 '18 at 17:16

1 Answers1

0

It turns out that for Library items, I shouldn't be using the web API. I should simply be just be accessing the library from the MediaPlayer API.

I can access my playlists with

let playlistQuery = MPMediaQuery.playlists()

then each item has a store ID

let playlist = playlistQuery.first
for item in playlist.items {
  print(item.playbackStoreId)
}

I can play the items with that ID.

NickDK
  • 999
  • 10
  • 24
  • 1
    Hey, but, what if you want to fetch it from web API for some reason? In that case, is there any way to map the song ID("i.jndvkjieubv2n32lkn") received from REST API with the ones from the device library("23423424121")? – Saru Oct 15 '18 at 16:50