Playing sound in Apple Watchkit

前端 未结 1 1285
栀梦
栀梦 2020-12-20 15:25

I\'m trying to play a sound through the watchkit extension using WKAudioFilePlayer however no sound gets played. I\'m using the haptic feedback code as a sort of debug to be

相关标签:
1条回答
  • 2020-12-20 15:57

    I implemented as follows and it worked fine. (Sorry, it's Swift)

    1) Define a property for the player object

    var player: WKAudioFilePlayer!
    

    2) Setup the asset and player when it's awaking

    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)
    
        let filePath = NSBundle.mainBundle().pathForResource("se_tap", ofType: "m4a")!
        let fileUrl = NSURL.fileURLWithPath(filePath)
        let asset = WKAudioFileAsset(URL: fileUrl)
        let playerItem = WKAudioFilePlayerItem(asset: asset)
        player = WKAudioFilePlayer(playerItem: playerItem)
    }
    

    3) Play if the player is ready to play.

    @IBAction func playBtnTapped() {
        switch player.status {
        case .ReadyToPlay:
            player.play()
        case .Failed:
            print("failed")
        case .Unknown:
            print("unknown")
        }
    }
    

    In addition, it needed to connect a Bluetooth Headset with watch.

    0 讨论(0)
提交回复
热议问题