如何使用Javascript播放mp3? [英] How to play a mp3 using Javascript?

查看:174
本文介绍了如何使用Javascript播放mp3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上有几个mp3目录。
我使用php动态地在网站中创建它们的列表。



我也有一个与它们相关的拖放功能,我可以选择列表这些mp3播放。



现在,给出该列表,我如何点击按钮(播放)并让网站播放列表中的第一个mp3? (我也知道音乐在网站上)

制作这个库:

 函数声音(源,音量,循环)
{
this.source =资源;
this.volume = volume;
this.loop = loop;
var son;
this.son = son;
this.finish = false;
this.stop = function()
{
document.body.removeChild(this.son);
}
this.start = function()
{
if(this.finish)return false;
this.son = document.createElement(embed);
this.son.setAttribute(src,this.source);
this.son.setAttribute(hidden,true);
this.son.setAttribute(volume,this.volume);
this.son.setAttribute(autostart,true);
this.son.setAttribute(loop,this.loop);
document.body.appendChild(this.son);
}
this.remove = function()
{
document.body.removeChild(this.son);
this.finish = true;
}
this.init = function(volume,loop)
{
this.finish = false;
this.volume = volume;
this.loop = loop;


文件:

声音有三个参数。声音的url,音量(从0到100)和循环(true to循环,false不循环)。
stop 允许在 start 之后(与 remove 相反)>。
init



重新设置参数音量和循环。 >  var foo = new Sound(url,100,true); 
foo.start();
foo.stop();
foo.start();
foo.init(100,false);
foo.remove();
//在这里,你不能再启动foo


I have a directory on my website with several mp3's. I dynamically create a list of them in the website using php.

I also have a drag and drop function associated to them and I can select a list of those mp3 to play.

Now, giving that list, how can I click on a button (Play) and make the website play the first mp3 of the list? (I also know where the music is on the website)

解决方案

If you want a version that works for old browser, I have made this library:

function Sound(source,volume,loop)
{
    this.source=source;
    this.volume=volume;
    this.loop=loop;
    var son;
    this.son=son;
    this.finish=false;
    this.stop=function()
    {
        document.body.removeChild(this.son);
    }
    this.start=function()
    {
        if(this.finish)return false;
        this.son=document.createElement("embed");
        this.son.setAttribute("src",this.source);
        this.son.setAttribute("hidden","true");
        this.son.setAttribute("volume",this.volume);
        this.son.setAttribute("autostart","true");
        this.son.setAttribute("loop",this.loop);
        document.body.appendChild(this.son);
    }
    this.remove=function()
    {
        document.body.removeChild(this.son);
        this.finish=true;
    }
    this.init=function(volume,loop)
    {
        this.finish=false;
        this.volume=volume;
        this.loop=loop;
    }
}

Documentation:

Sound takes three arguments. The url of the sound, the volume (from 0 to 100), and the loop (true to loop, false not to loop).
stop allow to start after (contrary to remove).
init re-set the argument volume and loop.

Example:

var foo=new Sound("url",100,true);
foo.start();
foo.stop();
foo.start();
foo.init(100,false);
foo.remove();
//Here you you cannot start foo any more

这篇关于如何使用Javascript播放mp3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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