iPhone - How to convert .caf audio files into .aac?

时光毁灭记忆、已成空白 提交于 2019-12-06 00:18:55

Try this

step 1) I have downloaded sample TPAACAudioConverter project from https://github.com/michaeltyson/TPAACAudioConverter

step 2)AACConverterViewController.m

i have set source file and destination file format

- (IBAction)convert:(id)sender {
if ( ![TPAACAudioConverter AACConverterAvailable] ) {
    [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Converting audio", @"")
                                 message:NSLocalizedString(@"Couldn't convert audio: Not supported on this device", @"")
                                delegate:nil
                       cancelButtonTitle:nil
                       otherButtonTitles:NSLocalizedString(@"OK", @""), nil] autorelease] show];
    return;
}

// Initialise audio session, and register an interruption listener, important for AAC conversion
if ( !checkResult(AudioSessionInitialize(NULL, NULL, interruptionListener, self), "initialise audio session") ) {
    [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Converting audio", @"")
                                 message:NSLocalizedString(@"Couldn't initialise audio session!", @"")
                                delegate:nil
                       cancelButtonTitle:nil
                       otherButtonTitles:NSLocalizedString(@"OK", @""), nil] autorelease] show];
    return;
}


// Set up an audio session compatible with AAC conversion.  Note that AAC conversion is incompatible with any session that provides mixing with other device audio.
UInt32 audioCategory = kAudioSessionCategory_MediaPlayback;
if ( !checkResult(AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory), "setup session category") ) {
    [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Converting audio", @"")
                                 message:NSLocalizedString(@"Couldn't setup audio category!", @"")
                                delegate:nil
                       cancelButtonTitle:nil
                       otherButtonTitles:NSLocalizedString(@"OK", @""), nil] autorelease] show];
    return;
} 

NSArray *documentsFolders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
audioConverter = [[[TPAACAudioConverter alloc] initWithDelegate:self 
                                                         source:[[NSBundle mainBundle] pathForResource:@"audio" ofType:@"aiff"]
                                                    destination:[[documentsFolders objectAtIndex:0] stringByAppendingPathComponent:@"audio.m4a"]] autorelease];
((UIButton*)sender).enabled = NO;
[self.spinner startAnimating];
self.progressView.progress = 0.0;
self.progressView.hidden = NO;

[audioConverter start];
}

play converted audio

- (IBAction)playConverted:(id)sender {
if ( audioPlayer ) {
    [audioPlayer stop];
    [audioPlayer release];
    audioPlayer = nil;
    [(UIButton*)sender setTitle:@"Play converted" forState:UIControlStateNormal];
} else {
    NSArray *documentsFolders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[documentsFolders objectAtIndex:0] stringByAppendingPathComponent:@"audio.aac"];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    [audioPlayer play];

    [(UIButton*)sender setTitle:@"Stop" forState:UIControlStateNormal];
}
}

step 3)TPAACAudioConverter.m

simply i have changed audio file type find and replace kAudioFileM4AType into kAudioFileAAC_ADTSType

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!