Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPMusicPlayer Error Domain=MPErrorDomain Code=4?

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

like image 853
Khodour.F Avatar asked Sep 04 '17 09:09

Khodour.F


Video Answer


1 Answers

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

like image 89
Bruno Fulber Wide Avatar answered Oct 05 '22 12:10

Bruno Fulber Wide