可以通过麦克风播放声音的Chrome扩展程序[关闭]

2skhul33  于 2023-08-01  发布在  Go
关注(0)|答案(1)|浏览(99)

已关闭,此问题需要更focused。它目前不接受回答。
**想改善这个问题吗?**更新问题,使其仅通过editing this post关注一个问题。

22天前关闭
Improve this question
我试图创建一个扩展,可以通过麦克风发射声音,mp3的声音。
例如,我希望用户输入一个google meet call或任何其他服务,通过按下popup.html中的一个按钮,会触发一个声音,所有参与对话的人都能听到。

xkrw2x1b

xkrw2x1b1#

使用软件将声音从扬声器重定向到声卡上的麦克风(有许多应用程序),或者您可以使用C#创建自己的
要从chrome扩展播放声音,请使用屏幕外
manifest.json

"permissions": ["offscreen"]

字符串
background.js

/**
 * Plays audio files from extension service workers
 * @param {string} source - path of the audio file
 * @param {number} volume - volume of the playback
 */
async function playSound(source = 'default.wav', volume = 1) {
    await createOffscreen();
    await chrome.runtime.sendMessage({ play: { source, volume } });
}

// Create the offscreen document if it doesn't already exist
async function createOffscreen() {
    if (await chrome.offscreen.hasDocument()) return;
    await chrome.offscreen.createDocument({
        url: 'offscreen.html',
        reasons: ['AUDIO_PLAYBACK'],
        justification: 'testing' // details for using the API
    });
}


offscreen.html

<script src="offscreen.js"></script>
offscreen.js

// Listen for messages from the extension
chrome.runtime.onMessage.addListener(msg => {
    if ('play' in msg) playAudio(msg.play);
});

// Play sound with access to DOM APIs
function playAudio({ source, volume }) {
    const audio = new Audio(source);
    audio.volume = volume;
    audio.play();
}

相关问题