是否可以使用 Apple Watch Simulator 进行录音?
Is audio recording possible using Apple Watch Simulator?
我正在尝试使用 Apple Watch Simulator(Watch OS 2 beta) 创建录音功能。但是我在调用 presentAudioRecorderControllerWithOutputURL.
时出现以下错误
错误:错误域=com.apple.watchkit.errors代码=3“(空)”
-(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 调用,您可能需要一个真正的手表来测试。请用结果更新我们。
给你,我认为其他答案不如这个完整或正确(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")
}
})
}
我正在尝试使用 Apple Watch Simulator(Watch OS 2 beta) 创建录音功能。但是我在调用 presentAudioRecorderControllerWithOutputURL.
时出现以下错误错误:错误域=com.apple.watchkit.errors代码=3“(空)”
-(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 调用,您可能需要一个真正的手表来测试。请用结果更新我们。
给你,我认为其他答案不如这个完整或正确(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")
}
})
}