簡體   English   中英

iOS - 使用AVAudioRecorder錄制音頻失敗,沒有錯誤

[英]iOS - Recording audio with AVAudioRecorder fail with no error

我遇到這個問題,AVAudioRecorder不能為我工作,至少從我能看到的(或聽不到)。

我用ARC瞄准iOS 5。

我確實設置了錯誤對象,但沒有一個被解雇,所以我想我在這里做錯了。

這是我設置AVAudioRecorder的部分:

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:currentTrack.trackFileName];

currentTrack.trackFileURL = [NSURL fileURLWithPath:soundFilePath];

NSLog(@"Chemin : %@", currentTrack.trackFileURL.path);

NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];


currentTrack.trackRecordSettings = recordSetting;


NSError *err = nil;


//[audioSession setDelegate:self];
if(err){
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
}
[[AVAudioSession sharedInstance] setActive:YES error:&err];
err = nil;
if(err){
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
}
err = nil;
recorder = [[AVAudioRecorder alloc]
                 initWithURL:currentTrack.trackFileURL
                 settings:currentTrack.trackRecordSettings
                 error:&error];

if (err)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning"
                               message: [err localizedDescription]
                              delegate: nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [alert show];
    NSLog(@"error: %@", [error localizedDescription]);

} else {
    [recorder setDelegate:self];
    [recorder prepareToRecord];

    BOOL audioHWAvailable = [[AVAudioSession sharedInstance]inputIsAvailable ];
    if (!audioHWAvailable) {
        UIAlertView *cantRecordAlert =
        [[UIAlertView alloc] initWithTitle: @"Warning"
                                   message: @"Audio input hardware not available"
                                  delegate: nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [cantRecordAlert show]; 
        return;
    }
}

這里沒有錯誤

然后,當它開始錄制時:

-(void)startRecordingAudio{
    NSLog(@"Start recording");
    [[AVAudioSession sharedInstance] setCategory :AVAudioSessionCategoryRecord error:nil];
    [recorder record];
}

然后,當是時候停止錄制:

-(void)stopRecordingAudio{
    NSLog(@"Stop recording");
    [recorder stop];
    NSError *err;
    NSData *audioData = [NSData dataWithContentsOfFile:currentTrack.trackFileURL.path options: 0 error:&err];
    if(!audioData)
        NSLog(@"audio data error: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    NSLog(@"%d", audioData.length);
    }

audioData長度總是4096是正常的嗎? 如果我理解正確的話,那是大約93毫秒的聲音......

最后,當播放錄制的聲音時:

-(void)startPlayingAudio{
    [[AVAudioSession sharedInstance] setCategory :AVAudioSessionCategoryPlayback error:nil];

    NSLog(@"Start playing");
    NSError *error;
    if(player == nil){
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:currentTrack.trackFileURL    
        error:&error];                                                                           
    }


    player.delegate = self;

    if (error)
        NSLog(@"Error: %@", 
              [error localizedDescription]);
    else
        [player  play];
}

我確實設置了從不開火的委托方法:

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
   NSLog (@"audioRecorderDidFinishRecording:successfully:");
}
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
    NSLog(@"Decode Error occurred");
}
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:    (BOOL)flag
{
    NSLog (@"audioRecorderDidFinishRecording:successfully: %d", flag);
}
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error
{
    NSLog(@"Encode Error occurred");
}
-(void)beginInterruption{
    NSLog(@"INTERRUPT");
}

此外,我確實看到.caf文件是在我的應用程序文檔文件夾中正確創建的。

感謝您提供的任何幫助。 至於現在,我聽不到錄制的聲音,我使用沒有耳機的iPhone 4設備進行測試。

發現問題:在prepareRecord之前調用了記錄。 由於我使用viewDidAppear來設置錄制,因此在視圖可見之前進行了開始錄制的調用...

我確實使用了viewDidAppear,因為我沒有調用viewDidLoad,因為我不使用initWithNib。 我試圖覆蓋loadView,但我找到的所有示例都不清楚主題,並且從不調用loadView。 也許有人可以指出我正確的方向? 盡管如此,我可以在iphone上聽到我的聲音!

謝謝你的時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM