需要导入
#import <AVFoundation/AVFoundation.h>
利用此框架中的
AVAudioRecorder和AVAudioPlayer来录音和播放以下是AVAudioRecorder录音的使用方法:
- (IBAction)downAction:(id)sender {
按下录音
if ([self canRecord]) {
NSError *error = nil;
必须真机上测试,模拟器上可能会崩溃
recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:playName] settings:recorderSettingsDict error:&error];
if (recorder) {
是否允许刷新电平表,默认是off
recorder.meteringEnabled = YES;
创建文件,并准备录音
[recorder prepareToRecord];
开始录音
[recorder record];
启动定时器,为了更新电平
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(levelTimer:) userInfo:nil repeats:YES];
} else
{
int errorCode = CFSwapInt32HostToBig ([error code]);
NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode);
}
}
}
- (IBAction)upAction:(id)sender {
松开 结束录音
录音停止
[recorder stop];
recorder = nil;
结束定时器
[timer invalidate];
timer = nil;
图片重置
soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:0]];
}
[cpp] view plain copy
- (IBAction)downAction:(id)sender {
按下录音
if ([self canRecord]) {
NSError *error = nil;
必须真机上测试,模拟器上可能会崩溃
recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:playName] settings:recorderSettingsDict error:&error];
if (recorder) {
是否允许刷新电平表,默认是off
recorder.meteringEnabled = YES;
创建文件,并准备录音
[recorder prepareToRecord];
开始录音
[recorder record];
启动定时器,为了更新电平
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(levelTimer:) userInfo:nil repeats:YES];
} else
{
int errorCode = CFSwapInt32HostToBig ([error code]);
NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode);
}
}
}
- (IBAction)upAction:(id)sender {
松开 结束录音
录音停止
[recorder stop];
recorder = nil;
结束定时器
[timer invalidate];
timer = nil;
图片重置
soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:0]];
}
以下是AVAudioPlayer播放器的使用方法:
- (IBAction)playAction:(id)sender {
NSError *playerError;
播放
player = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:playName] error:&playerError];
if (player == nil)
{
NSLog(@"ERror creating player: %@", [playerError description]);
}else{
[player play];
}
}
- (IBAction)playAction:(id)sender {
NSError *playerError;
播放
player = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:playName] error:&playerError];
if (player == nil)
{
NSLog(@"ERror creating player: %@", [playerError description]);
}else{
[player play];
}
}
提示:如果是7.0,第一次运行会提示,是否允许使用麦克风
if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending)
{
7.0第一次运行会提示,是否允许使用麦克风
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *sessionError;
AVAudioSessionCategoryPlayAndRecord用于录音和播放
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
if(session == nil)
NSLog(@"Error creating session: %@", [sessionError description]);
else
[session setActive:YES error:nil];
}
if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending)
{
7.0第一次运行会提示,是否允许使用麦克风
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *sessionError;
AVAudioSessionCategoryPlayAndRecord用于录音和播放
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
if(session == nil)
NSLog(@"Error creating session: %@", [sessionError description]);
else
[session setActive:YES error:nil];
}