我尝试在Swift中写“当用户吹入麦克风时检测”并且我收到此错误:“无法识别的选择器发送到实例0x78737d70” 这是我的代码:
import Foundation
import UIKit
import AVFoundation
import CoreAudio
class ViewController: UIViewController {
// @IBOutlet weak var mainImage: UIImageView!
var recorder: AVAudioRecorder!
var levelTimer = NSTimer()
var lowPassResults: Double = 0.0
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL.fileURLWithPath("dev/null")
//numbers are automatically wrapped into NSNumber objects, so I simplified that to [NSString : NSNumber]
var settings : [NSString : NSNumber] = [AVSampleRateKey: 44100.0, AVFormatIDKey: kAudioFormatAppleLossless, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey: AVAudioQuality.Max.rawValue]
var error: NSError?
// mainImage?.image = UIImage(named: "flyForReal.png");
recorder = AVAudioRecorder(URL:url, settings:settings, error:&error)
/*
sending prepareToRecord message -> activate metering -> recording -> NSTimer object that fires once ever 0.03 seconds repeatedly. Every time it fires, it sends a message to the listenForBlow function
*/
if((recorder) != nil){
recorder.prepareToRecord()
recorder.meteringEnabled = true
recorder.record()
levelTimer = NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: Selector("levelTimerCallback"), userInfo: nil, repeats: true)
}
else{
NSLog("%@", "Error");
}
}
func levelTimerCallback(timer:NSTimer) {
recorder.updateMeters()
let ALPHA: Double = 0.05
var peakPowerForChannel = pow(Double(10), (0.05 * Double(recorder.peakPowerForChannel(0))))
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
if(lowPassResults > 0.95){
NSLog("@Mic blow detected");
}
NSLog("@Average input: %f Peak input: %f Low pass results: %f", recorder.averagePowerForChannel(0), recorder.peakPowerForChannel(0), lowPassResults);
}
}
我不知道如何修复它。谢谢你!
这些是我得到的错误:
2015-07-05 13:22:11.210 Reaktion[7797:471551] -[Reaktion.ViewController levelTimerCallback]: unrecognized selector sent to instance 0x78737d70
2015-07-05 13:22:11.219 Reaktion[7797:471551] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [Reaktion.ViewController levelTimerCallback]: unrecognized selector sent to instance 0x78737d70'
*** First throw call stack:
(
0 CoreFoundation 0x00586746 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x01f87a97 objc_exception_throw + 44
2 CoreFoundation 0x0058e705 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x004d5287 ___forwarding___ + 1047
4 CoreFoundation 0x004d4e4e _CF_forwarding_prep_0 + 14
5 Foundation 0x0098d6d9 __NSFireTimer + 97
6 CoreFoundation 0x004df866 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
7 CoreFoundation 0x004df1ed __CFRunLoopDoTimer + 1309
8 CoreFoundation 0x0049d54a __CFRunLoopRun + 2090
9 CoreFoundation 0x0049ca5b CFRunLoopRunSpecific + 443
10 CoreFoundation 0x0049c88b CFRunLoopRunInMode + 123
11 GraphicsServices 0x03d172c9 GSEventRunModal + 192
12 GraphicsServices 0x03d17106 GSEventRun + 104
13 UIKit 0x00daa106 UIApplicationMain + 1526
14 Reaktion 0x00071034 main + 180
15 libdyld.dylib 0x04911ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
答案 0 :(得分:0)
函数levelTimerCallback有一个参数,所以选择器必须在末尾有一个冒号
selector: "levelTimerCallback:"
不需要Selector
初始化程序