Play sound from Apple Watch speaker

拈花ヽ惹草 提交于 2019-11-27 13:10:36

问题


Is there a way to play sound out of the Apple Watch's speaker? I have been unable to find any documentation online.


回答1:


This is now possible as of watchOS 2 using WKAudioFilePlayer or WKInterfaceMovie.

NSURL *assetURL = [[NSBundle mainBundle] URLForResource:@"file" withExtension:@"wav"];

WKAudioFilePlayer example:

WKAudioFileAsset *asset = [WKAudioFileAsset assetWithURL:assetURL];
WKAudioFilePlayerItem *playerItem = [WKAudioFilePlayerItem playerItemWithAsset:asset];
WKAudioFilePlayer *audioFilePlayer = [WKAudioFilePlayer playerWithPlayerItem:playerItem];
[audioFilePlayer play];

WKInterfaceMovie example:

[self presentMediaPlayerControllerWithURL:assetURL options:nil completion:nil];



回答2:


It is not possible to play sound out of the Apple Watch's speaker, but you can trigger playing a sound file on the iPhone, here is thread about it




回答3:


  • presentMediaPlayerControllerWithURL:options:completion: (New in watchOS 2.0)

URL The URL of the media file you want to play. The URL must specify a file; streamed media is not supported. The file may contain audio, video, or both.

If you specify a URL for a file on a remote server, this method downloads the file first and displays a progress indicator showing the progress of the operation. Because WatchKit uses App Transport Security (ATS) when downloading files from a web server, the file must be on a secure server, and the URL must use the https scheme. If your server does not support ATS–level security, download the file yourself before playing it.

Use sharedcontainer, watch extensions, for storing the file.

Any audio you play using this method is routed to a paired Bluetooth audio device if one is available. If no Bluetooth audio device is available, audio is routed to to the Apple Watch speaker.




回答4:


For WatchOS3 the answer to the original question is WKInterfaceInlineMovie https://developer.apple.com/reference/watchkit/wkinterfaceinlinemovie

You can hide the widget so it does not alter your interface design. It plays audio files through the watch's speaker if no bluetooth speaker is connected.




回答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
        {

        }

    }

I've used this to play a custom sound from the apple watch(4.3) speaker and worked just fine. Don't forget to set the audio file target membership to the watch kit.




回答6:


In the InterfaceController.swift file

Works Fine In Simulator & Device Also Available in latest WatchOS 5

import AVFoundation

var player = AVAudioPlayer()
let audioSession = AVAudioSession.sharedInstance()


    override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()

    do {
        // Working Reroutes to headset
        //            try session.setCategory(AVAudioSession.Category.playback,
        //                                    mode: .default,
        //                                    policy: .longForm,
        //                                    options: [])

        // Plays in watch speaker
        try audioSession.setCategory(AVAudioSession.Category.playback,
                                mode: .default,
                                policy: .default,
                                options: [])
    } catch let error {
        fatalError("*** Unable to set up the audio session: \(error.localizedDescription) ***")
    }
    if let path = Bundle.main.url(forResource: "piano", withExtension: "mp3") {
        let fileUrl = path
        do{
            player = try AVAudioPlayer(contentsOf: fileUrl)
        }
        catch
        {
            print("*** Unable to set up the audio player: \(error.localizedDescription) ***")
            // Handle the error here.
            return
        }
    }
}

Use this code to call the audio session and play the audio inside the play button action or on whichever case you want the audio to play.

     audioSession.activate(options: []) { (success, error) in
            guard error == nil else {
                print("*** error occurred: \(error!.localizedDescription) 
                     ***")
                // Handle the error here.
                return
            }
            if(success){
                // Play the audio file.
                self.player.play()
            }
        }

If you are still not being able to play audio , it must be because you are getting a nil value in the audio file , the audio file specified would not be in the right bundle you are searching for



来源:https://stackoverflow.com/questions/28980152/play-sound-from-apple-watch-speaker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!