【问题标题】:AVAudioRecorder records blank fileAVAudioRecorder 记录空白文件
【发布时间】:2013-10-02 16:12:00
【问题描述】:

以下是我的录制按钮方法的内容。它会创建所需的文件,但无论我录制多长时间,创建的文件始终是 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];
    }

【问题讨论】:

    标签: avfoundation avaudioplayer avaudiorecorder


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 解决了我的问题!谢谢!
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      如果录音没有嵌入到 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()
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-08
        • 1970-01-01
        相关资源
        最近更新 更多