在Apple Watchkit中播放声音

编程入门 行业动态 更新时间:2024-10-24 21:34:52
本文介绍了在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中播放声音

本文发布于:2023-07-11 12:10:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1094007.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声音   Apple   Watchkit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!