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

查看:31
本文介绍了在 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) 如果玩家准备好可以玩.

@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.

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

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