简体   繁体   English

简单的html5音频播放器可在浏览器中使用,但不能在chrome扩展中使用

[英]Simple html5 audio player works in browser but not in chrome extension

In the background.js file of my chrome extension i add this : 在我的chrome扩展程序的background.js文件中,添加以下代码:

function loadSong(url) {
    var urlRadio = "mydomain.com";
    document.getElementById("player").src=urlRadio + url;
    document.getElementById("player").load();
    document.getElementById("player").play();
}
window.addEventListener('load', loadSong);

and in html : 并在html中:

<div>
    <audio tabindex="0" id="player" controls="controls">nothing</audio>
    <ul id="playlist">
        <li><a onclick="loadSong('one.mp3')">one</a></li>
        <li><a onclick="loadSong('two.mp3')">two</a></li>
    </ul>
</div>

I added background.js in the homepage , before end tag and in the background part manifest of the chrome extension. 我在首页中,end标签之前和chrome扩展的背景部分清单中添加了background.js。

I want to use this player like a webpage and like a chrome extension, so when the code i put it here : 我想像网页和chrome扩展程序一样使用此播放器,所以当我将代码放在这里时:

  • "Uncaught TypeError: Cannot set property 'src' of null " at the loading in browser, but i can play songs 浏览器加载时出现“未捕获的TypeError:无法将属性'src'设置为null”,但是我可以播放歌曲
  • "Uncaught TypeError: Cannot set property 'src' of null " at the loading in chrome extension, can't play songs chrome扩展加载时出现“ Uncaught TypeError:无法将属性'src'设置为null”,无法播放歌曲

what's the problem ? 有什么问题 ?

There are two errors in ur script. 您的脚本中有两个错误。 First one is path of audio file and second in ur function call. 第一个是音频文件的路径,第二个是ur函数调用。 Here is the sample u can try like this 这是您可以尝试的示例

Html: HTML:

<div>
<audio tabindex="0" id="player" controls="controls">nothing</audio>
<ul id="playlist">
    <li><a onclick="loadSong('one.mp3')">one</a></li>
    <li><a onclick="loadSong('two.mp3')">two</a></li>
</ul>
</div>

Script: 脚本:

function loadSong(url) {
var urlRadio = "path/";
document.getElementById("player").src=urlRadio + url;
document.getElementById("player").load();
document.getElementById("player").play();
}
window.addEventListener('load', loadSong("your audio file name example one.mp3"));

Your First error : Wrong path 您的第一个错误:错误的路径

var urlRadio = "mydomain.com";
document.getElementById("player").src=urlRadio + url;

in this case (urlRadio + url) the src will become like (mydomain.comone.mp3) this is wrong path it has to be like mydomain.com/one.mp3 . 在这种情况下(urlRadio + url),src将变得像(mydomain.comone.mp3),这是错误的路径,它必须像mydomain.com/one.mp3一样。

Your Second error : Function call without passing parameter 您的第二个错误:没有传递参数的函数调用

 window.addEventListener('load', loadSong);

u have to pass the url value to loadSong function something like this loadSong("one.mp3") 您必须将url值传递给loadSong函数,例如:loadSong(“ one.mp3”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM