2014-01-22 42 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