2014-01-22 50 views
3

我正在開發一個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時。

+1

控制檯中的任何錯誤?數據中的html和mp3文件都存儲在同一個文件夾中嗎?如果是這樣,也許嘗試使用絕對路徑:''new Audio(「+ data.url(」notification-1.mp3「)+」)。play();「' – willlma

+0

@willlma控制檯中沒有錯誤,使用data.url沒有幫助不幸 – JackW

+0

實際上,這似乎工作(出於某種原因,不是第一次)。謝謝!隨意發佈它作爲答案,我會標記爲解決。 – JackW

回答

2

正如在評論中指出,嘗試在contentScript字符串使用絕對網址:

"new Audio("+data.url("notification-1.mp3")+").play();" 
+0

這對我來說不起作用,因爲'Audio'的參數必須是一個字符串(看似)。工作解決方案:''new Audio(\'「+ data.url(」notification-1.mp3「)+」\')。play();「' – anderstood