当我停止 AVAudioPlayer 时出现错误 EXC_BAD_ACCESS

When I stop AVAudioPlayer I get an error EXC_BAD_ACCESS

我有一个正在播放音乐的 UIViewController 和一个列出歌曲的 UITableViewController。我说我离开PlayViewControllerAVAudioPlayer还会继续播放歌曲。我希望我在 UITableViewController 上 return 并选择另一首歌曲 AVAudioPlayer 将停止。我尝试了很多不同的方法,但我得到了同样的错误 EXC_BAD_ACCESS。我也让 AVAudioPlayer 像 weak var 我也让 PlayViewController is a public class 但我得到了同样的错误。我该如何解决这个问题?

        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "playMusic" {
            var playMVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicVC
            var currentIndPass = tableView.indexPathForSelectedRow()!.row
//            println("array item \(arrayItem)")
            playMVC.currentIndex = currentIndPass
            playMVC.arrayOfSongs = arrayItem
            searchController.active = false
//            println("array item \(arrayItem)")
            //test
            var secondPlayView = PlayFromSearchVC()
            if secondPlayView.audioPlayer.playing == true {
                secondPlayView.audioPlayer.stop()
                println("It is works")
            } else {
                println("It is not works")
            }
            //test
        } else if segue.identifier == "summer" {
//            println("array super filter and count \(arrayFilterOfNames) co \(arrayFilterOfNames.count)")
            var playVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayFromSearchVC
            var currentInt = tableView.indexPathForSelectedRow()!.row
            playVC.currentSongString = arrayFilterOfNames[currentInt]
//            playVC.currentIndex = currentInt
//            playVC.arrayOfSongs = arrayFilterOfNames // arrayFilterNames
            searchController.active = false
            //
            var secondPlayView = PlayFromSearchVC()
            if secondPlayView.audioPlayer.playing == true {
                secondPlayView.audioPlayer.stop()
                println("It is works")
            } else {
                println("It is not works")
            }
            //
        }
    }

AVAudioPlayer 是

    var audioPlayer = AVAudioPlayer()

不同的变体也是错误的

由于 AvAudioPlayer (secondPlayView) 很弱 property.It 当 PlayViewController 从 memory.Make 中移除时会消失 memory.Make 确保您的 AVAudioPlayer 实例已正确初始化或在您返回 PlayViewController 时引用相同的实例.

这段代码没有意义:

var secondPlayView = PlayFromSearchVC()
if secondPlayView.audioPlayer.playing == true {

第一行创建一个新的 PlayFromSearchVC 实例。由于之前不存在,所以第二行没有意义。这就像问一个刚出生的婴儿最喜欢的玩具。

您没有显示 PlayViewController class 的结构。 audioPlayer是怎么定义的?

不要使用可以关闭的视图控制器来管理声音播放。创建一个管理声音播放的单例对象。向声音管理器添加一个停止当前正在播放的任何声音的方法,以及开始播放新声音的方法。从任何需要播放声音的地方调用声音管理器。

当你尝试将 currentTime 设置为 AVAudioPlayer 项目但在你设置播放内容之前 -> 你肯定会得到这样的错误