8vi*_*ius 5 core-audio avfoundation avaudioplayer avaudiorecorder ios
当我的视图加载时,我按以下方式设置AVAudioRecorder实例:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
audioSession.delegate = self;
[audioSession setActive:YES error:nil];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
NSString *tempDir = NSTemporaryDirectory();
NSString *soundFilePath = [tempDir stringByAppendingPathComponent:@"sound.m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(@"%@", soundFileURL);
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
[NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16], AVEncoderBitRateKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithFloat:8000.0], AVSampleRateKey,
[NSNumber numberWithInt:8], AVLinearPCMBitDepthKey,
nil];
NSError *error = nil;
self.recorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
self.recorder.delegate = self;
if (error) {
NSLog(@"error: %@", [error localizedDescription]);
} else {
[self.recorder prepareToRecord];
}
Run Code Online (Sandbox Code Playgroud)
然后,当用户按下记录按钮时,我做:
- (IBAction)record:(id)sender {
if (!self.isRecording) {
self.isRecording = YES;
[self.recorder record];
self.recordButton.enabled = NO;
}
}
Run Code Online (Sandbox Code Playgroud)
然后停止录制/播放:
- (IBAction)stop:(id)sender {
if (self.isRecording) {
[self.recorder stop];
self.recordButton.enabled = YES;
self.isRecording = NO;
}
if (self.isPlaying) {
[self.player stop];
self.isPlaying = NO;
self.playButton.enabled = YES;
}
}
Run Code Online (Sandbox Code Playgroud)
之后,我希望能够播放录制的内容,因此我执行以下操作:
- (IBAction)play:(id)sender {
NSError *error = nil;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.recorder.url error:&error];
self.player.delegate = self;
if (error) {
NSLog(@"%@", error.localizedDescription);
} else {
self.isPlaying = YES;
[self.player play];
self.playButton.enabled = NO;
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我去播放文件时,找不到OSStatus错误-43文件.
这一切都在设备btw上运行,在模拟器上尝试实例化录音机或播放器时出错,从我看到它是模拟器问题.
编辑1:我解决了OSStatus错误-43部分它与编码格式有关,我注释掉格式并且它正确记录和播放文件,但我需要以压缩格式记录.有谁知道这个设置?
编辑2:我设法找到适用于压缩AAC音频的设置.一个2分钟的音频文件出现了256KB,我更新了我的代码以反映当前的状态.感谢所有为您提供帮助的人.
因为没有其他人回答(编辑:现在有)(我对iOS中的音频没有多少经验)但我会对此采取疯狂的尝试,但......
尝试改变
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
Run Code Online (Sandbox Code Playgroud)
至
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6442 次 |
| 最近记录: |