如何将WAV文件转换为M4A?

jfa*_*jay 5 audio m4a wav ios

有没有办法将录制的.WAV文件转换为iOS中的.M4A文件?

而且我还必须将.M4A文件转换为.WAV文件.

我试过音频队列服务,但我无法做到.

小智 3

这篇文章:从 iPod 库到 PCM 样本的步骤比以前所需的步骤少得多,描述了如何从用户的 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;
}
Run Code Online (Sandbox Code Playgroud)

如果您要朝相反的方向前进,则需要更改输出端的格式:

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];
Run Code Online (Sandbox Code Playgroud)

我不确定 m4a 的确切设置,但这应该会让您更接近。

另一种选择是加载 ffmpeg lib 并在那里进行所有转换,但这似乎与您想要的不同。