Swift:不会调用AudioPlayerDidFinished [英] Swift: AudioPlayerDidFinished will not be called

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

问题描述

音频结束后,将不会调用我的AudioPlayerDidFinishPlaying.我知道这与我的代表有关,但我不能自己解决.

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.

有人可以给我一些提示吗?我在Google上搜索了很多东西,但在这里发现了与该问题相同的其他问题,但这对我不起作用.

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.

谢谢

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.
}
*/

}

推荐答案

您没有将自己设置为玩家的代表

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

在调用 play()之前先执行 player.delegate = self

这篇关于Swift:不会调用AudioPlayerDidFinished的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆