views:

213

answers:

2

Hello all,

I'm trying to make a simple iphone app that has two buttons: Start and Stop. When you hit start it starts listening to the phone's microphone volume and makes label show how loud you're talking into the mic. When you hit stop, it stops listening.

I found a great class called SCListener, but I'm not sure how to get it implemented. Right now i've got a button hooked up to the following code

-(IBAction)getVolume
{
    SCListener *listener = [SCListener sharedListener];
    [listener listen];
    Float32 peakPower = [listener peakPower];
    NSString *theString = [NSString stringWithFormat:@"Level is: %1.2f", peakPower];
    [volumeLabel setText:theString];
}

This works perfectly well, but only gets the volume once. I've tried to do something like:

[listener addObserver:self 
           forKeyPath:@"peakPower"               
              options:NSKeyValueObservingOptionOld 
              context:NULL];

But since peakPower is a method not a variable I can't put an observer on it. How would I go about using a class like SCListener to set up an app that allows the user to press a button and have the label constantly update?

Thanks, JP

A: 

The reason KVO doesn't work has nothing to do with the fact that it's "not a variable." KVO observes keys, not variables. It's entirely possible to observe a property with no underlying variable. The reason it won't work in this case is that SCListener doesn't emit KVO notifications.

The simplest solution: Decide on a resolution you want and have a timer poll your SCListener at that rate.

Chuck
A: 

Chuck said

The simplest solution: Decide on a resolution you want and have a timer poll your SCListener at that rate

Agreed. The more complex solutoin: make SCListener KVO compliant after reading Apple's docs and contribute the results back to the community. It will make your code cleaner, teach you how KVO works and solve other people's problems with SCListener.

http://stackoverflow.com/questions/625225/monitor-iphone-mic http://stackoverflow.com/questions/477020/iphone-mic-volume

Roger Nolan