如何在customView類中使用getLevel()的值?該功能與NSTimer相關聯。該值是一個mp3文件的分貝值,我想用內部customView與動畫在不同的類中使用函數()變量 - Swift
class customView: UIView {
// get getLevels() value
}
class ViewController: UIViewController, AVAudioPlayerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
var bundle = NSString(string: NSBundle.mainBundle().pathForResource("guitar", ofType: "mp3")!)
var error: NSError? = nil
player = AVAudioPlayer(contentsOfURL: NSURL(string: bundle), error: &error)
player.delegate = self
player.volume = 1.0
player.meteringEnabled = true
player.play()
var timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("getLevel"), userInfo: nil, repeats: true)
}
func getLevel() {
if (player.playing == true) {
player.updateMeters()
var dblevel = player.averagePowerForChannel(1)
var percentage = CGFloat(pow(10, (0.05 * dblevel)) * 100)
// This prints out the the decibel levels from 0.0 to 100.0 every 0.1 sec
println(percentage)
}
}
我需要的變量是「百分比」同步的分貝值。我有什麼選擇可以做到這一點?感謝Godart。
感謝didSet {}這份工作! –