Can the Apple Watch use AVFoundation?

2019-02-18 04:59发布

Can the Apple Watch use AVFoundation? More specifically, can AVAudioPlayer and AVAudioRecorder work?

I am trying to make an app that lets you record a sound to the Apple Watch and have it play it back using the audioplayer. Thanks

4条回答
虎瘦雄心在
2楼-- · 2019-02-18 05:33

UPDATE 11/28/15

This answer applies to WatchKit and WatchOS 1.x. Though I have not tested myself, WatchOS 2.x offers different support, including hardware. I can confirm that as of XCode 7.x, the compiler behaves correctly and WatchKit 1.x extensions with calls to AVFoundation won't build.

For WatchOS 1.x and XCode up to 6.x

Despite the fact that AVAudioRecorder and AVAudioPlayer both work in Simulator Apple Watch, they don't actually work on an actual device! I had opened a bug with Apple back on 5/5/15 (once I got to test an App I had written on my actual watch.) My app (a wrist audio recorded) indeed worked beautifully on Simulator. Yet on the actual watch, it would install and run, but the AVAudioRecorder "record" message would simply never toggle into "recording". Interestingly, the code does not throw any exceptions anywhere. It simply does not record!

I received a reply to my Apple Bug today 5/28/15 that "there are no plans to address this" based on "AVFoundation isn't supported on the watch." No word on whether or not Simulator will be updated so that AVFoundation also fails to work.

So, for now, Apple Watch is limited to controlling recording "on the phone" via watch extension to phone messaging which is supported in Apple Watch extensions.

查看更多
我想做一个坏孩纸
3楼-- · 2019-02-18 05:42

Try using WKAudioFilePlayerItem ? To use this class to play audio, you need to declare 3 variables:

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

They have different role inside the program. For audioFile is used to define NSURL. For example, if you have file called "bicycle.mp3", you can define the URL path like this :

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

when you have a soundpath, you make it a NSURL inside audioFile:

audioFile = WKAudioFileAsset.init(URL: soundPathURL)

When you have a audioFile, you put it inside the audioPlayerItem

audioItem = WKAudioFilePlayerItem.init(asset: audioFile)

When you have audioItem, you put it inside the audioPlayer

    `audioPlayer = WKAudioFilePlayer(playerItem: audioItem)`

Finally, you can put the code audioPlayer.play() where you want to play the sound

Enjoy!

查看更多
放荡不羁爱自由
4楼-- · 2019-02-18 05:50

Short answer? No. WatchKit doesn't currently offer access to any of the hardware in the Watch.

查看更多
等我变得足够好
5楼-- · 2019-02-18 05:51

finally ended up with this:

@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)
            }
        })
}
查看更多
登录 后发表回答