Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i play sound in a chrome extension [duplicate]

I would like to play sound in chrome extension. How can I do it? What should I write in myscript.js file?

I tried to write in myscript.js:

var audio = new Audio("alarm.wav");
audio.play();

and:

document.write('<audio id="player" src="alarm.wav" >');
document.getElementById('player').play();

but it does not work. I did not add anything more, so there are no unfulfilled conditions.

My manifest.json file:

{
  "name": "Alarm",
  "version": "1.0.0",
  "icons": {"64": "icon64.png"},
  "permissions": [
    "http://site1/",
    "http://site2/"
  ],
  "content_scripts": [
    {
      "matches": ["http://site1/", "http://site2/"],
      "js": ["myscript.js"],
      "run_at": "document_end"
    }
  ],
  "manifest_version": 2
}

If I add button to site in myscript.js file, this button works well, but i can't play sound. My audio file is mp3 and is in the same folder as manifest.json and myscript.js, and my myscript.js is:

var myAudio = new Audio();
myAudio.src = "alarm.mp3";
myAudio.play();
like image 297
Przemysław Niedziela Avatar asked Dec 16 '14 02:12

Przemysław Niedziela


People also ask

How to play Chrome audio through separate devices?

How to: Click on the extension icon and choose the output device on the website you want to play audio out of. Note: you will need to authorize the microphone permission that pops up due to chrome restrictions. Once you have done so choose the device again and it will work with the current site in the open chrome tab.

Can Chrome extensions communicate with each other?

In addition to sending messages between different components in your extension, you can use the messaging API to communicate with other extensions.

What is Web_accessible_resources?

Using web_accessible_resources This prevents websites from fingerprinting a browser by examining the extensions it has installed. Note: In Chrome in Manifest V2, an extension's ID is fixed.


2 Answers

Update: as of Chrome Extension Manifest v3, Chrome Extensions have switched to service workers for background pages, so if you are using Manifest v3, it is no longer possible to play audio directly from the background page of your extension. See also: Play a sound from a Service Worker. If you are still using Manifest v2, then go ahead and read the answer below.


The easiest way to play some sound/music using JavaScript is by using an Audio object: you just need to have the file you want to play inside your extension folder, and you can play it like this:

var myAudio = new Audio(chrome.runtime.getURL("path/to/file.mp3"));
myAudio.play();

You can play using play() and pause using pause().

Remember that if you want to play the sound in a content script (or anywhere else that is not under a chrome://extension URL) you'll have to declare the audio file in the web_accessible_resources manifest field:

"web_accessible_resources": [
    "path/to/file.mp3"
]

Working example

You can download a test extension I made from HERE. It plays a sound through a content script when you click anything inside a stackoverflow.com page.

like image 181
Marco Bonelli Avatar answered Sep 22 '22 12:09

Marco Bonelli


According to the documentation, chrome.sound is deprecated in mv3.

An alternative is to use chrome.tts to read a message like "Hey, wake up". :/

Another way is to open a new tab which plays the audio. You could use the chrome.tabs.create() or window.open("https://....", "_blank")

like image 25
Kristiyan Tsvetanov Avatar answered Sep 18 '22 12:09

Kristiyan Tsvetanov