播放Apple Watch扬声器的声音

fre*_*111 12 iphone audio objective-c ios apple-watch

有没有办法从Apple Watch的扬声器中播放声音?我无法在线找到任何文档.

Ric*_*tos 17

现在可以在watchOS 2中使用WKAudioFilePlayer或使用WKInterfaceMovie.

NSURL *assetURL = [[NSBundle mainBundle] URLForResource:@"file" withExtension:@"wav"];
Run Code Online (Sandbox Code Playgroud)

WKAudioFilePlayer 例:

WKAudioFileAsset *asset = [WKAudioFileAsset assetWithURL:assetURL];
WKAudioFilePlayerItem *playerItem = [WKAudioFilePlayerItem playerItemWithAsset:asset];
WKAudioFilePlayer *audioFilePlayer = [WKAudioFilePlayer playerWithPlayerItem:playerItem];
[audioFilePlayer play];
Run Code Online (Sandbox Code Playgroud)

WKInterfaceMovie 例:

[self presentMediaPlayerControllerWithURL:assetURL options:nil completion:nil];
Run Code Online (Sandbox Code Playgroud)

  • 这实际上不是问题的答案,因为该片段不通过手表的扬声器播放声音文件,而是通过配对的蓝牙耳机:"WatchKit扩展可以通过配对的蓝牙音频耳机启动扩展音频内容的播放." https://developer.apple.com/library/prerelease/watchos/documentation/General/Conceptual/AppleWatch2TransitionGuide/ManagingYourData.html#//apple_ref/doc/uid/TP40015234-CH12-SW6 (9认同)

小智 5

import AVFoundation
var player: AVAudioPlayer?

if let path = Bundle.main.path(forResource: "siren", ofType: "wav") {

        let fileUrl = URL(fileURLWithPath: path)

        do{
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)

            player = try AVAudioPlayer(contentsOf: fileUrl)

            guard let player = player else { return }

            player.play()

        }
        catch
        {

        }

    }
Run Code Online (Sandbox Code Playgroud)

我用它从 Apple Watch(4.3) 扬声器播放自定义声音并且工作得很好。不要忘记将音频文件目标成员资格设置为手表套件。