Firefox的插件SDK不使用新的音频播放音频 [英] firefox addon SDK not playing audio using new Audio

查看:117
本文介绍了Firefox的插件SDK不使用新的音频播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Firefox扩展,并且希望在事件发生时播放通知声音。不过,尽管遵循了从Firefox扩展的数据目录中播放音频 ,声音不起作用。它存储在我的数据目录中。这是我的代码:

pre $ var $ pageworker = require(sdk / page-worker);
let {setTimeout} = require(sdk / timers); // setTimeout在ff扩展中没有默认启用

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('音频没有运行'),//这是它应该播放的地方,但是它不会,即使我删除了console.log
//脚本在我输入到javascript控制台,但用文件替换文件名:/ / / URL
});
setTimeout(function(){soundPlayer.destroy();},5000); // 5秒后销毁声音播放器
}

但是,虽然两个 console.log 都被调用,但没有音频播放。
这是在XPI和使用cfx。

解决方案

正如在评论中指出的,尝试使用绝对 contentScript 字符串中的URL:

 new Audio(+ data .URL( 通知-1.MP3)+ )播放();。 


I'm developing a firefox extension, and I'd like it to play a notification sound when an event occurs. However, despite following the instructions from Play audio from firefox extension's data directory, the sound doesn't play. It's stored in my data directory. Here is my code:

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
}

but although both console.log's are called, no audio ever plays. This is both in the XPI and when using cfx.

解决方案

As noted in the comments, try using an absolute URL in the contentScript string:

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

这篇关于Firefox的插件SDK不使用新的音频播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆