首页 文章

Apple Watch 可以使用 AVFoundation 吗?

提问于
浏览
4

Apple Watch 可以使用AVFoundation吗?更具体地说,AVAudioPlayerAVAudioRecorder可以工作吗?

我正在尝试制作一款应用程序,让您可以将声音录制到 Apple Watch 并让它使用音频播放器播放。谢谢

4 回答

  • 1

    更新 11/28/15

    这个答案适用于 WatchKit 和 WatchOS 1.x。虽然我没有测试过自己,但 WatchOS 2.x 提供了不同的支持,包括硬件。我可以确认,从 XCode 7.x 开始,编译器行为正常,并且不会构建调用 AVFoundation 的 WatchKit 1.x 扩展。

    对于 WatchOS 1.x 和 XCode 最多 6.x

    尽管 AVAudioRecorder 和 AVAudioPlayer 都在 Simulator Apple Watch 中运行,但它们实际上并不适用于实际的设备!我在 5/5/15 上打开了一个关于 Apple 的错误(一旦我测试了我在我的实际 watch.)我的应用程序(录制的手腕音频)上写的应用程序确实在模拟器上工作得很漂亮。但在实际的手表上,它会安装并运行,但 AVAudioRecorder“记录”消息将永远不会切换到“录制”。有趣的是,代码不会在任何地方抛出任何异常。它根本不记录!

    我今天收到了对我的 Apple Bug 的回复 5/28/15“基于”手表不支持 AVFoundation,“没有计划解决这个问题”。没有关于是否更新模拟器的说法,以致 AVFoundation 也无法工作。

    因此,目前,Apple Watch 仅限于通过 Apple Watch 扩展支持的手机短信的手表扩展控制“在手机上”录制。

  • 1

    尝试使用WKAudioFilePlayerItem?要使用此类播放音频,您需要声明 3 个变量:

    var audioFile: WKAudioFileAsset!
    var audioItem: WKAudioFilePlayerItem!
    var audioPlayer: WKAudioFilePlayer!
    

    他们在计划中扮演着不同的角色。因为audioFile用于定义NSURL。例如,如果您有名为“bicycle.mp3”的文件,则可以像这样定义 URL 路径:

    let soundPath = NSBundle.mainBundle().pathForResource("bicycle", ofType: "mp3")
    print("load file")
    

    如果你有soundpath,你可以在 audioFile 中创建一个 NSURL:

    audioFile = WKAudioFileAsset.init(URL: soundPathURL)

    当你有audioFile时,你把它放在audioPlayerItem里面

    audioItem = WKAudioFilePlayerItem.init(asset: audioFile)

    当你有audioItem时,你把它放在audioPlayer里面

    `audioPlayer = WKAudioFilePlayer(playerItem: audioItem)`
    

    最后,您可以将代码audioPlayer.play()放在要播放声音的位置

    请享用!

  • 0

    简短的回答?不可以.WatchKit 目前不提供对 Watch 中任何硬件的访问。

  • 0

    终于结束了这个:

    @IBAction func playButtonTapped() {
            let options = [WKMediaPlayerControllerOptionsAutoplayKey : "true"]
            let filePath = NSBundle.mainBundle().pathForResource("1button", ofType: "wav")!
            let fileUrl = NSURL.fileURLWithPath(filePath)
            presentMediaPlayerControllerWithURL(fileUrl, options: options,
                                                completion: { didPlayToEnd, endTime, error in
                                                    if let err = error {
                                                        print(err.description)
                }
            })
    }
    

相关问题