您的位置:首页 > 其它

录音/播放音频

2015-10-23 12:02 309 查看
需要导入系统库文件

import <AVFoundation/AVFoundation.h>


//定义录音类
@property (nonatomic,strong) AVAudioRecorder *recoder;
//定义播放类
@property (nonatomic,strong) AVAudioPlayer *player;


录音

点击UINavigation右侧的录音按钮进行录音,点击停止则停止录音,示例代码如下:

//点击录音按钮进行录音

-(void)onAdd{
//设置录音文件存放的沙盒路径
NSString *path = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[NSUUID UUID].UUIDString];
//NSUUID会返回唯一的ID
//创建录音类
//取本地文件的URL fileURLWithPath
self.recoder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:path] settings:nil error:nil];
//准备录音
if ([self.recoder prepareToRecord]) {
//开始录音
[self.recoder record];
}
[self.data addObject:path];
[self.tableView reloadData];

}


//停止录音

[self.recoder stop];


播放

录音的文件路径在tableviewcell中存储

点击cell进行播放,代码如下:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.player isPlaying]) {
//正在播放,停止
[self.player stop];
}else {
//创建对象
NSURL *url = [NSURL fileURLWithPath:self.data[indexPath.row]];
self.player =[[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
//准备播放
if ([self.player prepareToPlay]) {
//开始播放
[self.player play];
}
}
}


播放器属性

//定义Audio player

@property (nonatomic,strong) AVAudioPlayer *audioPlayer;


当前播放时间

self.audioPlayer.currentTime


总时间

self.audioPlayer.duration
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: