录音/播放音频
2015-10-23 12:02
309 查看
需要导入系统库文件
//点击录音按钮进行录音
//停止录音
点击cell进行播放,代码如下:
播放器属性
//定义Audio player
当前播放时间
总时间
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
相关文章推荐
- LeetCode Find Median from Data Stream
- c++ de-mangle 反编译器命名工具:c++filt
- CountDownLatch和CyclicBarrier的区别
- C#代码实现PDF文档操作类
- VVDocumenter-Xcode 函数注释
- AC自动机+base64解码 zoj3430 Detect the Virus
- vim、gvim在windows下中文乱码的终极解决方案
- PHP常用函数整理
- QQ拼音在中文输入下默认英文标点
- iOS UIView动画效果
- Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
- HDU1028 (整数拆分)
- epoll 经验谈
- android sd卡路径正确获取 双sd卡获取(getexternalstoragedirectory)
- cygwin-介绍-安装
- 点击文字弹出一个DIV层窗口代码
- Visual Studio原生开发的20条调试技巧(下)
- Sublime text 2or3 插件安装办法(2015更新)
- AndroidStudio 显示中文乱码
- epoll 经验谈