Jon*_*an. 2 iphone objective-c iphone-sdk-3.0 ios-simulator
我正在尝试使用iphone麦克风进行录制:这是我的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"testing.mp3"];
NSURL *url = [NSURL fileURLWithPath:appFile isDirectory:NO];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatMPEGLayer3], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityLow], AVEncoderAudioQualityKey,
nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if ([recorder prepareToRecord] == YES){
[recorder record];
}else {
int errorCode = CFSwapInt32HostToBig ([error code]);
NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode);
}
NSLog(@"BOOL = %d", (int)recorder.recording);
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
Operation could not be completed. (OSStatus error 1718449215.)
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么这不起作用,因为我从网站获得了很多代码.
乔纳森
你的问题是kAudioFormatMPEGLayer3
不幸的是,你不能以mp3格式在iPhone上编码声音.目前,压缩音频的最佳格式是kAudioFormatAppleIMA4.(是的,还有kAudioFormatMPEG4AAC - 甚至更好,但它只适用于模拟器,但不适用于设备).另请注意,在其他系统(例如窗口)上解码记录的kAudioFormatAppleIMA4文件时可能会出现问题,因此对于小文件来说,简单的lpcm格式可能更方便.