【问题标题】:Hi, Is there a way for the AVSpeech Synthesizer to play audio using either channels while playing audio using a headset?嗨, AVSpeech 合成器有没有办法在使用耳机播放音频的同时使用任一通道播放音频?
【发布时间】:2016-09-09 22:46:53
【问题描述】:

我需要的是单独使用左声道或右声道播放音频。 我知道 AVAudioPlayer 可以使用 pan 属性使用任一通道播放音频。 如果 AVSpeechSynthesizer 无法做到这一点,是否可以使用 AVAudioPlayer 播放话语以便控制通道? 如果我能以某种方式获取 AVSpeechUtterance 的 NSURL 并使用 AVAudioPlayer 播放?

只有以前的类似问题:Any way to control which audio channel AVSpeechSynthesizer outputs to? 我发现没有回答,我想找到一个解决方案。

【问题讨论】:

    标签: ios audio avaudioplayer avspeechsynthesizer


    【解决方案1】:

    是的,您可以使用AVSpeechSynthesizeroutputChannels 属性来执行此操作。 Apple 没有为此发布任何文档,但它的工作方式与 AVAudioPlayerchannelAssignments 属性相同。这将返回一个人类可读的可用频道列表:

    - (NSArray *)getAudioChannelNames {
        // return the name of each channel in each connected audio port
        // examples: Headphones Left/Right, Speaker, DUO-CAPTURE 1/2
        AVAudioSession *session = [AVAudioSession sharedInstance];
        AVAudioSessionRouteDescription *route = [session currentRoute];
        NSArray *outputPorts = [route outputs];
        NSMutableArray *channels = [NSMutableArray array];
        for (AVAudioSessionPortDescription *outputPort in outputPorts) {
            for (AVAudioSessionChannelDescription *channel in outputPort.channels) {
                [channels addObject:channel.channelName];
            }
        }
        return channels;
    }
    

    您或用户可以选择要使用的频道名称。然后将AVSpeechSynthesizer 设置为使用该频道:

    - (void)setSpeechSynthesizer:(AVSpeechSynthesizer *)speechSynthesizer toChannels:(NSArray *)channelNames {
        AVAudioSession *session = [AVAudioSession sharedInstance];
        AVAudioSessionRouteDescription *route = [session currentRoute];
        NSArray *outputPorts = [route outputs];
        NSMutableArray *channelDescriptions = [NSMutableArray array];
        for (NSString *channelName in channelNames) {
            for (AVAudioSessionPortDescription *outputPort in outputPorts) {
                for (AVAudioSessionChannelDescription *channel in outputPort.channels) {
                    if ([channel.channelName isEqualToString:channelName]) {
                        [channelDescriptions addObject:channel];
                    }
                }
            }
        }
        if ([channelDescriptions count]) {
            speechSynthesizer.outputChannels = channelDescriptions;
        }
    }
    

    【讨论】:

    • speechSynthesizer.outputChannels 从 iOS 10 开始支持。* 在 iOS 8.0 中是否有任何选项可以做到这一点
    • 每次我设置输出通道时,通道数组总是空的。任何线索是怎么来的? (我使用 Swift 和 ios 10)
    • @zo_chu 你的意思是getAudioChannelNames 的结果是空的吗?您是否使用AVAudioSessionCategoryMultiRoute 进行音频会话?我不知道还有什么建议。
    • 我已经想通了。我想将合成器路由到不同的音频路由(连接耳机时内置扬声器),但不支持。当我从当前路线选择频道时,它可以工作。
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 2016-06-17
    相关资源
    最近更新 更多