Search code examples
iosswiftxcodeexc-bad-accessmpmusicplayercontroller

EXC_BAD_ACCESS and MPMusicPlayerController


I wrote an app that I haven't worked on in awhile. It started in Xcode 7.3.1, but worked fine in Xcode 8.3.3. I'm getting EXC_BAD_ACCESS when I try to run it on an iPhone 8 with iOS 11.4.1 (I wish I never updated the iOS, but now I'm stuck). But the app still runs fine on my old iPhone 5 with iOS 10.3.3.

My Mac runs Sierra and has Xcode 8.3.3 and 9.2. I did copy the appropriate support files into the correct folder in each Xcode app (something I learned on this site). That trick had worked for awhile!

I decide to build a little "Hello World" app under Xcode 9.2 just to simplify things. If I remove the MPMusicPlayerController() below, the app installs and runs fine. But, with that code, it crashes on self.myMPMusicPlayerController.playbackState == .stopped.

Can anyone suggest anything? Thank you very much ahead of time.

import UIKit
import MediaPlayer

class ViewController: UIViewController {

@IBOutlet var myLabel: UILabel!
@IBOutlet var nowPlaying: UIButton!

var myMPMusicPlayerController = MPMusicPlayerController()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // The following is a tiny bit of an app
    // My mac is running Sierra so it can only take Xcode 8.3.3 or 9.2
    // I will be updating my mac at least to High Sierra soon, so it will be able to take Xcode 9.4.1
    // I have copied in the appropriate support file folders accordingly. So I have the 11.4 (15F79) folder in place

    // The following line, worked fine using Xcode 8.3.3 and an iPhone8 running 11.2 and an iPhone5 running 10.3.3
    // The following line, crashes on an iPhone8 running 11.4.1 but RUNS FINE on the iPhone5 running 10.3.3

    // I'm getting the error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x70)
    if self.myMPMusicPlayerController.playbackState == .stopped {

        // Hide the nowPlaying button.
        nowPlaying.isHidden = true
    } else {

        // Show the nowPlaying button.
        nowPlaying.isHidden = false
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// I added this just for a simple test
@IBAction func nowPlayingButton(_ sender: Any) {
    self.myLabel.text = "Hello"
}

}

Solution

  • I tried to run your code, unfortunately I am on Xcode 10.1 but I am having a compile error in the initialization of MPMusicPlayerController and shows the following error: enter image description here

    From Apple's Documentation of the MPMusicPlayerController class you may try initializing with
    MPMusicPlayerController.systemMusicPlayer or MPMusicPlayerController.applicationMusicPlayer depending on your use case. I have tried this and ran on 11.4.1 and didn't run into any crashes.