AVAudioRecorder记录空白文件

时间:2013-10-02 16:12:00

标签: avfoundation avaudioplayer avaudiorecorder

以下是我的记录按钮方法的内容。它创建了所需的文件,但无论我记录多长时间,创建的文件总是4kb和0秒,我无法弄清楚它为什么无效。对于averagePowerPerChannel,计量也返回-120,我假设它是因为文件已损坏。

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];
    }

3 个答案:

答案 0 :(得分:6)

在开始录制之前,请将应用的AudioSession设置为支持录制的会话类型之一。例如,在开始录制之前调用它:

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

如果您尝试将AudioSession设置为不支持录制的类型进行录制,则代码似乎执行正常,但会导致您描述的空4kb音频文件。

要稍后播放该文件,请务必将类别设置回支持播放的类别,例如AVAudioSessionCategoryPlayback或AVAudioSessionCategoryPlayAndRecord。

答案 1 :(得分:1)

当应用没有麦克风权限时,AVRecorder也会录制一个空文件。

答案 2 :(得分:0)

如果录音未嵌入在audioSession()中,则会发生这种情况。

这是我在希望能够播放和录制音频,但相应地限制设置的情况下解决类似情况的方法。 委托允许检测“播放结束”等(未显示)。

import AVKit

class ViewController: UIViewController, AVAudioRecorderDelegate {
   var audioPlaybackSession: AVAudioSession!
   var audioRecordAndPlaybackSession: AVAudioSession!
   var audioRecorder : AVAudioRecorder!
}
    
   override func viewDidLoad() {
       super.viewDidLoad()
       let audioPlaybackSession = AVAudioSession.sharedInstance()
            do {
               try audioPlaybackSession.setCategory(.playback, 
        mode: .default, options: [])
               } catch {
                    print("Failed to set audio session category.")
               }
        }
        
   func playback() {
       let audioPlaybackSession = AVAudioSession.sharedInstance()
            do {
               try audioPlaybackSession.setCategory(.playback, mode: .default, options: [])
             } catch {
                 print("Failed to set audio session category.")
             }
        ...
        audioRecorder.play()    
   }
            
   func record() {
      let audioPlaybackSession = AVAudioSession.sharedInstance()
           do {
              try audioPlaybackSession.setCategory(.playback, mode: .default, options: [])
           } catch {
               print("Failed to set audio session category.")
            }
      ...
      audioRecorder.delegate = self
      audioRecorder.prepareToRecord()
      audioPlayer.record()
    }