iPhone – Comment convertir les files audio .caf dans .aac?

Je travaille sur l'logging audio. Je suis capable d'save mon audio au format caf (Core audio format) .i a suivi ce tutoriel Recording Audio sur un iPhone avec AVAudioRecorder . Maintenant, je ne veux pas save le son au format .aac directement, j'ai besoin de convertir le file audio enregistré .caf en file audio .aac … une idée de comment faire cela?

#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface recordViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate> { AVAudioRecorder *audioRecorder; AVAudioPlayer *audioPlayer; UIButton *playButton; UIButton *recordButton; UIButton *stopButton; } @property (nonatomic, retain) IBOutlet UIButton *playButton; @property (nonatomic, retain) IBOutlet UIButton *recordButton; @property (nonatomic, retain) IBOutlet UIButton *stopButton; -(IBAction) recordAudio; -(IBAction) playAudio; -(IBAction) stop; @end #import "recordViewController.h" @implementation recordViewController @synthesize playButton, stopButton, recordButton; - (void)viewDidLoad { [super viewDidLoad]; playButton.enabled = NO; stopButton.enabled = NO; NSArray *dirPaths; NSSsortingng *docsDir; dirPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); docsDir = [dirPaths objectAtIndex:0]; NSSsortingng *soundFilePath = [docsDir ssortingngByAppendingPathComponent:@"sound.caf"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey, [NSNumber numberWithInt:16], AVEncoderBitRateKey, [NSNumber numberWithInt: 2], AVNumberOfChannelsKey, [NSNumber numberWithFloat:44100.0], AVSampleRateKey, nil]; NSError *error = nil; audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&error]; if (error) { NSLog(@"error: %@", [error localizedDescription]); } else { [audioRecorder prepareToRecord]; } } -(void) recordAudio { if (!audioRecorder.recording) { playButton.enabled = NO; stopButton.enabled = YES; [audioRecorder record]; } } -(void)stop { stopButton.enabled = NO; playButton.enabled = YES; recordButton.enabled = YES; if (audioRecorder.recording) { [audioRecorder stop]; } else if (audioPlayer.playing) { [audioPlayer stop]; } } -(void) playAudio { if (!audioRecorder.recording) { stopButton.enabled = YES; recordButton.enabled = NO; if (audioPlayer) [audioPlayer release]; NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioRecorder.url error:&error]; audioPlayer.delegate = self; if (error) NSLog(@"Error: %@", [error localizedDescription]); else [audioPlayer play]; } } -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { recordButton.enabled = YES; stopButton.enabled = NO; } -(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error { NSLog(@"Decode Error occurred"); } -(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag { } -(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error { NSLog(@"Encode Error occurred"); } @end 

Essaye ça

étape 1) J'ai téléchargé l'exemple de projet TPAACAudioConverter de https://github.com/michaeltyson/TPAACAudioConverter

étape 2) AACConverterViewController.m

J'ai mis le file source et le format de file de destination

 - (IBAction)convert:(id)sender { if ( ![TPAACAudioConverter AACConverterAvailable] ) { [[[[UIAlertView alloc] initWithTitle:NSLocalizedSsortingng(@"Converting audio", @"") message:NSLocalizedSsortingng(@"Couldn't convert audio: Not supported on this device", @"") delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedSsortingng(@"OK", @""), nil] autorelease] show]; return; } // Initialise audio session, and register an interruption listner, important for AAC conversion if ( !checkResult(AudioSessionInitialize(NULL, NULL, interruptionListener, self), "initialise audio session") ) { [[[[UIAlertView alloc] initWithTitle:NSLocalizedSsortingng(@"Converting audio", @"") message:NSLocalizedSsortingng(@"Couldn't initialise audio session!", @"") delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedSsortingng(@"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:NSLocalizedSsortingng(@"Converting audio", @"") message:NSLocalizedSsortingng(@"Couldn't setup audio category!", @"") delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedSsortingng(@"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] ssortingngByAppendingPathComponent:@"audio.m4a"]] autorelease]; ((UIButton*)sender).enabled = NO; [self.spinner startAnimating]; self.progressView.progress = 0.0; self.progressView.hidden = NO; [audioConverter start]; } 

lire l'audio converti

 - (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); NSSsortingng *path = [[documentsFolders objectAtIndex:0] ssortingngByAppendingPathComponent:@"audio.aac"]; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; [audioPlayer play]; [(UIButton*)sender setTitle:@"Stop" forState:UIControlStateNormal]; } } 

étape 3) TPAACAudioConverter.m

simplement j'ai changé le type de file audio find et replace kAudioFileM4AType dans kAudioFileAAC_ADTSType