iOS开发-AVFoundation框架实现录音和播放

最近手上活不多,闲下来写了些小demo,总结一些出来。供大家参考。


iOS开发-AVFoundation框架实现录音和播放_第1张图片
效果图

首先,当然要引入#import

利用此框架中的

AVAudioRecorder和AVAudioPlayer来录音和播放以下是AVAudioRecorder录音的使用方法:

- (IBAction)downAction:(id)sender{

按下录音

if([selfcanRecord]) {

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.1target:selfselector:@selector(levelTimer:) userInfo:nilrepeats:YES];

}else

{

interrorCode =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]];

}

- (IBAction)downAction:(id)sender{

按下录音

if([selfcanRecord]) {

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.1target:selfselector:@selector(levelTimer:) userInfo:nilrepeats:YES];

}else

{

interrorCode =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:YESerror: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:YESerror:nil];

}

ok!完美,perfect!

你可能感兴趣的:(iOS开发-AVFoundation框架实现录音和播放)