How to convert WAV file to M4A?

2019-03-29 10:59发布

问题:

Is there any way to convert my recorded .WAV file to .M4A file in iOS?

And also I have to convert .M4A file to .WAV file.

I tried with Audio Queue Services, but I am not able to do.

回答1:

This post: From iPod Library to PCM Samples in Far Fewer Steps Than Were Previously Necessary describes how to load a file from the users ipod library and write it to the file system as a linear pcm (wav) file.

I believe that the change that you will need to make to the code to load a file from the file system instead would be in the NSURL that describes where the asset is:

-(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;
}

If you are going in the opposite direction, you will need to change the formatting on the output end:

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];

I am not sure of the exact settings that would go in here for m4a, but this should get you closer.

The other option would be to load in ffmpeg lib and do all your conversion in there, but that seems like different than what you want.



回答2:

TPAACAudioConverter works fine



标签: ios audio wav m4a