开发者

Differences in behavior of AVAudioRecorder in iOS 4.1 and iOS3.1.3

开发者 https://www.devze.com 2023-01-21 16:16 出处:网络
I want to play an audio when someone speaks in the iPhone\'s mic. This code works perfectly in iPhone 2G(OS 3.1.3), but when it开发者_StackOverflow中文版 is tested in iPhone 4, it doesnt work. I check

I want to play an audio when someone speaks in the iPhone's mic. This code works perfectly in iPhone 2G(OS 3.1.3), but when it开发者_StackOverflow中文版 is tested in iPhone 4, it doesnt work. I checked for API and method differences, but could not find any.

- (void)viewDidLoad {

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Audio.mp3", [[NSBundle mainBundle] resourcePath]]];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.delegate=self;
if (audioPlayer == nil)
    NSLog(@" %@ ", [error description]);

audioSession = [[AVAudioSession sharedInstance] retain];
[audioSession setActive:YES error: nil];


[super viewDidLoad]; }

The action event:

-(IBAction) micAction{

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                          [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                          [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                          [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                          nil];

NSError *error;

recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

if (recorder) {
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;
    [recorder record];
    levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: NO];
} }

- (void)levelTimerCallback:(NSTimer *)t {
[recorder updateMeters];
if([recorder averagePowerForChannel:0]>-38){
    if ([audioPlayer prepareToPlay]) {
        [recorder stop];
        [recorder release];
        [levelTimer invalidate];
        [self playSound];   
    }
}}  

-(void)playSound{
[audioSession setCategory:AVAudioSessionCategoryPlayback error: nil];
if ([audioPlayer prepareToPlay])
    [audioPlayer play]; }

To make sure that the audio is playing in iPhone 4, i'd also set up an IBAction event, which is working fine. Also, for testing I'd set up a label to display the value of

[recorder averagePowerForChannel:0

after the button is pressed. This label updates itself as per the timer in 2G, but in iPhone 4 it displays just one value on the click event.

Am unable to debug the behavior of the method in iPhone 4 since i dont have the device with me now.

Can anyone please point out what the error is?

Thanks


Have you tried saving to a file instead of to /dev/null?


Found the solution. Issue was in this line:

levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: NO];

Since repeats was set to NO, the timer used to fire only once. So, the continuos monitoring of the averagePowerForChannel value could not be done. After setting it to YES, the code works perfectly.

0

精彩评论

暂无评论...
验证码 换一张
取 消