作者:粪想升或_519 | 来源:互联网 | 2023-10-14 11:32
1,初始化AVAudioSession7.0第一次运行会提示,是否允许使用麦克风AVAudioSession*session[AVAudioSessionsharedInsta
1,初始化 AVAudioSession
class="s2">//7.0第一次运行会提示,是否允许使用麦克风
AVAudioSession *session = [AVAudioSessionclass="s4">sharedInstance];
NSError
*sessionError;
//AVAudioSessionCategoryPlayAndRecord用于录音和播放
[session class="s4">setCategory:class="s5">AVAudioSessionCategoryPlayAndRecord class="s4">error:&sessionError];
if(session ==
nil)
class="s4">NSLog(@"Error creating session: %@",
[sessionError description]);
else
[session setActive:class="s7">YES error:class="s7">nil];
2,生成目录
class="s2">NSString *docDir =
[NSSearchPathForDirectoriesInDomainsclass="s1">(NSDocumentDirectory,
NSUserDomainMask, class="s3">YES) objectAtIndexclass="s1">:0];
playName = [class="s2">NSString stringWithFormat:@"%@/play.aac",docDir];
3,录制
class="s2">recorder = [[class="s3">AVAudioRecorderalloc] initWithURLclass="s1">:[NSURLURLWithStringclass="s1">:playName]
settings:class="s2">recorderSettingsDicterrorclass="s1">:&error];
if (class="s2">recorder) {
class="s2">recorder.meteringEnabled = class="s4">YES;
[class="s2">recorder prepareToRecord];
[class="s2">recorder record];
4,播放
class="s2">player = [[class="s3">AVAudioPlayeralloc]
initWithContentsOfURL:[class="s3">NSURLURLWithString:class="s2">playName] errorclass="s1">:&playerError];
if (class="s2">player == nil)
{
NSLogclass="s1">(@"ERror creating player: %@", [playerError
description]);
}else{
[player class="s5">play];
}
ps:好像AVAudioSession 与class="s3">AVAudioRecorder, class="s3">AVAudioPlayer没有什么关系,代码之间看不到,
a,设置:
//录音设置
NSMutableDictionary *recordSetting = [[[NSMutableDictionary
alloc]init] autorelease];
//设置录音格式
AVFormatIDKey==kAudioFormatLinearPCM
[recordSetting setValue:[NSNumber
numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//设置录音采样率(Hz)
如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
[recordSetting
setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
//录音通道数 1 或 2
[recordSetting setValue:[NSNumber numberWithInt:1]
forKey:AVNumberOfChannelsKey];
//线性采样位数 8、16、24、32
[recordSetting
setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//录音的质量
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh]
forKey:AVEncoderAudioQualityKey];
//要找的文件路径
NSString *strUrl =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
lastObject];
//创建存储的文件夹
NSURL *url = [NSURL fileURLWithPath:[NSString
stringWithFormat:@"%@/lll.aac", strUrl]];
urlPlay =
url;//获得播放路径
NSLog(@"录音的路径为:%@",strUrl);
NSLog(@"录音====路径:%@",url);
NSError *error;
//初始化
recorder =
[[AVAudioRecorder alloc]initWithURL:url settings:recordSetting
error:&error];
//开启音量检测
recorder.meteringEnabled = YES;
recorder.delegate = self;
b,启动,时间限制,
- (IBAction)btnDown:(id)sender
{
//创建录音文件,准备录音
if ([recorder
prepareToRecord]) {
//开始
[recorder record];
}
//设置定时检测
timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self
selector:@selector(detectionVoice) userInfo:nil repeats:YES];
}
- (IBAction)btnUp:(id)sender
{
>double cTime =
recorder.currentTime;
if
(cTime > 2) {//如果录制时间<2 不发送
[mainRecorder recordForDuration:60.0];//设置录音时限60秒,该方法自动执行prepareToRecord
NSLog(@"发出去");
}else {
//删除记录的文件
[recorder
deleteRecording];
//删除存储的
}
[recorder stop];
[timer
invalidate];//移除计时器
}
- (IBAction)btnDragUp:(id)sender
{
//删除录制文件
[recorder deleteRecording];
[recorder stop];
[timer
invalidate];
NSLog(@"取消发送");
}