ios - EXC_BAD_ACCESS 和 MPMusicPlayerController

标签 ios swift xcode exc-bad-access mpmusicplayercontroller

我编写了一个应用程序,但我已经有一段时间没有开发它了。它在 Xcode 7.3.1 中启动,但在 Xcode 8.3.3 中运行良好。当我尝试在装有 iOS 11.4.1 的 iPhone 8 上运行它时,我得到了 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"
}

}

最佳答案

我尝试运行您的代码,不幸的是我在 Xcode 10.1 上,但我在 MPMusicPlayerController 初始化时遇到编译错误并显示以下错误: enter image description here

来自 Apple's Documentation of the MPMusicPlayerController你可以尝试用
初始化的类 MPMusicPlayerController.systemMusicPlayerMPMusicPlayerController.applicationMusicPlayer 取决于您的用例。我试过这个并在 11.4.1 上运行并且没有遇到任何崩溃。

关于ios - EXC_BAD_ACCESS 和 MPMusicPlayerController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53200179/

相关文章:

ios - PFQueryTableView 在新数据更新时自动刷新或使用 Parse 每分钟刷新一次

ios - 从 applicationwillresignactive 中调用函数

swift - 为什么 UITextview 的交互部分需要长按?

Xcode 12.3 : Failed to re-create a coordinator for com. me.myapp

ios - 将现有类与 Storyboard 相关联

ios - Xamarin iOS WKWebView 不显示谷歌recaptcha

ios - 聚类标识符设置为 nil 的标记注释聚类

javascript - Phonegap - Xcode 中的 Javascript 调试

ios - 为什么我的 CLLocationManager 没有响应 startMonitoringForRegion?

ios - Xcode 自动化 UI 测试日志内存使用