在Apple Watchkit中播放声音 [英] Playing sound in Apple Watchkit

查看:207
本文介绍了在Apple Watchkit中播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WKAudioFilePlayer通过watchkit扩展播放声音但是没有播放声音。我正在使用触觉反馈代码作为一种调试来确保它执行它(它做了)。作为旁注,静音未打开且文件名正确。

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 sure it executes it (which it does). As a side note, mute is not on and the filename is correct.

NSURL *falcon = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"falcon"
                                        ofType:@"mp3"]];
WKAudioFileAsset *asset = [WKAudioFileAsset assetWithURL:falcon];
WKAudioFilePlayerItem *sound = [WKAudioFilePlayerItem playerItemWithAsset:asset];
audioPlayer = [WKAudioFilePlayer playerWithPlayerItem:sound];


[audioPlayer play];

WKInterfaceDevice *device = [WKInterfaceDevice currentDevice];
[device playHaptic:WKHapticTypeClick];


推荐答案

我实现如下,它工作正常。 (对不起,这是Swift)

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

1)为玩家对象定义一个属性

1) Define a property for the player object

var player: WKAudioFilePlayer!

2)在资产和玩家醒来时设置资产和玩家

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)如果玩家准备好玩

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")
    }
}

此外,还需要连接蓝牙耳机用watch。

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

这篇关于在Apple Watchkit中播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆