gpt4 book ai didi

javascript - 使用 HTML5 和 javascript 播放连续的声音以构建音频句子

转载 作者:行者123 更新时间:2023-11-28 01:46:04 28 4
gpt4 key购买 nike

我正在尝试为phonegap 应用程序制作音频 map 。我想构造简单的句子来创建方向。

为了缩小范围,我有一组音频元素,我想连续播放它们以执行音频导航。例如:

// playList[0].play --> "You are coming from the"
// playList[1].play --> "Reptile House"
// playList[2].play --> "(half-second pause as sentence period...)"
// playlist[3].play --> "Please select a destination from the list on the left."

这篇文章的底部是我一直在使用的简化代码,但没有取得太大成功。我不断收到错误。在建议使用 jplayer 或其他系统之前,在我看来,这些系统更适合播放歌曲并具有停止它们的能力。我不需要一个成熟的 jplayer 或其他风格的界面。我只需要人们按下按钮,这样他们就可以通过音频 map 系统进行引导。我不断在控制台中收到此错误:

Uncaught TypeError: Object #selectionFrom has no method 'play' audio-map.js:142
(anonymous function) audio-map.js:142
jQuery.event.dispatch jquery-1.9.1.js:3074
jQuery.event.add.elemData.handle jquery-1.9.1.js:2750

我尝试过将数组作为元素 ID 和变量。

var playList= ["#selectionFrom","#playReptileHouse","#play500msPause","#selectDestination"];
var playList= ["selectionFrom","playReptileHouse","play500msPause","selectDestination"];

如果您有任何帮助,我将不胜感激。这是简化的脚本:

$(document).ready(function() {

var playZooEntrance = document.createElement('audio');
playZooEntrance.setAttribute('src', 'assets/audio/playZooEntrance.mp3');
playZooEntrance.setAttribute('id', 'playZooEntrance');
document.body.appendChild(playZooEntrance);

var playReptileHouse = document.createElement('audio');
playReptileHouse.setAttribute('src', 'assets/audio/playReptileHouse.mp3');
playReptileHouse.setAttribute('id', 'playReptileHouse');
document.body.appendChild(playReptileHouse);

var selectionFrom = document.createElement('audio');
selectionFrom.setAttribute('src', 'assets/audio/selectionFrom.mp3');
selectionFrom.setAttribute('id', 'selectionFrom');
document.body.appendChild(selectionFrom);

var selectDestination = document.createElement('audio');
selectDestination.setAttribute('src', 'assets/audio/selectDestination.mp3');
selectDestination.setAttribute('id', 'selectDestination');
document.body.appendChild(selectDestination);

var play500msPause = document.createElement('audio');
play500msPause.setAttribute('src', 'assets/audio/play500msPause.mp3');
play500msPause.setAttribute('id', 'play500msPause');
document.body.appendChild(play500msPause);

$('#selected_from').click(function() { // On the press of a button...
var playList= ["selectionFrom","playReptileHouse","play500msPause","selectDestination"];
var i;
for (i = 0; i < playList.length; ++i) {
console.log(playList[i]);
playList[i].play();
playList[i].setAttribute('ended',function(){/* next i??? */});
}
});
// Play this consecutively...
// playList[0].play --> "You're coming from the"
// playList[1].play --> "Reptile House"
// playList[2].play --> "(half-second pause)"
// playlist[3].play --> "Please select a destination from the list on the left."
});

最佳答案

您似乎犯了一个类型错误:当您想要获取具有该 id 的元素时,您要求播放一个字符串('SelectionFrom')。
因此,您可以在创建元素时将元素预先存储在数组或对象中,或者使用 jquery 通过 id 查找它们 ($('#'+playList[i])。

关于javascript - 使用 HTML5 和 javascript 播放连续的声音以构建音频句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20302897/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com