+ (void)mp3ToPCMWithMp3File:(NSString*)mp3FilePath? outPutPCMPath:(NSString*)outPutPCMPath mp3ToPcmComplete:(Mp3ToPcmComplete)mp3ToPcmComplete {
? ? AVAsset*asset = [OCmp3ToPCMreadMp3FileWithMp3File:mp3FilePath];
? ? AVAssetReader*assetReader = [OCmp3ToPCMinitAssetReaderWithAsset:asset];
? ? AudioChannelLayoutchannelLayout;
? ? memset(&channelLayout,0,sizeof(channelLayout));
? ? channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
? ? NSDictionary*outputSettings =@{AVFormatIDKey:@(kAudioFormatLinearPCM),? ? // 音頻格式
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AVSampleRateKey:@(44100),? ? // 采樣率
//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AVSampleRateKey : @(22050),? ? // 采樣率
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AVNumberOfChannelsKey:@(2),? ? // 通道數(shù) 1 || 2
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AVChannelLayoutKey: [NSDatadataWithBytes:&channelLayoutlength:sizeof(channelLayout)],? // 聲音效果(立體聲)
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AVLinearPCMBitDepthKey:@(16),? // 音頻的每個樣點(diǎn)的位數(shù)
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AVLinearPCMIsNonInterleaved:@NO,? // 音頻采樣是否非交錯
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AVLinearPCMIsFloatKey:@NO,? ? // 采樣信號是否浮點(diǎn)數(shù)
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AVLinearPCMIsBigEndianKey:@NO// 音頻采用高位優(yōu)先的記錄格式
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? };
? ? AVAssetReaderAudioMixOutput *readerAudioMixOutput = [[AVAssetReaderAudioMixOutput alloc] initWithAudioTracks:asset.tracks audioSettings:outputSettings];
? ? if(![assetReadercanAddOutput:readerAudioMixOutput]) {
? ? ? ? NSLog(@"can't add readerAudioMixOutput");
? ? ? ? return;
? ? }
? ? [assetReaderaddOutput:readerAudioMixOutput];
? ? AVAssetWriter*assetWriter = [OCmp3ToPCMinitAssetWriterWithPCMName:outPutPCMPath];
? ? if(![assetWritercanApplyOutputSettings:outputSettingsforMediaType:AVMediaTypeAudio]) {
? ? ? ? NSLog(@"can't apply outputSettings");
? ? ? ? return;
? ? }
? ? AVAssetWriterInput *writerInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeAudio outputSettings:outputSettings];
? ? writerInput.expectsMediaDataInRealTime = NO;
? ? if(![assetWritercanAddInput:writerInput]) {
? ? ? ? NSLog(@"can't add writerInput");
? ? ? ? return;
? ? }
? ? [assetWriteraddInput:writerInput];
? ? [assetReaderstartReading];
? ? [assetWriterstartWriting];
? ? AVAssetTrack *track = asset.tracks.firstObject;
? ? if(!track) {
? ? ? ? return;
? ? }
? ? CMTimestartTime =CMTimeMakeWithSeconds(0, track.naturalTimeScale);
? ? [assetWriterstartSessionAtSourceTime:startTime];
? ? dispatch_queue_t mediaInputQueue = dispatch_queue_create([@"mediaInputQueue" cStringUsingEncoding:NSASCIIStringEncoding], DISPATCH_QUEUE_SERIAL);
? ? [writerInputrequestMediaDataWhenReadyOnQueue:mediaInputQueue usingBlock:^{
? ? ? ? while(writerInput.isReadyForMoreMediaData) {
? ? ? ? ? ? CMSampleBufferRefnextBuffer = readerAudioMixOutput.copyNextSampleBuffer;
? ? ? ? ? ? if(nextBuffer) {
? ? ? ? ? ? ? ? [writerInputappendSampleBuffer:nextBuffer];
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? [writerInputmarkAsFinished];
? ? ? ? ? ? ? ? [assetReadercancelReading];
? ? ? ? ? ? ? ? [assetWriterfinishWritingWithCompletionHandler:^{
? ? ? ? ? ? ? ? ? ? NSLog(@"mp3轉(zhuǎn)pcm完成");
? ? ? ? ? ? ? ? ? ? if(mp3ToPcmComplete) {
? ? ? ? ? ? ? ? ? ? ? ? mp3ToPcmComplete();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }];
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }];
}
+ (AVAsset*)readMp3FileWithMp3File:(NSString*)mp3FilePath {
? ? NSURL*fileURL = [NSURLfileURLWithPath:mp3FilePath];
? ? AVAsset*asset = [AVAssetassetWithURL:fileURL];
? ? returnasset;
}
+ (AVAssetReader*)initAssetReaderWithAsset:(AVAsset*)asset {
? ? NSError*error;
? ? AVAssetReader*assetReader;
? ? assetReader = [[AVAssetReaderalloc]initWithAsset:asseterror:&error];
? ? returnassetReader;
}
+ (AVAssetWriter*)initAssetWriterWithPCMName:(NSString*)outPutPCMPath {
? ? NSError*error;
? ? AVAssetWriter*assetWriter;
? ? NSURL*outPutURL = [NSURLfileURLWithPath:outPutPCMPath];
? ? assetWriter = [[AVAssetWriteralloc]initWithURL:outPutURLfileType:AVFileTypeWAVEerror:&error];
? ? returnassetWriter;
}