EXC_BAD_ACCESS和MPMusicPlayerController

时间:2018-11-08 01:06:57

标签: ios swift xcode exc-bad-access mpmusicplayercontroller

我编写了一段时间未使用的应用程序。它始于Xcode 7.3.1,但在Xcode 8.3.3中运行良好。尝试在装有iOS 11.4.1的iPhone 8上运行EXC_BAD_ACCESS时,我得到了EXC_BAD_ACCESS(我希望我从未更新过iOS,但现在遇到了麻烦)。但是该应用程序仍可以在装有iOS 10.3.3的旧iPhone 5上正常运行。

我的Mac运行Sierra,并具有Xcode 8.3.3和9.2。我确实将适当的支持文件复制到每个Xcode应用程序中的正确文件夹中(这是我在本网站上学到的)。这个把戏已经有一段时间了!

我决定在Xcode 9.2下构建一个小的“ Hello World”应用程序,以简化操作。如果我删除下面的MPMusicPlayerController(),则该应用程序将安装并正常运行。但是,使用该代码,它会在self.myMPMusicPlayerController.playbackState == .stopped上崩溃。

有人可以提出任何建议吗?提前非常感谢您。

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

}

1 个答案:

答案 0 :(得分:1)

我尝试运行您的代码,很遗憾,我使用的是Xcode 10.1,但是在MPMusicPlayerController的初始化中出现编译错误,并显示以下错误: enter image description here

Apple's Documentation of the MPMusicPlayerController类中,您可以尝试使用
进行初始化 MPMusicPlayerController.systemMusicPlayerMPMusicPlayerController.applicationMusicPlayer取决于您的用例。我已经尝试过并在11.4.1上运行,但没有遇到任何崩溃。