我正在开发一个 chrome 扩展,它利用“microsoft-cognitiveservices-speech-sdk”将任何网页上的选定文本转换为语音。这个 chrome 扩展的核心代码如下:
import { SpeakerAudioDestination } from "microsoft-cognitiveservices-speech-sdk";
import { getSettings } from "./utils/setting";
let player: SpeakerAudioDestination | null = null;
export const textToSpeech = async (
text
) => {
if (player) {
player.pause();
player.close();
}
const voiceName = await getSettings("voice");
return new Promise( async(resolve, reject) => {
const voiceName = await getSettings("voice");
const speechConfig = window.SpeechSDK.SpeechConfig.fromSubscription(...);
speechConfig.speechRecognitionLanguage = "...";
speechConfig.speechSynthesisVoiceName = voiceName;
player = new window.SpeechSDK.SpeakerAudioDestination();
const audioConfig = window.SpeechSDK.AudioConfig.fromSpeakerOutput(player);
const speechSynthesizer = new window.SpeechSDK.SpeechSynthesizer(speechConfig, audioConfig);
speechSynthesizer.speakTextAsync(
text,
result => {
resolve(result);
speechSynthesizer.close();
},
error => {
console.log(error);
reject(error);
speechSynthesizer.close();
}
);
});
};
但是,我遇到了一些网站设置内容安全策略阻止扩展播放语音的问题。例如,下面包含在 [] 中的错误消息来自特定网站:
[拒绝从 'blob:https://developer.mozilla.org/57fb5680-66f6-49f8-b953-0fb9f2b140df' 加载媒体,因为它违反了以下内容安全策略指令:“media-src 'self' archive.org videos.cdn.mozilla.net.]
我该如何解决这个问题? 如果 SpeechSDK.AudioConfig.fromSpeakerOutput(player) 方法生成了一个违反网页安全策略的 blob URL,是否有其他方法可以解决此问题?例如,我们可以改用音频流播放吗?我对这方面不是很熟悉