【问题标题】:WatchKit: system sounds allowed?WatchKit:允许系统声音?
【发布时间】:2015-05-03 04:09:00
【问题描述】:

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

谢谢

【问题讨论】:

    标签: ios audio watchkit apple-watch system-sounds


    【解决方案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 类有一个文档。

    希望这能帮助您解决问题。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • Jordi 是正确的,这在 WatchKit 的当前实现中是不可能的。您只能从 iOS 应用播放音频。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-12
        • 1970-01-01
        • 2011-07-25
        • 2011-04-25
        • 2012-09-07
        • 1970-01-01
        • 2013-08-20
        相关资源
        最近更新 更多