我一直在阅读关于在Apple Watch
播放声音的一些帖子,它看起来不可能......但是,这些帖子处理音频文件,并且我想知道是否可以播放至少系统的声音,如果通知传送到手表可以播放系统声音,如iPhone和iPad上的通知。我没有在Apple Watch Human Interface Guidelines和Apple Watch Programming Guide中发现任何与声音无关的内容。WatchKit:允许系统声音?
谢谢
我一直在阅读关于在Apple Watch
播放声音的一些帖子,它看起来不可能......但是,这些帖子处理音频文件,并且我想知道是否可以播放至少系统的声音,如果通知传送到手表可以播放系统声音,如iPhone和iPad上的通知。我没有在Apple Watch Human Interface Guidelines和Apple Watch Programming Guide中发现任何与声音无关的内容。WatchKit:允许系统声音?
谢谢
通过使用WatchKit API无法播放任何音频。
在watchOS 2中,现在可以实现了。由于NDA,我不能详细说明。请查阅Apple提供的开发者文档。
您可以加载音频文件或系统声音,并使用Apple Watch扬声器播放。使用WKAudioFilePlayer用于播放音频文件所需的蓝牙耳机连接到Apple Watch以播放声音。
播放任何为数不多的触觉反馈使用振动WKInterfaceDevice声音:
[[WKInterfaceDevice currentDevice] playHaptic:WKHapticTypeFailure]
要播放任何音频文件,使用AVAudioEngine:
1)添加AVFoundation.framework到您的框架列表。
2)在h文件,把
#import <AVFoundation/AVFoundation.h>
@property (nonatomic) AVAudioEngine *audioEngine;
@property (nonatomic) AVAudioPlayerNode *playerNode;
@property (nonatomic) AVAudioFile *audioFile;
@property (nonatomic) AVAudioPCMBuffer *audioPCMBuffer;
3).m文件,把
NSError *error = nil;
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"alarm" withExtension:@"wav"];
self.audioFile = [[AVAudioFile alloc] initForReading:fileURL error:&error];
if (error) {
NSLog(@"error");
return;
}
self.audioEngine = [[AVAudioEngine alloc]init];
AVAudioFormat *audioFormat = self.audioFile.processingFormat;
AVAudioFrameCount length = (AVAudioFrameCount)self.audioFile.length;
self.audioPCMBuffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:audioFormat frameCapacity:length];
[self.audioFile readIntoBuffer:self.audioPCMBuffer error:nil];
AVAudioMixerNode *mixerNode = [self.audioEngine mainMixerNode];
self.playerNode = [[AVAudioPlayerNode alloc] init];
[self.audioEngine attachNode:self.playerNode];
[self.audioEngine connect:self.playerNode to:mixerNode format:self.audioFile.processingFormat];
[self.audioEngine startAndReturnError:&error];
[self.playerNode scheduleFile:self.audioFile atTime:nil completionHandler:nil];
[self.playerNode play];
可以使用这些类从watchOS 2.0进一步。有一个playHaptic:和AVAudioEngine类的文档。
我希望这会帮助你解决你的问题。
Jordi是正确的,这对于当前的WatchKit实现来说是不可能的。您只能播放iOS应用中的音频。 – cnoon 2015-03-03 16:43:22