在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") } }
另外,还需要将蓝牙耳机连接到手表。
- 当试图在实际的Apple Watch上构build和运行WatchKit扩展时,如何避免“Error Launching”AppName“WatchKit Extension”错误?
- 如何在Apple Watch中将modal segue的标签“取消”更改
- 如何自动设置Watchkit应用程序目标的版本和内部版本号
- 无法find接口控制器类“InterfaceController”来实例化
- 在iOS模拟器问题中运行Apple Watch
- 苹果手表设置背景图像
- HKWorkoutSession:当watchOS中的屏幕closures时,没有获得心率
- WatchKit错误 – 无法find接口控制器类“'来实例化
- 实时读取Apple Watch syslog(NSLog())