AVAudioPlayerDelegate 不调用该方法

AVAudioPlayerDelegate doesn't call the method

这里是class里面的方法:

import UIKIt
import Foundation

class notMoving {
    var drumPlayerObject = drumPlayer()
    var fileManagerObject = fileManager1()
    let drumStrength = 1
    var bassStrength = 1
    var synthStrength = 1
    var indexToPlay: Int = 0

    // here we start the drum player.
    func startToPlay()  {
        fileManagerObject.clearPlayedListDrum(drumStrength, KeyNoteOfInstDrum: "C")
        if let indexToPlay = fileManager1().randomizeTheNextInstrument(fileManager1().drums, Strength: drumStrength, KeyNote: "C")  {
            fileManager1().drums[indexToPlay].4 = true
            self.indexToPlay = indexToPlay
        }
        let instrument = fileManager1().drums[self.indexToPlay].0
        let name = fileManager1().drums[self.indexToPlay].1
        let length = fileManager1().drums[self.indexToPlay].2
        let power = fileManager1().drums[self.indexToPlay].3
        let ifplayed = fileManager1().drums[self.indexToPlay].4
        let tempo = Double(fileManager1().drums[self.indexToPlay].5)
        let bridge: Bool = false
        let extention = fileManagerObject.extentionOfFile
        let loops = fileManager1().drumNumberOfLoops()

        drumPlayerObject.play(instrument, name: name, extentionOfFile: extention, 
            length: length, power: power, ifplayed: ifplayed, tempo: tempo, loops: 
            loops, bridge: bridge)

        fileManager1().clearPlayedListDrum(drumStrength, KeyNoteOfInstDrum: "C")
    }
}

这里是 drumPlayer 的 AVAudioPlayerDelegate 扩展 class。

extension drumPlayer : AVAudioPlayerDelegate {
    func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool) {
        println("finished playing \(flag)")
        var notMovingObject = notMoving()
        notMovingObject.startToPlay()
    }

    func audioPlayerDecodeErrorDidOccur(player: AVAudioPlayer!, error: NSError!) {
        println("\(error.localizedDescription)")
    }
}

但是audioPlayerDidFinishPlaying在文件是finished.It之后不调用startToPlay方法只是打印"finished playing true"

我做错了什么?

你的 notMovingObject 没有保留在任何地方,因此在程序退出后 audioPlayerDidFinishPlaying() 对象被释放。

我通过在播放器的 "func play" 中创建实例 self.notMovingObject = NotMoving() 找到了解决方案。所以只有在播放器启动后才会创建实例。