從音樂庫取得歌曲清單之後,下一步往往是拿到「聲音本身」——可能要做波形分析、節奏偵測,或任何需要 PCM 原始資料的處理。這件事在 iOS 上有兩條路,取捨在於速度與佔用空間。
把歌曲從音樂庫轉存到應用程式目錄,格式為 .caf,使用的介面是 AVAssetReader、AVAssetWriter、AVAssetReaderAudioMixOutput 與 AVAssetWriterInput。
優點是流程直觀、程式碼好寫;缺點有兩個:速度偏慢,而且每讀一首歌就會在應用程式資料夾裡生成一個 .caf 檔——而 .caf 的檔案大小大約是同一首歌 .mp3 的十倍,一首一首累積下來很快就會壓垮使用者的儲存空間。若走這條路,處理完一定要記得刪除暫存檔。
直接把檔案內容分塊讀進記憶體,邊讀邊解析,不必落地成檔案。適合音訊解析這類應用,也不會佔用額外磁碟空間。
// 傳入參數為 MPMediaItem 的 assetURL
- (void)loadToMemory:(NSURL*)asset_url
{
NSError *reader_error = nil;
AVURLAsset *item_choosed_asset = [AVURLAsset URLAssetWithURL:asset_url options:nil];
AVAssetReader *item_reader = [AVAssetReader assetReaderWithAsset:item_choosed_asset
error:&reader_error];
if (reader_error) {
NSLog(@"failed to creat asset reader, reason:%@", [reader_error description]);
return;
}
NSArray *asset_tracks = [item_choosed_asset tracks];
AVAssetReaderAudioMixOutput *item_reader_output =
[AVAssetReaderAudioMixOutput assetReaderAudioMixOutputWithAudioTracks:asset_tracks
audioSettings:nil];
if ([item_reader canAddOutput:item_reader_output]) {
[item_reader addOutput:item_reader_output];
} else {
NSLog(@"the reader can not add the output");
}
UInt64 total_converted_bytes;
UInt64 converted_count;
UInt64 converted_sample_num;
size_t sample_size;
short* data_buffer = nil;
CMBlockBufferRef next_buffer_data = nil;
[item_reader startReading];
while (item_reader.status == AVAssetReaderStatusReading) {
CMSampleBufferRef next_buffer = [item_reader_output copyNextSampleBuffer];
if (next_buffer) {
total_converted_bytes = CMSampleBufferGetTotalSampleSize(next_buffer);
sample_size = CMSampleBufferGetSampleSize(next_buffer, 0);
converted_sample_num = CMSampleBufferGetNumSamples(next_buffer);
NSLog(@"the number of samples is %f", (float)converted_sample_num);
NSLog(@"the size of the sample is %f", (float)sample_size);
NSLog(@"the size of the whole buffer is %f", (float)total_converted_bytes);
// 每取得一次 sample buffer 就立即解析,而不是整檔載入後才處理
// copyNextSampleBuffer 每次讀取約 8196 個 sample(最後一次除外)
// 資料以 short 形式存放,每個 short 佔兩位元組
// 每個 sample 的大小與聲道數相關,可用 CMSampleBufferGetSampleSize 取得
// 因此每次取得的資料量為 8196 * sample_size (bytes)
// 對應需要的緩衝區為 (8196 * sample_size) / 2 個 short
if (!data_buffer) {
data_buffer = new short[4096 * sample_size];
}
next_buffer_data = CMSampleBufferGetDataBuffer(next_buffer);
OSStatus buffer_status =
CMBlockBufferCopyDataBytes(next_buffer_data, 0, total_converted_bytes, data_buffer);
if (buffer_status != kCMBlockBufferNoErr) {
NSLog(@"something wrong happened when copying data bytes");
}
/*
此時音訊資料位於 data_buffer 中,是未經壓縮的原始資料,
可進行解析或其他處理
*/
} else {
NSLog(@"total sample size %d", converted_count);
break;
}
}
if (item_reader.status == AVAssetReaderStatusCompleted) {
NSLog(@"read over......");
} else {
NSLog(@"read failed;");
}
}
若只是想把歌曲變成應用程式可直接取用的檔案,用 MPMediaPickerController 挑歌,再以 AVAssetExportSession 轉存即可。
MPMediaPickerController *pickerController =
[[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
pickerController.allowsPickingMultipleItems = NO;
pickerController.delegate = self;
[self presentModalViewController:pickerController animated:YES];
[pickerController release];
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker
didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
NSArray *media_array = [mediaItemCollection items];
MPMediaItem *song_item = [media_array objectAtIndex:0];
NSURL *url = [song_item valueForProperty:MPMediaItemPropertyAssetURL];
NSLog(@"url is %@", url);
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVAssetExportSession *exporter =
[[AVAssetExportSession alloc] initWithAsset:songAsset
presetName:AVAssetExportPresetAppleM4A];
exporter.outputFileType = @"com.apple.m4a-audio";
NSString *exportFile = [myDocumentsDirectory() stringByAppendingPathComponent:@"exported.m4a"];
if ([[NSFileManager defaultManager] fileExistsAtPath:exportFile]) {
NSError *deleteErr = nil;
[[NSFileManager defaultManager] removeItemAtPath:exportFile error:&deleteErr];
if (deleteErr) {
NSLog(@"Can't delete %@: %@", exportFile, deleteErr);
}
}
NSURL *path_url = [NSURL fileURLWithPath:exportFile];
exporter.outputURL = path_url;
[exporter exportAsynchronouslyWithCompletionHandler:^{
switch (exporter.status)
{
case AVAssetExportSessionStatusFailed:
NSLog(@"AVAssetExportSessionStatusFailed: %@", exporter.error);
break;
case AVAssetExportSessionStatusCompleted:
{
NSLog(@"AVAssetExportSessionStatusCompleted");
NSData *data = [NSData dataWithContentsOfURL:path_url];
NSLog(@"data is %@", data);
break;
}
case AVAssetExportSessionStatusUnknown:
NSLog(@"AVAssetExportSessionStatusUnknown");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"AVAssetExportSessionStatusExporting");
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"AVAssetExportSessionStatusCancelled");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"AVAssetExportSessionStatusWaiting");
break;
default:
NSLog(@"didn't get export status");
break;
}
}];
[mediaPicker dismissModalViewControllerAnimated:YES];
}
三個方案各有定位。需要 PCM 原始樣本做訊號處理,用方案二的分塊讀取,記憶體可控、不落地;需要一個可直接播放或上傳的檔案,用方案三匯出為 m4a;方案一流程最簡單,但在檔案佔用與速度上都要付出代價,如今已有更好的替代做法。
另外幾點共通提醒:上述程式碼為早期 MRC 寫法,ARC 專案請移除 release 呼叫;AVAssetExportSession 的完成回呼在背景執行緒觸發,若要更新介面必須切回主執行緒;讀取音樂庫前別忘了先確認使用者授權狀態。