简体   繁体   English

iPhone - 如何将.caf音频文件转换为.aac?

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

I am working on audio recording. 我正在录音。 I am able to record my audio in caf (Core audio format).i followed this tutorial Recording Audio on an iPhone with AVAudioRecorder . 我可以在caf(Core audio format)中录制我的音频.i遵循本教程在带有AVAudioRecorder的iPhone上录制音频 Now I don't want to record sound in .aac format directly, I need to convert recorded .caf audio file into .aac audio file... any idea how to do this? 现在我不想直接录制.aac格式的声音,我需要将录制的.caf音频文件转换为.aac音频文件...任何想法如何做到这一点?

#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;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(
    NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
   stringByAppendingPathComponent:@"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

Try this 试试这个

step 1) I have downloaded sample TPAACAudioConverter project from https://github.com/michaeltyson/TPAACAudioConverter 步骤1)我从https://github.com/michaeltyson/TPAACAudioConverter下载了示例TPAACAudioConverter项目

step 2) AACConverterViewController.m 步骤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 步骤3) TPAACAudioConverter.m

simply i have changed audio file type find and replace kAudioFileM4AType into kAudioFileAAC_ADTSType 我只更改了音频文件类型查找并将kAudioFileM4AType替换为kAudioFileAAC_ADTSType

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM