MPMusicPlayer Error Domain=MPErrorDomain Code=4?

809 views Asked by At

I am having issue with Music player, most of the songs gives Error

Error Domain=MPErrorDomain Code=4

The testing device has Apple music subscription and the tracks gives error on the app they are working fine in Apple music app!

Here is the code:

let applicationMusicPlayer = MPMusicPlayerController.systemMusicPlayer()
 applicationMusicPlayer.setQueueWithStoreIDs([ID])

      if #available(iOS 10.1, *)
      {

        applicationMusicPlayer.prepareToPlay { (error) in

            if (error != nil)
            {
                print("[MUSIC PLAYER] Error preparing : \(String(describing: error))")
                return
            }else
            {
                self.start_timer();
                self.applicationMusicPlayer.play()
            }

        }

      }else
      //Play directly ios below version 10.1
      {
        self.applicationMusicPlayer.play()
      }

    }

But what I've tried, when the track gives this error, I went to Apple music player and played it from there its worked, then I came back to my app and play it from my app its worked also fine, so I need to go to Apple music app to play tracks not playing in my app to make them work in my app! That's so weird any idea why?

PS: the testing device has Apple music subscription

1

There are 1 answers

0
Bruno Fulber Wide On BEST ANSWER

I had some similar problems when adding songs to a playlist, solved it by using:

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) {
    // Code
}

i would play around with waiting a bit before or after preparing.

5 seconds may be too much, but you can start from there