启动HTML5<音频>在随机位置 [英] Start HTML5 <Audio> at random position

查看:178
本文介绍了启动HTML5<音频>在随机位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个音轨,其长度约2小时,我想在我的网站上使用。我希望它开始跟踪在页面加载时的随机位置播放。这可能使用HTML5?我知道你可以使用element.currentTime()函数来获取当前位置,但你会怎么得到它的完全下载前轨道的总时间?我试过.load的每个组合。preLOAD .autobuffer,数百人。

I have an audio track which is around 2 hours long that I want to use on my website. I want it to start the track playing at a random position when the page loads. Is this possible using HTML5? I know you can use the element.currentTime() function to get the current position but how would you get the total time of the track before it's fully downloaded? I've tried every combination of .load .preload .autobuffer and hundreds more.

<script>

var stream_url_mp3 = "";
var stream_url_ogg = "";
var stream_total_time = 100;
var stream_start_time = Math.floor(Math.random() * stream_total_time);
var daJukebox = document.createElement('audio');

if (daJukebox.canPlayType) {

    if ("" != daJukebox.canPlayType('audio/mpeg')) {
        daJukebox.src = stream_url_mp3;
    } else if ("" != myAudio.canPlayType('audio/ogg; codecs="vorbis"')) {
        daJukebox.src = stream_url_ogg;
    }

    daJukebox.preload = true;
    daJukebox.autobuffer = true;
    daJukebox.pause();
    daJukebox.currentTime = stream_start_time;
    daJukebox.play();

}

感谢:)

推荐答案

如果没有办法获得来自HTML5音频元素的信息,您可以决定在服务器上的持续时间和传递信息作为数据属性的HTML 。例如:

If there's no way to obtain the information from the HTML5 audio element, you could determine the duration at the server and pass the information as a data attribute to the HTML. For example:

&LT;音频ID =一些-ID数据持续时间=7200&GT;

其中, 7200 是以秒为单位的时间(或时间其他单位)。然后,您可以在JavaScript中使用该属性的值来决定从哪里开始的音频文件。

where 7200 is the time in seconds (or some other unit of time). You can then use the value of this attribute in JavaScript to decide where to start the audio file.

这篇关于启动HTML5&LT;音频&GT;在随机位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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