Swift: AudioPlayerDidFinished will not be called

二次信任 提交于 2021-01-28 07:30:44

问题


My AudioPlayerDidFinishPlaying will not be called after audio has finished. I know it has something to do with my delegate, but I can't fix it on my own.

Can somebody give me some tips? I Googled a lot and I found other questions here with the same issue but it didn't work for me.

Thanks

import UIKit
import Parse
import AVFoundation

class ViewControllerMies: UIViewController, AVAudioPlayerDelegate {

var timer = NSTimer()
var player: AVAudioPlayer = AVAudioPlayer()
var currentStateAudio = ""
var oldAudio = String()

func startTimer() {

    if player.playing == false {
        print("Tijd voor de Spice Girls")
    }

    let query = PFQuery(className:"CurrentState")
    query.getObjectInBackgroundWithId("9r61TRaRqu") {
        (objects: PFObject?, error: NSError?) -> Void in
        if error == nil && objects != nil {

            self.currentStateAudio = objects!.objectForKey("currentState") as! String
            print(self.currentStateAudio)

        } else {
            print(error)
        }
    }

    if (oldAudio != self.currentStateAudio)
    {

        let audioPath = NSBundle.mainBundle().pathForResource(self.currentStateAudio, ofType: "mp3")!

        do {

            try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath))


        } catch {

            // Process error here

        }

        player.play()

        oldAudio = self.currentStateAudio

    }

}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("startTimer"), userInfo: nil, repeats: true)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool)
{
    print("Finished Playing")

}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

回答1:


you're not setting yourself as the player's delegate

before calling play() do player.delegate = self




回答2:


if you are using private delegate then its will not call too use this for resolving this issue

//MARK:- This is correct delegate working fine (use this)
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
    audioRecorder = nil
}
//MARK:- Insted of this (this will not call)
private func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
    audioRecorder = nil
}


来源:https://stackoverflow.com/questions/33707462/swift-audioplayerdidfinished-will-not-be-called

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