AVAudioplayer 有两个初始化方法:网络
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的前提下,就须要将每次录音完成的文件进行数据追加。app
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]; }