AVAudioPlayerDelegate不调用该方法

时间:2015-02-28 12:27:10

标签: ios swift delegates audio-player

以下是类中的方法:

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扩展。

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在文件结束后没有调用startToPlay方法。它只是打印"播放完毕"

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您的notMovingObject未保留在任何位置,因此在程序退出audioPlayerDidFinishPlaying()对象被取消分配后。

答案 1 :(得分:0)

我通过在" func play"中创建实例self.notMovingObject = NotMoving()找到了解决方案。我的球员因此,只有在玩家启动后才会创建实例。