gpt4 book ai didi

ios - AVAudioRecorder 不保存录音

转载 作者:行者123 更新时间:2023-11-29 12:27:56 25 4
gpt4 key购买 nike

我正在制作 iOS 游戏。我需要做的一件事是允许用户进行快速录音。这一切都有效,但录音只是暂时保存。因此,当用户关闭应用程序并重新打开它时,录音应该能够再次播放,但它没有,当我关闭应用程序时它被删除了。我不明白我做错了什么。下面是我的代码:

我在 ViewDidLoad 方法中设置了 AVAudioRecorder,如下所示:

// Setup audio recorder to save file.
NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], @"MyAudioMemo.m4a", nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

// Setup audio session.
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];

audio_recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
audio_recorder.delegate = self;
audio_recorder.meteringEnabled = YES;
[audio_recorder prepareToRecord];

我也有 AVAudio 委托(delegate)方法:

-(void)audio_playerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
NSLog(@"Did finish playing: %d", flag);
}

-(void)audio_playerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error {
NSLog(@"Decode Error occurred");
}

-(void)audio_recorderDidFinishRecording:(AVAudioPlayer *)recorder successfully:(BOOL)flag {
NSLog(@"Did finish recording: %d", flag);
}

-(void)audio_recorderEncodeErrorDidOccur:(AVAudioPlayer *)recorder error:(NSError *)error {
NSLog(@"Encode Error occurred");
}

当我想播放、录制或停止音频时,我做了以下链接到 UIButton 的 IBAction:

-(IBAction)play_audio {

NSLog(@"Play");

if (!audio_recorder.recording){
audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL:audio_recorder.url error:nil];
[audio_player setDelegate:self];
[audio_player play];
}
}

-(IBAction)record_voice {

NSLog(@"Record");

if (!audio_recorder.recording) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];

// Start recording.
[audio_recorder record];
}

else {
// Pause recording.
[audio_recorder pause];
}
}

-(IBAction)stop_audio {

NSLog(@"Stop");

[audio_recorder stop];

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
}

如果您尝试我的代码,您会发现它可以工作,但它似乎只是暂时保存音频文件。

我做错了什么?我以为我已经使用了所有正确的 AVAudioRecorder 方法?

最佳答案

要制作工作录音机并保存录音文件,您需要:

  1. 创建一个新的 Audio Session
  2. 确保麦克风已连接/工作
  3. 开始录制
  4. 停止录音
  5. 保存录制的音频文件
  6. 播放保存的语音文件

您的代码中缺少第 5 步,因此刚刚录制的文件仍然可供您播放,但是一旦您关闭应用程序,因为它没有保存到应用程序目录中某处的实际文件中,您会丢失它。您应该添加一个方法来将录制的音频保存到一个文件中,以便您以后可以随时访问它:

-(void) saveAudioFileNamed:(NSString *)filename {

destinationString = [[self documentsPath] stringByAppendingPathComponent:filename];
NSLog(@"%@", destinationString);
NSURL *destinationURL = [NSURL fileURLWithPath: destinationString];

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];

NSError *error;

audio_recorder = [[AVAudioRecorder alloc] initWithURL:destinationURL settings:settings error:&error];
audio_recorder.delegate = self;
}

与此问题无关,但一般要提及的是,在定义变量等时必须遵循 Apple(Objective-C)的命名约定。audio_recording 绝不遵循这些准则。您可以改用 audioRecording 之类的东西。

关于ios - AVAudioRecorder 不保存录音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28526236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com