gpt4 book ai didi

ios - AVAudioPlayer 无法在共享类中快速工作

转载 作者:行者123 更新时间:2023-11-30 13:51:45 25 4
gpt4 key购买 nike

我从单例类中调用一个方法,该方法允许播放其他类的声音,但播放器不起作用,我尝试了处理相同问题的帖子中提到的内容,但它对我不起作用,在这里是我的代码:

import Foundation
import AVFoundation
import UIKit
var soundPlayer = AVAudioPlayer()

class MySingleton: NSObject, AVAudioPlayerDelegate {
var timer = NSTimer()

class var sharedSingleton: MySingleton {
struct Static {
static var onceTocken: dispatch_once_t = 0
static var instance : MySingleton? = nil
}
dispatch_once(&Static.onceTocken) {
Static.instance = MySingleton()
}
return Static.instance!
}
func callTimer () {
timer = NSTimer.scheduledTimerWithTimeInterval(0.6, target: self, selector: "repeatedSound", userInfo: nil, repeats: true)
}
func repeatedSound() {

var repeatedSoundUrl = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(prayerRepitationList[selectedCellInIndex], ofType: "mp3")!)

soundPlayer = AVAudioPlayer(contentsOfURL: repeatedSoundUrl, error: nil)
println("repeated url is \(repeatedSoundUrl)")
soundPlayer.prepareToPlay()
soundPlayer.delegate = self
soundPlayer.play()


}
}

我尝试了varplayer:AVAudioPlayer! =零:AVAudioPlayer! = nil 但不起作用,我该如何修复它?

最佳答案

创建计时器时,您需要在选择器名称末尾添加冒号“:”,因为该方法指定了一个参数。它看起来像这样:

timer = NSTimer.scheduledTimerWithTimeInterval(0.6, target: self, selector: "repeatedSound:", userInfo: nil, repeats: true)

}

并且,由于计时器调用的方法接收计时器本身的参数,因此必须如下指定:

func repeatedSound(timer: NSTimer) {
// your other code goes here
}

我没有尝试你的代码,但这应该有帮助。

关于ios - AVAudioPlayer 无法在共享类中快速工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34187698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com