动态源HTML5音频元素 [英] HTML5 audio element with dynamic source

查看:186
本文介绍了动态源HTML5音频元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的音频播放器:

I have a basic audio player:

<audio controls="controls" autoplay="true" loop="loop">
<source src="song.php" type="audio/mpeg" />
</audio>

而不是直接指向MP3文件,我把它指向一个PHP文件,然后指向MP3文件,它的工作来源和。但是,在当前的轨道是在PHP文件指向一个新的MP3文件。所以,我的问题是,当玩家进入循环,用新的MP3文件,将其完全停止工作。有没有解决这个办法吗?有没有办法用一个播放列表,或任何其他球员做到这一点?

And instead of the source pointing directly to the MP3 file I have it pointed at a PHP file which then points to the MP3 file, which works. But, when the current track is over the PHP file is pointing to a new MP3 file. So, the problem that I have is that when the player goes to loop, with the new MP3 file, it completely stops working. Is there any way around this? Is there a way to do this with a playlist or any other players?

推荐答案

重复的问题

对于初学者来说,要连接一个新的事件处理程序每​​一次被点击的元素。如果有人经常会暂停音乐,他们将遇到问题。

For starters, you are attaching a new event handler every single time the element is clicked. If someone frequently pauses the music, they will run into problems.

这一翻译,试试这个:

<audio id="audio" autoplay controls src="song.php" type="audio/mpeg"></audio>
<script type="text/javascript">
    document.getElementById('audio').addEventListener("ended",function() {
        this.src = "song.php?nocache="+new Date().getTime();
        this.play();
    });
</script>

我假设 song.php 是一个PHP文件,返回音频数据。在非缓存查询参数将确保该文件实际上是所谓的每一次。

I'm assuming that song.php is a PHP file that returns the audio data. The nocache query parameter will ensure that the file is actually called every time.

这篇关于动态源HTML5音频元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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