我正在開發一個Firefox擴展,我希望它發揮事件發生時的通知聲音。但是,儘管遵循Play audio from firefox extension's data directory的說明,但聲音無法播放。它存儲在我的數據目錄中。這裏是我的代碼:firefox addon SDK不播放音頻使用新的音頻
var pageworker = require ("sdk/page-worker");
let { setTimeout } = require("sdk/timers"); //setTimeout is not enabled by default in ff extensions
function playSound(sound){
console.log("playing sound: "+sound);
var soundPlayer = pageworker.Page({
contentURL: data.url("blank.html"),
contentScript: "new Audio('notification-1.mp3').play();console.log('the audio did run')",//This is where it should play, but it doesn't, even if I remove the console.log
//The script does work if I type it into the javascript console, but replace the file name with a file:/// URL
});
setTimeout(function(){soundPlayer.destroy();},5000);//destroy the sound player after 5 seconds
}
但雖然兩者的console.log的是所謂的,沒有音頻播放過。 這是在XPI和使用cfx時。
控制檯中的任何錯誤?數據中的html和mp3文件都存儲在同一個文件夾中嗎?如果是這樣,也許嘗試使用絕對路徑:''new Audio(「+ data.url(」notification-1.mp3「)+」)。play();「' – willlma
@willlma控制檯中沒有錯誤,使用data.url沒有幫助不幸 – JackW
實際上,這似乎工作(出於某種原因,不是第一次)。謝謝!隨意發佈它作爲答案,我會標記爲解決。 – JackW