簡體   English   中英

AVAudioRecorder Record方法在隨機時間返回NO

[英]AVAudioRecorder Record method returns NO at random times

我目前正在開發一款支持應用內錄音的應用。 用戶可以獲得他/她已經通過應用程序記錄的本地保存的音頻文件的標准表格視圖,或者按下按鈕轉到新的錄制視圖,從那里可以錄制新的音頻會話,這將自動獲得保存到應用程序沙箱。 現在這個功能在大多數情況下運行良好。 我可以錄制新文件並從應用程序播放它們,但隨機時間錄音機將無法以此方法開始錄制:

-(void) startRecording
{
if (!isRecording)
{
    isRecording = YES;

    BOOL recordingSucess = [audioRecorder record];

    if (recordingSucess)
        NSLog(@"WE STARTED THE RECORDING!");
    else
        NSLog(@"WE DID NOT START THE RECORDING!");

    // Start the timer
    recordTiming = [NSDate date];

    timeCheck = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timeCheck) userInfo:nil repeats:YES]
}

也就是說,NSLog將打印出“我們沒有開始錄制”,表示它無法開始錄制。 這導致仍然將空音頻文件保存到應用程序,文件大小通常為4096字節。 這是錄音機對象的初始化方式:

-(id) initWithContentURL:(NSURL *)contentURL
{
self = [super init];

if (self)
{
    NSLog(@"Initializing audio recorder!");

    // Specific settings for the audio recording
    NSDictionary *recordSettings = [NSDictionary 
                                    dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:8], 
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt: 2], 
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:22000.0], 
                                    AVSampleRateKey,
                                    nil];

    NSError *error = nil;

    audioRecorder = [[AVAudioRecorder alloc] initWithURL:contentURL settings:recordSettings error:&error];
    audioRecorder.delegate = self;
}

還有NSLog檢查,它檢查錯誤是否為零,所以此時我確信初始化似乎沒有失敗。 什么可能導致記錄方法在看似隨機的時間失敗,而在大多數其他時間它的效果非常好?

在錄制開始之前,應初始化音頻會話:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err){
   NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
   return;
}
err = nil;
[audioSession setActive:YES error:&err];
if(err){
   NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
   return;
}

請參閱接受的答案以供參考。

contentURL是否始終相同,或者每次都生成一個新的?

您也可以嘗試在記錄之前顯式調用prepareToRecord並檢查它是否成功?

暫無
暫無

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

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