在Apple Watchkit中播放声音

我试图通过使用WKAudioFilePlayer的watchkit扩展播放声音,但没有声音播放。 我使用触觉反馈代码作为一种debugging,以确保它执行(它)。 作为一个侧面说明,静音没有打开,文件名是正确的。

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)

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

 var player: WKAudioFilePlayer! 

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

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

另外,还需要将蓝牙耳机连接到手表。