AVAudioplayer 有两个初始化方法:

1、[[AVAudioPlayer alloc] initWithData:musicData error&e];
2、[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
第一种是使用将音频文件的data文件初始化,但是data必须是一个完整的文件。
第二种是从url获取,但是这里的url是本地的file URL。
 
 所以AVAudioplayer无法直接从网络url上获取音频文件,所以可以用:
data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://.../file.mp3"]];
将网络上音频down下来,再用initWithData初始化player。但是这样down下来的文件有时是不完整的,所以出现创建失败问题,出现:
Error Domain=NSOSStatusErrorDomain Code=1954115647
解决这个问题的方法就是将data保存在本地,在通过initWithContentsOfURL,找到该文件,这样就可以成功创建了。
整个流程为:
NSData *audioData = [NSData dataWithContentsOfURL:someURL]; 
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp3", docDirPath , fileName];
[audioData writeToFile:filePath atomically:YES]; 
NSError *error; 
NSURL *fileURL = [NSURL fileURLWithPath:filePath]; 
player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
if (player == nil) 
{ NSLog(@"AudioPlayer did not load properly: %@", [error description]); }
else
{ [player play]; }

局限性:recorder,player简单易用,但是有局限性。
对我项目影响最大的是,多次录音时,并播放时,会出现文件错误。
在继续利用recorder,player的前提下,就需要将每次录音完成的文件进行数据追加。

if ([[NSFileManager defaultManager] fileExistsAtPath:临时音频路径) {

      NSData *tempAudioData = [[NSData alloc] initWithContentsOfFile:临时音频路径];

      if ([[NSFileManager defaultManager] fileExistsAtPath:音频路径]) {
NSMutableData *newAudioData = [NSMutableData data];
NSData *audioData = [[NSData alloc] initWithContentsOfFile:[self configureAudioRecordFilePath:self.currentFileName]];
[newAudioData appendData:audioData];
[newAudioData appendData:tempAudioData];
PADebug(@"data length:%zd", [newAudioData length]);
[newAudioData writeToFile:音频路径 atomically:YES];
}else
{
[tempAudioData writeToFile:[self configureAudioRecordFilePath:self.currentFileName] atomically:YES];
}
[[NSFileManager defaultManager]removeItemAtPath:音频路径 error:nil];
}


 
 

最新文章

  1. C++ 虚函数,纯虚函数的一些问题
  2. js数组的操作
  3. yum install 安装时报yum doesn't have enough cached data to continue.
  4. Navicat(连接) -1之SSH 设置
  5. 【转】第一次使用Android Studio时你应该知道的一切配置
  6. 一些VR延迟优化方法
  7. hdu 4864 Task (馋)
  8. Windows7 x64 编译Dlib库
  9. tesseract-ocr字库训练图文讲解
  10. threadlocal原理及常用应用场景
  11. @ConditionalOnMissingBean注解理解
  12. Linux基础知识第二讲,文件目录命令使用
  13. Python全栈-magedu-2018-笔记5
  14. table-layout:fixed; 表格比例固定
  15. Java 多态的实现机制
  16. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(六)一定要RESTful吗?
  17. 062 hive中的常用方法(case,cast,unix_timestamp)
  18. LeetCode 11 - 盛最多水的容器 - [双指针暴力]
  19. vmplayer桥接以及nat配置nginx
  20. ALV界面显示

热门文章

  1. [转帖]Speed-BI数据分析案例:2016年8月汽车销量排行榜
  2. swift NSUserDefaults的基本使用
  3. 查看oracle 数据库的DBID
  4. SSM框架搭建
  5. Java基础之读文件——使用输入流读取二进制文件(StreamInputFromFile)
  6. Tomcat类加载器机制
  7. PostgreSQL Replication之第十一章 使用Skytools(5)
  8. ls常用参数
  9. php 警告
  10. (转)supertable像excel那样固定table的表头和第一列