Swift Can';无法使AVFoundation Pause()在CollectionView中工作

Swift Can';无法使AVFoundation Pause()在CollectionView中工作,swift,avfoundation,Swift,Avfoundation,在我的应用程序中,我有一个播放音乐的collectionViewCell,在collectionViewCell类中,我将player设置为变量 var player: AVAudioPlayer? 我还尝试: var player = AVAudioplayer() 我做了一个方法() 当我在UIBarButtonim操作中从我的FirstViewController调用此pauseMusic()方法时,我的应用程序崩溃 @IBAction func pauseButton(sende

在我的应用程序中,我有一个播放音乐的collectionViewCell,在collectionViewCell类中,我将player设置为变量

 var player: AVAudioPlayer?
我还尝试:

var player = AVAudioplayer()
我做了一个方法()

当我在UIBarButtonim操作中从我的FirstViewController调用此pauseMusic()方法时,我的应用程序崩溃

 @IBAction func pauseButton(sender: UIBarButtonItem) {

    var colVW = CollectionViewCell()
    colVW.pauseMusic()
}   
我还上传了这个屏幕截图,它显示了崩溃时的错误

有人熟悉这次事故吗? 如果你能给我一些建议,我将不胜感激

附加代码:

此函数检查必须播放的歌曲,此函数位于CollectionView中。由于此func与CollectionViewCell类之间的链接,我无法将AVPlayer移动到其他地方

func prepareAudios() {

   var songIndexString = (imageOutlet.titleLabel?.text)!
    //println(songIndexString)

    var songIndex = (songIndexString.toInt())!

    println("songIndex: \(songIndex)")

    if musicStopped == true {

        rateButton()
        println("empty")

    } else if var filePath = NSBundle.mainBundle().pathForResource(audioArray[songIndex], ofType: "mp3"){

        var filePathUrl = NSURL.fileURLWithPath(filePath)
        player = AVAudioPlayer(contentsOfURL: filePathUrl, error: nil)
        player!.play()

    } else {
        println("Path for audio file not found")
    }
}

在UICollectionView单元中使用AVPlayer实例可能不是一个好主意。您可以将它放在控制器或其他对象中,当单元中存在一些交互时,将使用这些对象。但是有一个球员听起来有点奇怪。这个问题可能是因为你的球员零分。您何时创建AVPlayer以及如何使用它。如果您能提供一些代码,那将很有帮助。@GeneratorOfOne我在CollectionViewCell中创建了AVPlayer,因为每个单元格播放的歌曲不同。第一个en secondViewController在导航栏中都有一个pause按钮,在pause按钮操作中,我调用CollectionView中的pause函数。我真的不知道怎么才能解决这个问题。。我在问题中添加了一些附加代码
func prepareAudios() {

   var songIndexString = (imageOutlet.titleLabel?.text)!
    //println(songIndexString)

    var songIndex = (songIndexString.toInt())!

    println("songIndex: \(songIndex)")

    if musicStopped == true {

        rateButton()
        println("empty")

    } else if var filePath = NSBundle.mainBundle().pathForResource(audioArray[songIndex], ofType: "mp3"){

        var filePathUrl = NSURL.fileURLWithPath(filePath)
        player = AVAudioPlayer(contentsOfURL: filePathUrl, error: nil)
        player!.play()

    } else {
        println("Path for audio file not found")
    }
}