Ios AVAudioPlayer不';我不能在共享课堂上工作
我从一个singleton类调用了一个方法,该方法允许播放其他类的声音,但播放机不起作用,我尝试了在处理相同问题的帖子中提到的方法,但对我不起作用,下面是我的代码:Ios AVAudioPlayer不';我不能在共享课堂上工作,ios,swift,avaudioplayer,Ios,Swift,Avaudioplayer,我从一个singleton类调用了一个方法,该方法允许播放其他类的声音,但播放机不起作用,我尝试了在处理相同问题的帖子中提到的方法,但对我不起作用,下面是我的代码: import Foundation import AVFoundation import UIKit var soundPlayer = AVAudioPlayer() class MySingleton: NSObject, AVAudioPlayerDelegate { var timer = NSTimer() cl
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()
}
}
我试过var播放器:AVAudioPlayer!=无:AVAudioPlayer!=无,但不工作,
如何修复它?创建计时器时,需要在选择器名称的末尾添加一个冒号“:”,因为该方法指定了一个参数。看起来是这样的:
timer = NSTimer.scheduledTimerWithTimeInterval(0.6, target: self, selector: "repeatedSound:", userInfo: nil, repeats: true)
func repeatedSound(timer: NSTimer) {
// your other code goes here
}
}
而且,由于计时器调用的方法接收计时器本身的参数,因此必须如下指定:
timer = NSTimer.scheduledTimerWithTimeInterval(0.6, target: self, selector: "repeatedSound:", userInfo: nil, repeats: true)
func repeatedSound(timer: NSTimer) {
// your other code goes here
}
我没有尝试你的代码,但这应该会有所帮助