使用chrome.extension.getBackgroundPage()播放音频文件 [英] Use chrome.extension.getBackgroundPage() to play an audio file

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

问题描述

我试图开发基于语音输入事件播放声音的基本Chrome扩展程序(所有事情都发生在后台)。

这是我的清单

  {
manifest_version:2,
name:Test123,
版本:1.0,
description:我的音频扩展名,

图标:{
128:icon.jpg$ b $ b,

权限:[
实验性,
背景
],

背景 :{
persistent:true,
scripts:[
bg.js

},
}

bg.js 中,我得到 window 具有的对象,var chrome.extension.getBackgroundPage(); ,但我无法从中找到该做什么。



如何播放音频文件? 这里 是解决方案!



首先,将音乐文件加载到后台页面是一个很好的解决方案。但是你必须从弹出页面触发你的音频文件的执行。这是脚本:

  //在popup.html 
< div id =play-it>> ;播放< / DIV>

//不要忘记使用外部脚本文件
$('#play-it')。click(function(){
chrome.extension.sendMessage({action :play})
});

然后在bg.js中,您将收听消息

  //在bg.js 
var audioElement = document.createElement('audio');
audioElement.setAttribute(preload,auto);
audioElement.autobuffer = true;

var source1 = document.createElement('source');
source1.type ='audio / mpeg';
source1.src ='http://lezotre.free.fr/Mp3/disco.mp3';
audioElement.appendChild(source1);

chrome.extension.onMessage.addListener(
function(request,sender,sendResponse){
if(request.action ==play){
audioElement .load;
audioElement.play();
}
});

您可以在官方的google chrome文档中找到更多关于消息传递的信息: http://developer.chrome.com/extensions/messaging.html


I’m trying to develop a basic Chrome extension that plays a sound, based on a speech input event (everything happens in the background).

Here’s my manifest

{
  "manifest_version" : 2,
  "name"             : "Test123",
  "version"          : "1.0",
  "description"      : "My audio extension",

  "icons"            : {
    "128"              : "icon.jpg"
  },

  "permissions"      : [
    "experimental",
    "background"
  ],

  "background"       : {
    "persistent"       : true,
    "scripts"          : [
      "bg.js"
    ]
  },
}

In bg.js, I get the window object with var chrome.extension.getBackgroundPage();, but I can’t figure what to do from there.

How can I play an audio file?

解决方案

Here is the solution !

First of all, loading the music file in a background page is the good solution. But you have to trigger the execution of your audio file from the popup page. Here is the script :

 // In popup.html 
 <div id="play-it">Play</div>

 // dont forget to use an external script file
 $('#play-it').click(function(){
     chrome.extension.sendMessage({action: "play"})
 });

Then in the bg.js you will listen to the Message

 // In bg.js
 var audioElement = document.createElement('audio');
 audioElement.setAttribute("preload", "auto");
 audioElement.autobuffer = true;

 var source1 = document.createElement('source');
 source1.type= 'audio/mpeg';
 source1.src= 'http://lezotre.free.fr/Mp3/disco.mp3';
 audioElement.appendChild(source1);

 chrome.extension.onMessage.addListener(
    function(request, sender, sendResponse) {
      if (request.action == "play"){
          audioElement.load;
          audioElement.play();
      }
});

You will find more information on message passing on the official google chrome documentation : http://developer.chrome.com/extensions/messaging.html

这篇关于使用chrome.extension.getBackgroundPage()播放音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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