我从一个单独的类中调用一个方法,允许从其他类播放声音,但是播放器不起作用,我尝试了在处理相同问题的帖子中提到的内容但它没有工作对我来说,这是我的代码:
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 player : AVAudioPlayer! = nil : AVAudioPlayer! = nil
但没有工作,
我该如何解决?
答案 0 :(得分:0)
You need to add a colon ":" to the end of the selector name when creating the timer since that method specifies a parameter. It looks like this:
timer = NSTimer.scheduledTimerWithTimeInterval(0.6, target: self, selector: "repeatedSound:", userInfo: nil, repeats: true)
}
And, since the method that the timer calls receives an argument of the timer itself, it must be specified like this:
func repeatedSound(timer: NSTimer) {
// your other code goes here
}
I did not try your code, but that should help.