Question

I'm trying to make a small sound measuring app in xcode for an iPhone and I've been using the following tutorial to activate the microphone

http://mobileorchard.com/tutorial-detecting-when-a-user-blows-into-the-mic/

I want to pull out the input as a number and display it in a label in the app. How do I go about doing this?

I've tried creating an IBOutlet and the header like so:

@property (weak,nonatomic) IBOutlet UILabel *myLabel;

and I've tried including the _mylabel part in the following code:

if (recorder) {
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;
    [recorder record];
    levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector:      @selector(levelTimerCallback:) userInfo: nil repeats: YES];
    _myLabel.text = [[NSString alloc] initWithFormat:@"%@", recorder];**
} else
    NSLog([error description]);

This is the code for the levelTimerCallback

- (void)levelTimerCallback:(NSTimer *)timer {
[recorder updateMeters];
NSLog(@"Average input: %f Peak input: %f", [recorder averagePowerForChannel:0], [recorder peakPowerForChannel:0]);}

Is this complete nonsense?

When I run the code, the label I've attached the code to displays the message <AVAudioRecorder: 0x7d2b00>. I did however notice that my terminal inside xcode begins displaying the average input and peak input for the microphone. But how do I get that data into the label?

Was it helpful?

Solution

You should do it like,

- (void)levelTimerCallback:(NSTimer *)timer 
{
    [recorder updateMeters];
    NSLog(@"Average input: %f Peak input: %f", [recorder averagePowerForChannel:0], [recorder peakPowerForChannel:0]);
    _myLabel.text = [NSString stringWithFormat@"Average input: %f Peak input: %f",[recorder averagePowerForChannel:0], [recorder peakPowerForChannel:0]];
}

See above _myLabel.text is to be updated in your method.

Hope this helps.

EDIT

 _myLabel.text = [NSString stringWithFormat@"%f",[recorder averagePowerForChannel:0]]; //Only average input
 _myLabel.text = [NSString stringWithFormat@"%f",[recorder peakPowerForChannel:0]]; //Only peak input
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top