iOS-音頻格式轉換-b

來源:互聯網
上載者:User

標籤:

iOS處理音頻過程中有時候需要不同格式的音頻進行轉換,最近需要將m4a格式的音頻轉換成wav,在網上搜尋之後代碼整理如下:

- (void)convetM4aToWav:(NSURL *)originalUrl  destUrl:(NSURL *)destUrl {        AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:originalUrl options:nil];    //讀取原始檔案資訊         NSError *error = nil;        AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:songAsset error:&error];        if (error) {                NSLog (@"error: %@", error);                return;         }        AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderAudioMixOutput                                                assetReaderAudioMixOutputWithAudioTracks:songAsset.tracks                                                audioSettings: nil];        if (![assetReader canAddOutput:assetReaderOutput]) {                NSLog (@"can‘t add reader output... die!");                return;         }         [assetReader addOutput:assetReaderOutput];        AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:destUrl                                                            fileType:AVFileTypeCoreAudioFormat                                                               error:&error];        if (error) {                NSLog (@"error: %@", error);                return;         }         AudioChannelLayout channelLayout;         memset(&channelLayout, 0, sizeof(AudioChannelLayout));         channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;        NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:                                     [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,                                     [NSNumber numberWithFloat:16000.0], AVSampleRateKey,                                     [NSNumber numberWithInt:2], AVNumberOfChannelsKey,                                     [NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], AVChannelLayoutKey,                                     [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,                                     [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,                                     [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,                                     [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,                                    nil];    AVAssetWriterInput *assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio                                                                                outputSettings:outputSettings];        if ([assetWriter canAddInput:assetWriterInput]) {                 [assetWriter addInput:assetWriterInput];         } else {                NSLog (@"can‘t add asset writer input... die!");                return;         }          assetWriterInput.expectsMediaDataInRealTime = NO;          [assetWriter startWriting];         [assetReader startReading];        AVAssetTrack *soundTrack = [songAsset.tracks objectAtIndex:0];         CMTime startTime = CMTimeMake (0, soundTrack.naturalTimeScale);         [assetWriter startSessionAtSourceTime:startTime];          __block UInt64 convertedByteCount = 0;        dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL);         [assetWriterInput requestMediaDataWhenReadyOnQueue:mediaInputQueue                                             usingBlock: ^      {                 while (assetWriterInput.readyForMoreMediaData) {                          CMSampleBufferRef nextBuffer = [assetReaderOutput copyNextSampleBuffer];                         if (nextBuffer) {                                 // append buffer                                  [assetWriterInput appendSampleBuffer: nextBuffer];                                 NSLog (@"appended a buffer (%zu bytes)",                                      CMSampleBufferGetTotalSampleSize (nextBuffer));                                  convertedByteCount += CMSampleBufferGetTotalSampleSize (nextBuffer);                             } else {                                   [assetWriterInput markAsFinished];                                   [assetWriter finishWritingWithCompletionHandler:^{                                    }];                                   [assetReader cancelReading];                                  NSDictionary *outputFileAttributes = [[NSFileManager defaultManager]                                                        attributesOfItemAtPath:[destUrl path]                                                        error:nil];                                 NSLog (@"FlyElephant %lld",[outputFileAttributes fileSize]);                                 break;                          }                   }           }]; }

參考連結:http://subfurther.com/blog/2010/12/13/from-ipod-library-to-pcm-samples-in-far-fewer-steps-than-were-previously-necessary/

iOS-音頻格式轉換-b

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.