在Objective C中实现录音和播放音频可以使用AVFoundation库。以下是录音和播放音频的代码示例:
录音:
#import <AVFoundation/AVFoundation.h>
//设置录音会话
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
//创建录音机
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = @{
AVSampleRateKey: @(44100.0),
AVFormatIDKey: @(kAudioFormatAppleLossless),
AVNumberOfChannelsKey: @(1),
AVEncoderAudioQualityKey: @(AVAudioQualityMax)
};
NSError *error;
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
//开始录音
[recorder prepareToRecord];
[recorder record];
播放音频:
#import <AVFoundation/AVFoundation.h>
//创建音频播放器
NSString *path = [[NSBundle mainBundle] pathForResource:@"audio" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
//播放音频
[player play];
以上代码仅作为参考,具体的实现还需要根据不同的需求进行修改和完善。