【发布时间】:2013-09-19 00:13:04
【问题描述】:
我正在录音。我能够以 caf(核心音频格式)录制我的音频。我遵循了本教程 Recording Audio on an iPhone with AVAudioRecorder。现在我不想直接以 .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
【问题讨论】:
-
@Smick 我已经尝试过了。第一个 iPhoneExtAudioFileConvertTest 示例项目同时将 .caf 转换为 .aac 文件转换很好,但它不能播放...第二个 TPAACAudioConverter 项目除了 .aac 转换外一切正常
-
我期待看到这个问题得到解决。我试图做一个频率应用程序将音乐更改为 432Hz.... 无法解决它。我会投票赞成这个问题。
标签: ios objective-c audio aac caf