AVAudioPlayerDelegate doesn't call the method

随声附和 提交于 2020-06-17 09:23:45

问题


Here is the method inside a 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")
    }
}

And here is AVAudioPlayerDelegate extension for a drumPlayer 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)")
    }
}

But audioPlayerDidFinishPlaying doesn't call the startToPlay method after the file is finished.It just only prints "finished playing true"

What I'm doing wrong?


回答1:


Your notMovingObject is not retained anywhere, thus after program exits audioPlayerDidFinishPlaying() object is deallocated.




回答2:


I found a solution by creating an instance self.notMovingObject = NotMoving() inside the "func play" of my player. So the instance is created only after the player starts.



来源:https://stackoverflow.com/questions/28781684/avaudioplayerdelegate-doesnt-call-the-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!