문제

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 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

도움이 되었습니까?

해결책 2

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top