0
votes

I tried to write "Detecting When A User Blows Into The Mic" in Swift and I am receiving this error: "unrecognized selector sent to instance 0x78737d70" This is my code:

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);
 }
}

I don't know how to fix it. Thanks ahead!

These are the errors I get:

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) 
1
Which line is it breaking on? Add an exception breakpoint if you don't know. Also, what is the rest of the error?Fogmeister
There is a colon missing in the selector. I cannot search for it on the phone, but this must have been answered before.Martin R

1 Answers

0
votes

the function levelTimerCallback has an argument so the selector must have a colon at the end

selector: "levelTimerCallback:"

the Selector initializer is not needed