5

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

3 回答 3

6

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

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

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

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

于 2013-12-08T03:31:43.263 回答
1

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

于 2018-04-11T17:32:25.770 回答
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()
    }
于 2020-10-20T16:56:50.230 回答