iOS 设备上的 Google 文字转语音音量太低

发布于 2025-01-18 12:42:08 字数 2630 浏览 0 评论 0原文

我在网站中使用 Google TTS API 来读出后端服务器返回的文本字符串。我们已按照博客的步骤设置音频从浏览器麦克风流式传输到服务器,并将 TTS 从服务器返回到浏览器。此实现在计算机和 Android 设备上运行良好。但是,在 iOS 设备上,TTS 播放仅使用耳机扬声器,而不使用两个(耳机扬声器和主扬声器)扬声器。此问题导致 iOS 设备上的音频播放音量过低。

  • 我们尝试在 iOS 设备上使用音频标签播放本地 wav 文件,并且使用两个扬声器。

我们怀疑这一点: 但是,TTS 播放需要转换 arrayBuffer转换为 wav 类型的 blob 对象,并且在该转换中可能会损失一些音频音量? 我们如何设置 wav blob 标头,使其不会丢失任何信息/音量?

请在底部找到代码片段以更多地理解上述语句。

  • 我们在后端(服务器端)语音服务(该服务包含 STT、dialogFlow 和 TTS 功能)和前端(在 playOutput 功能中)设置 OPUS 音频编码。这在 iOS 上不起作用。

    <块引用>

    const blob = new Blob([arrayBuffer], { 'type' : 'audio/ogg; 编解码器=opus' });

  • 我们尝试使用 WebAudio API 播放 TTS AudioContext 对象,但它被 iOS 阻止。错误是:

    <块引用>

    未处理的承诺拒绝:NotAllowedError:请求不是 当前上下文中的用户代理或平台允许, 可能是因为用户拒绝了权限。

我们希望通过使用两个可用的手机扬声器,TTS 播放在 iOS 上具有更高的音量。欢迎提出建议。提前致谢。

我们创建了一个“结果”侦听器,一旦在浏览器中检索到来自服务器端的数据,该侦听器就会运行。这将使用此代码片段之后显示的 playOutput 方法

//creating an io object
const socketio = io();
//connecting to the socket
const socket = socketio.on('connect', function() {});
    socketio.on('results’', function (data) {
    console.log(data);
    //playing the data retrieved from server side
    playOutput(data);
});

这是浏览器上音频播放功能的代码:

   const audioGcp = document.getElementById("audioGcp"); 
   //audioGcp is the id of the <audio> HTML tag
   function playOutput(arrayBuffer) {
   //arrayBuffer is the data sent from the server-side via socket to the frontend.
   //This arrayBuffer is then converted to a blob object that can be played back.
        try {
            if (arrayBuffer.byteLength > 0) {
                console.log(`--- Returned TTS audio buffer size: ${arrayBuffer.byteLength}`);
                //creating blob from arrayBuffer of type: wav
                const blob = new Blob([arrayBuffer], { type: "audio/wav" });
                //changing the source of the audio tag which plays back the audio contained in the blob object
                audioGcp.src = windowURL.createObjectURL(blob);
                //playing the audio source
                audioGcp.play();
            }
        } 
        catch(e) {
            console.log(`Play Audio Error: ${e}`);
        }
    }

任何有关 iOS 中音频低的原因的线索将不胜感激。

I am using Google TTS API in a website to sound out a text string returned by a backend server. We have followed this blog's steps to setup audio streaming from browser microphone to a server and return TTS from server to browser. This implementation works well on computer and Android devices. However, on iOS devices the TTS playback uses only the ear speaker and not both (ear speaker and main speaker) speakers. This issue leads to audio playback volume being too low on iOS devices.

  • We tried to play a local wav file using an audio tag on an iOS device and that was using both the speakers.

<audio src="audio_wav_file_url_here" controls autoplay="true"></audio>

We suspect this: The TTS playback however, requires an arrayBuffer to be converted to a blob object of wav type and maybe there is some loss of audio volume in that conversion? How do we set the wav blob header so that it doesn't lose any information/volume?

Please find the code snippet in the bottom to understand the above statement more.

  • We set OPUS audio encoding on both, backend (server-side) speech service (this service contains the STT, dialogFlow and TTS functions) and frontend(in the playOutput function). This didn’t work on iOS.

    const blob = new Blob([arrayBuffer], { 'type' : 'audio/ogg;
    codecs=opus' });

  • We tried to playback TTS using the WebAudio API's AudioContext object but it gets blocked by iOS. The error being:

    Unhandled Promise Rejection: NotAllowedError: The request is not
    allowed by the user agent or the platform in the current context,
    possibly because the user denied permission.

We expect the TTS playback to have a higher volume on iOS by using both the available phone speakers. Suggestions are welcome. Thanks in advance.

We created a ‘results’ listener, which runs once the data from the server-side is retrieved in the browser. This will use the playOutput method which is shown after this code snippet

//creating an io object
const socketio = io();
//connecting to the socket
const socket = socketio.on('connect', function() {});
    socketio.on('results’', function (data) {
    console.log(data);
    //playing the data retrieved from server side
    playOutput(data);
});

This is the code for the audio playback function on browser:

   const audioGcp = document.getElementById("audioGcp"); 
   //audioGcp is the id of the <audio> HTML tag
   function playOutput(arrayBuffer) {
   //arrayBuffer is the data sent from the server-side via socket to the frontend.
   //This arrayBuffer is then converted to a blob object that can be played back.
        try {
            if (arrayBuffer.byteLength > 0) {
                console.log(`--- Returned TTS audio buffer size: ${arrayBuffer.byteLength}`);
                //creating blob from arrayBuffer of type: wav
                const blob = new Blob([arrayBuffer], { type: "audio/wav" });
                //changing the source of the audio tag which plays back the audio contained in the blob object
                audioGcp.src = windowURL.createObjectURL(blob);
                //playing the audio source
                audioGcp.play();
            }
        } 
        catch(e) {
            console.log(`Play Audio Error: ${e}`);
        }
    }

Any leads as to why the audio is low in iOS would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文