2013-10-02 61 views
2

以下是我的記錄按鈕方法的內容。它會創建所需的文件,但無論記錄多長時間,創建的文件總是4kb和0秒,我無法弄清楚它爲什麼無效。 averagePowerPerChannel也會返回-120,我假設它是因爲文件已損壞。AVAudioRecorder記錄空白文件

NSDictionary *recordSettings = [NSDictionary 
            dictionaryWithObjectsAndKeys: 
            [NSNumber numberWithInt:AVAudioQualityMin], 
            AVEncoderAudioQualityKey, 
            [NSNumber numberWithInt:16], 
            AVEncoderBitRateKey, 
            [NSNumber numberWithInt: 2], 
            AVNumberOfChannelsKey, 
            [NSNumber numberWithFloat:44100.0], 
            AVSampleRateKey, 
            nil]; 
    NSError *error; 
    NSArray *dirPaths; 
    NSString *docsDir; 

    dirPaths = NSSearchPathForDirectoriesInDomains(
                NSDocumentDirectory, NSUserDomainMask, YES); 
    docsDir = [dirPaths objectAtIndex:0]; 
    NSString *soundFilePath = [docsDir 
           stringByAppendingPathComponent:@"sound.caf"]; 

    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

    self.shotRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&error]; 
    self.shotRecorder.delegate = self; 

    if (error) 
    { 
     NSLog(@"error: %@", [error localizedDescription]); 

    } else { 
     [self.shotRecorder prepareToRecord]; 
     self.shotRecorder.meteringEnabled = YES; 
     [self.shotRecorder record]; 
     shotTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES]; 
    } 

回答

4

在開始拍攝前,您的應用程式AudioSession設置爲支持記錄會話類型之一。例如,在開始錄製之前調用此:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error]; 

如果你試圖做設置爲不支持記錄類型的AudioSession一個記錄,該代碼會出現執行罰款,但會導致您所描述的空的4kb音頻文件。

要稍後播放文件,請務必將類別重新設置爲支持播放的類別,例如AVAudioSessionCategoryPlayback或AVAudioSessionCategoryPlayAndRecord。

+0

解決了我的問題!謝謝! – nexuzzz