慕标5832272
虽然这是一个已回答的问题(有点旧),但我决定将我的完整工作代码发布给其他发现很难找到良好工作(开箱即用)播放和录制示例的人-包括编码、pcm、通过扬声器播放、写在这里的文件:AudioPlayerViewController.h:#import <UIKit/UIKit.h>#import <AVFoundation/AVFoundation.h>@interface AudioPlayerViewController : UIViewController {AVAudioPlayer *audioPlayer;AVAudioRecorder *audioRecorder;int recordEncoding;enum{
ENC_AAC = 1,
ENC_ALAC = 2,
ENC_IMA4 = 3,
ENC_ILBC = 4,
ENC_ULAW = 5,
ENC_PCM = 6,} encodingTypes;}-(IBAction) startRecording;-(IBAction) stopRecording;-(IBAction) playRecording;-(IBAction) stopPlaying;@endAudioPlayerViewController.m:#import "AudioPlayerViewController.h"@implementation AudioPlayerViewController- (void)viewDidLoad{
[super viewDidLoad];
recordEncoding = ENC_AAC;}-(IBAction) startRecording{NSLog(@"startRecording");[audioRecorder release];audioRecorder = nil;// Init audio with record capabilityAVAudioSession *audioSession = [AVAudioSession sharedInstance];[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];if(recordEncoding == ENC_PCM){
[recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
[recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
[recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; }else{
NSNumber *formatObject;
switch (recordEncoding) {
case (ENC_AAC):
formatObject = [NSNumber numberWithInt: kAudioFormatMPEG4AAC];
break;
case (ENC_ALAC):
formatObject = [NSNumber numberWithInt: kAudioFormatAppleLossless];
break;
case (ENC_IMA4):
formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
break;
case (ENC_ILBC):
formatObject = [NSNumber numberWithInt: kAudioFormatiLBC];
break;
case (ENC_ULAW):
formatObject = [NSNumber numberWithInt: kAudioFormatULaw];
break;
default:
formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
}
[recordSettings setObject:formatObject forKey: AVFormatIDKey];
[recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
[recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
[recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey];}NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.caf", [[NSBundle mainBundle] resourcePath]]];NSError *error = nil;audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];if ([audioRecorder prepareToRecord] == YES){
[audioRecorder record];}else {
int errorCode = CFSwapInt32HostToBig ([error code]);
NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode); }NSLog(@"recording");}-(IBAction) stopRecording{NSLog(@"stopRecording");[audioRecorder stop];NSLog(@"stopped");}-(IBAction) playRecording{NSLog(@"playRecording");// Init audio with playback capabilityAVAudioSession *audioSession = [AVAudioSession sharedInstance];[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.caf", [[NSBundle mainBundle] resourcePath]]];NSError *error;audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];audioPlayer.numberOfLoops = 0;[audioPlayer play];NSLog(@"playing");}-(IBAction) stopPlaying{NSLog(@"stopPlaying");[audioPlayer stop];NSLog(@"stopped");}- (void)dealloc{[audioPlayer release];[audioRecorder release];[super dealloc];}@end希望这能帮到你们中的一些人。
哆啦的时光机
摇曳的蔷薇虽然这是一个已回答的问题(有点旧),但我决定将我的完整工作代码发布给其他发现很难找到良好工作(开箱即用)播放和录制示例的人-包括编码、pcm、通过扬声器播放、写在这里的文件:AudioPlayerViewController.h:#import <UIKit/UIKit.h>#import <AVFoundation/AVFoundation.h>@interface AudioPlayerViewController : UIViewController {AVAudioPlayer *audioPlayer;AVAudioRecorder *audioRecorder;int recordEncoding;enum{
ENC_AAC = 1,
ENC_ALAC = 2,
ENC_IMA4 = 3,
ENC_ILBC = 4,
ENC_ULAW = 5,
ENC_PCM = 6,} encodingTypes;}-(IBAction) startRecording;-(IBAction) stopRecording;-(IBAction) playRecording;-(IBAction) stopPlaying;@endAudioPlayerViewController.m:#import "AudioPlayerViewController.h"@implementation AudioPlayerViewController- (void)viewDidLoad{
[super viewDidLoad];
recordEncoding = ENC_AAC;}-(IBAction) startRecording{NSLog(@"startRecording");[audioRecorder release];audioRecorder = nil;// Init audio with record capabilityAVAudioSession *audioSession = [AVAudioSession sharedInstance];[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];if(recordEncoding == ENC_PCM){
[recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
[recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
[recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; }else{
NSNumber *formatObject;
switch (recordEncoding) {
case (ENC_AAC):
formatObject = [NSNumber numberWithInt: kAudioFormatMPEG4AAC];
break;
case (ENC_ALAC):
formatObject = [NSNumber numberWithInt: kAudioFormatAppleLossless];
break;
case (ENC_IMA4):
formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
break;
case (ENC_ILBC):
formatObject = [NSNumber numberWithInt: kAudioFormatiLBC];
break;
case (ENC_ULAW):
formatObject = [NSNumber numberWithInt: kAudioFormatULaw];
break;
default:
formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
}
[recordSettings setObject:formatObject forKey: AVFormatIDKey];
[recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
[recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
[recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey];}NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.caf", [[NSBundle mainBundle] resourcePath]]];NSError *error = nil;audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];if ([audioRecorder prepareToRecord] == YES){
[audioRecorder record];}else {
int errorCode = CFSwapInt32HostToBig ([error code]);
NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode); }NSLog(@"recording");}-(IBAction) stopRecording{NSLog(@"stopRecording");[audioRecorder stop];NSLog(@"stopped");}-(IBAction) playRecording{NSLog(@"playRecording");// Init audio with playback capabilityAVAudioSession *audioSession = [AVAudioSession sharedInstance];[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.caf", [[NSBundle mainBundle] resourcePath]]];NSError *error;audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];audioPlayer.numberOfLoops = 0;[audioPlayer play];NSLog(@"playing");}-(IBAction) stopPlaying{NSLog(@"stopPlaying");[audioPlayer stop];NSLog(@"stopped");}- (void)dealloc{[audioPlayer release];[audioRecorder release];[super dealloc];}@end希望这能帮到你们中的一些人。