我正在尝试使用 Apple Watch Simulator(Watch OS 2 beta)创建音频录制功能。但我在调用 presentAudioRecorderControllerWithOutputURL 时收到以下错误。
错误:错误 Domain=com.apple.watchkit.errors Code=3 "(null)"
-(void)didSelectRowWithTag:(NSInteger)tag
{
NSString*strPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *strAudioFileName = [strPath stringByAppendingString:[NSString stringWithFormat:@"/%d.caf",tag]];
NSURL *urlOutPut = [NSURL fileURLWithPath:strAudioFileName];
NSDictionary *dictMaxAudioRec = @{@"WKAudioRecorderControllerOptionsMaximumDurationKey":@1800};
[self presentAudioRecorderControllerWithOutputURL:urlOutPut preset:WKAudioRecorderPresetHighQualityAudio options:dictMaxAudioRec completion:^(BOOL didSave, NSError * error) {
if(didSave)
{
NSLog(@"File Saved....");
}
NSLog(@"%@",error);
}];
}
最佳答案
Watchkit 错误代码 3 是 Watchkit 无效参数错误。看起来错误可能出现在您的输出文件路径中。您正在附加名称 .caf,这不是受支持的音频文件输出类型。从文档中可以看出 URL 参数:
The URL at which to store the recorded output. The filename extension determines the type of audio to record. You may specify the extensions .wav, .mp4, and .m4a.
以下是用于录制音频的示例代码。
let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
let url = NSURL(fileURLWithPath: path.stringByAppendingPathComponent("dictation.wav"))
self.presentAudioRecordingControllerWithOutputURL(url, preset: WKAudioRecordingPreset.NarrowBandSpeech, maximumDuration: 30, actionTitle: "Save") { (didSave, error) -> Void in
if let error = error {
print("error: \(error)")
return
}
if didSave {
print("saved!")
}
}
通过此修复,我仍然不确定模拟器是否支持音频录制。模拟器不支持所有 API 调用,您可能需要真正的 watch 来测试。请向我们通报结果。
关于objective-c - 可以使用 Apple Watch Simulator 进行录音吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31987438/