我正在尝试使用 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);
这是我得到的错误:
Operation could not be completed. (OSStatus error 1718449215.)
我无法弄清楚为什么这不起作用,因为我从网站上获得了很多代码。
乔纳森
最佳答案
您的问题是kAudioFormatMPEGLayer3
遗憾的是,您无法在 iPhone 上将声音编码为 mp3 格式。目前压缩音频的最佳格式是 kAudioFormatAppleIMA4。 (是的,还有 kAudioFormatMPEG4AAC - 更好,但它仅适用于模拟器,但不适用于设备)。 另请注意,在其他系统上解码录制的 kAudioFormatAppleIMA4 文件可能会出现问题,例如windows,所以简单的lpcm格式对于小文件可能更方便。
关于iphone - 操作无法完成。 AVAudioRecorder iPhone SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2450103/