在我的应用程序中,我有一个播放音乐的collectionViewCell,在collectionViewCell类中我制作了播放器变量
var player: AVAudioPlayer?
我也尝试过:
var player = AVAudioplayer()
我做了一个方法pauseMusic()
func pauseMusic (){
if player != nil {
if player!.playing == true{
player!.pause()
}
}
}
当我在UIBarButtonItem操作中从我的FirstViewController调用此pauseMusic()方法时,我的应用程序崩溃
@IBAction func pauseButton(sender: UIBarButtonItem) {
var colVW = CollectionViewCell()
colVW.pauseMusic()
}
我还上传了此屏幕截图,显示崩溃时的错误
http://nl.tinypic.com/r/t66zir/8
有人熟悉此次崩溃吗? 如果你能给我一些建议我真的很感激!
附加代码:
此func检查必须播放哪首歌曲,此功能位于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")
}
}