3

我一直在阅读一些关于在 an 中播放声音的帖子,Apple Watch看起来这是不可能的......但是,这些帖子涉及音频文件,我想知道是否可以至少播放系统声音,如果发送到手表的通知可以像 iPhone 和 iPad 中的通知一样播放系统声音。我在Apple Watch Human Interface GuidelinesApple Watch Programming Guide中都没有找到任何与声音相关的内容。

谢谢

4

3 回答 3

3

使用 WatchKit API 无法播放任何音频。

于 2015-03-02T18:50:42.330 回答
1

您可以加载音频文件或系统声音并使用 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类有一个文档。

我希望这能帮助你解决你的问题。

于 2017-03-02T14:30:43.653 回答
0

在 watchOS 2 中,这现在是可能的。由于 NDA,我无法详细说明。请查看 Apple 提供的开发者文档。

于 2015-06-18T20:42:06.593 回答