AVAudioPlayerDelegate不调用该方法

AVAudioPlayerDelegate不调用该方法

本文介绍了AVAudioPlayerDelegate不调用该方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个类内的方法:

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

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)")
    }
}

但是audioPlayerDidFinishPlaying在文件完成后不会调用startToPlay方法.它只会打印"finished play true"(完成播放为真)

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

我做错了什么?

推荐答案

我通过在播放器的功能播放"中创建实例self.notMovingObject = NotMoving()找到了解决方案.因此,仅在播放器启动后才创建实例.

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.

这篇关于AVAudioPlayerDelegate不调用该方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:35