如何顺序播放许多音频文件 [英] How to play many audio files in sequence

查看:563
本文介绍了如何顺序播放许多音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个长篇故事,其源音频是逐句音频文件.我想创建一个网页,使人们可以多次收听特定故事的各个句子,或者从头到尾收听整个故事.

I have several long stories which for which the source audio is sentence-by-sentence audio files. I would like to create a web page where one could listen to individual sentences of a particular story many times, or listen to the whole story from start to finish.

首先,我的网页上有许多<audio>元素,每个元素都在<p>中,并带有相应的文本.每个<audio>元素都对应一个故事的句子.

To start, my web page has many <audio> elements, each of which is in a <p> with the corresponding text. Each of these <audio> elements corresponds to a single sentence of the story.

我将要开始编写一个JavaScript对象,该对象将允许某种全部播放"功能,单击该按钮,它将播放音频[0],完成后将播放音频[1]等等.它还会有一个暂停"按钮,并跟踪您的所在位置等.这样,由于每个句子都有一个audio元素,因此它们仍然可以播放或欣赏.或者,可以使用顶部的全部播放"按钮将audio元素序列视为一个大元素.

I was about to start coding up a javascript object which was going to allow some sort of "play all" functionality, you'd click the button and it would play audio[0], when that finished, audio[1], etc. It would also have a 'pause' button and keep track of where you were, etc. This way the individual sentences could still be played or enjoyed because they each have an audio element. Or, one could use my "play all" buttons at the top to treat the sequence of audio elements as if they were one big element.

然后我开始问自己,这里是否有更好/更容易/更规范的解决方案.我想到的一件事是将所有这些单独的音频文件组合在一起,成为一个大音频文件,并可能创建章节" <track>元素.这样比较好吗?

Then I started asking myself if there's some better/easier/more canonical solution here. One thing I thought of doing would be to cat all of these individual audio files together into a big audio file and perhaps create 'chapter' <track> elements. Is this preferable?

每种方法的优缺点是什么?是否已经为我准备了一些开箱即用的解决方案,而我只是没有出现?

What are the pros and cons to each approach? Is there some out-of-the-box solution already made for me which I simply haven't turned up?

推荐答案

您可以使用end事件在上一个声音完成后播放下一个声音(

You could use the ended event to play the next sound when the previous sound completes (Playing HTML 5 Audio sequentially via Javascript):

var sounds = new Array(new Audio("1.mp3"), new Audio("2.mp3"));
var i = -1;
playSnd();

function playSnd() {
    i++;
    if (i == sounds.length) return;
    sounds[i].addEventListener('ended', playSnd);
    sounds[i].play();
}

这篇关于如何顺序播放许多音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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