我正在尝试使用Apple Watch Simulator(Watch OS 2 beta)创建录音功能。但是在调用 presentAudioRecorderControllerWithOutputURL 时出现以下错误。
错误:错误域= com.apple.watchkit.errors代码= 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);
}];
}
答案 0 :(得分:3)
Watchkit错误代码3是Watchkit无效参数错误。看起来错误可能在您的输出文件路径中。您将附加名称.caf,它不是受支持的音频文件输出类型。从文档中为URL参数说明:
存储录制输出的URL。文件扩展名决定了要录制的音频类型。您可以指定扩展名.wav,.mp4和.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调用,您可能需要一个真正的手表来测试。请用结果更新我们。
答案 1 :(得分:0)
在这里,我不认为其他答案与此完整或正确(WatchOS 2.1,2016年1月):
func recordAudio(){
let duration = NSTimeInterval(5)
let recordOptions =
[WKAudioRecorderControllerOptionsMaximumDurationKey : duration]
//CONSTRUCT AUDIO FILE URL
let fileManager = NSFileManager.defaultManager()
let container = fileManager.containerURLForSecurityApplicationGroupIdentifier("group.artificialsolutions.applewatch")
let fileName = "audio.wav"
saveUrl = container?.URLByAppendingPathComponent(fileName)
presentAudioRecorderControllerWithOutputURL(saveUrl!,
preset: .NarrowBandSpeech,
options: recordOptions,
completion: { saved, error in
if let err = error {
print(err.description)
}
if saved {
print("Audio Saved")
}
})
}