swift - Apple Watch 开始录音后录音音频界面崩溃

标签 swift apple-watch watchos-2

我正在尝试使用 presentAudioRecorderControllerWithOutputURL 方法使用 Apple Watch 录制音频。

我使用的是 Xcode 7.0 beta 5、iOS9 beta、WatchOS 2 beta 和 Swift2。

它在模拟器上运行良好。但是,一旦在实际设备上调用该方法,它就会崩溃。

这是我当前的代码:

@IBAction func onClickSpeech() {
    let filePaths = NSSearchPathForDirectoriesInDomains(
        NSSearchPathDirectory.DocumentDirectory,
        NSSearchPathDomainMask.UserDomainMask,
        true)
    let documentDir = filePaths.first!
    let recSoundURL = documentDir + "/record.m4a"
    let nsUrl = NSURL.fileURLWithPath(recSoundURL)

    let audioOptions = [
        WKAudioRecorderControllerOptionsActionTitleKey  : "Recording title",
        WKAudioRecorderControllerOptionsAlwaysShowActionTitleKey : false,
        WKAudioRecorderControllerOptionsAutorecordKey: true,
        WKAudioRecorderControllerOptionsMaximumDurationKey: NSTimeInterval.infinity
    ]

    presentAudioRecorderControllerWithOutputURL(
        nsUrl,
        preset: WKAudioRecorderPreset.NarrowBandSpeech,
        options: audioOptions as [NSObject : AnyObject]) { (didSave, error) -> Void in
            print("didSave:\(didSave), error:\(error)")
    }
}

recSoundURL 是有效路径。

我在设备日志中遇到的错误:

Aug 18 16:42:12 Sennetts-AppleWatch mediaserverd[283] <Error>: 16:42:12.532 EXCEPTION: [0x1f1ac000] >va> 565: kAudioHardwareUnknownPropertyError: "AudioObjectHasProperty([goin/glob/0]) returned false."
Aug 18 16:42:12 Sennetts-AppleWatch mediaserverd[283] <Error>: 16:42:12.555 ERROR:    [0x1f1ac000] >va> 240: CAException caught by ExceptionBarrier: 2003332927.

任何想法将不胜感激,因为我无法弄清楚这个。谢谢。

最佳答案

请看下面的代码(使用 app group's 共享文件夹)

    @IBAction func recordAudio() {

    let fileName = "audioFile.wav"

    let fileManager = NSFileManager.defaultManager()
    let container = fileManager.containerURLForSecurityApplicationGroupIdentifier(Constants.kAppGroupId)
    self.saveUrl = container?.URLByAppendingPathComponent(fileName)


    let duration = NSTimeInterval(120)
    let recordOptions = [WKAudioRecorderControllerOptionsMaximumDurationKey : duration]
    presentAudioRecorderControllerWithOutputURL(self.saveUrl!,
        preset: .WideBandSpeech,
        options: recordOptions,
        completion: { saved, error in

            if let err = error {
                print(err.description)
            }
            if saved {
                // Write code to execute when audio file gets saved.

            }
    })
}

关于swift - Apple Watch 开始录音后录音音频界面崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32084650/

相关文章:

ios - 苹果 watch : Status bar not visible after presenting a modal screen from page based navigation

ios - WCSession 和今日小部件

ios - 如何使用 Watch Connectivity 传输 UIImage

arrays - Swift - 具有特定类类型的数组

ios - 1 个 iOS 应用程序的多个 WatchKit 应用程序

ios - Watchkit 错误 : WatchKit Extension contains multiple WatchKit apps

objective-c - TwoPieceImage 复杂化

ios - 如何调整 swiftUI 中 HStack 元素之间的间距?

swift - 将类型存储在变量中以便以后像类型一样使用

ios - swift 核心数据 : How to fetch all items in array?