2011-01-28 156 views
5

有什麼辦法將我錄製的.WAV文件轉換爲iOS中的.M4A文件?如何將WAV文件轉換爲M4A?

而且我還必須將.M4A文件轉換爲.WAV文件。

我嘗試使用音頻隊列服務,但我無法做到。

回答

3

此文章:From iPod Library to PCM Samples in Far Fewer Steps Than Were Previously Necessary介紹瞭如何從用戶ipod庫加載文件並將其作爲線性pcm(wav)文件寫入文件系統。

我相信,你將需要對代碼的改變來加載從文件系統中的文件,而不是將是描述NSURL其中資產是:

-(IBAction) convertTapped: (id) sender { 
// set up an AVAssetReader to read from the iPod Library 
NSURL *assetURL = [[NSURL alloc] initFileURLWithPath:@"your_m4a.m4a"]; 
AVURLAsset *songAsset = 
    [AVURLAsset URLAssetWithURL:assetURL options:nil]; 

NSError *assetError = nil; 
AVAssetReader *assetReader = 
    [[AVAssetReader assetReaderWithAsset:songAsset 
      error:&assetError] 
     retain]; 
if (assetError) { 
    NSLog (@"error: %@", assetError); 
    return; 
} 

如果您打算在相反的方向,則需要改變輸出端的格式:

NSDictionary *outputSettings =[NSDictionary dictionaryWithObjectsAndKeys: 
[NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey, 
[NSNumber numberWithFloat:44100.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]; 

我不知道確切的設置可能會包含在這裏M4A,但這應該讓你更接近。

另一種選擇是加載ffmpeg庫並在那裏做所有的轉換,但是這看起來與你想要的不同。

+0

什麼是channelLayout在這裏? – 2013-01-01 15:28:37