因此,我制作了一个phonegap build应用程序,其目标是读取包含有关音轨信息的xml文件,并且包含每个音轨的路径。 我继续将xml文件的数据输入到html,并在它工作正常之前。在将我的项目导入phonegap build后,不再有音频播放。控制工作,但音频不播放。Phonegap Build App - 播放音频
下面是代码:
<script>
document.addEventListener('play', function(e){
var audios = document.getElementsByTagName('audio');
for(var i = 0, len = audios.length; i < len;i++){
if(audios[i] != e.target){
audios[i].pause();
}
}
}, true);
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","teste.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table><tr><th>Track</th><th>Description</th<th>URL</th></tr>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++){
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("DESCRIPTION")[0].childNodes[0].nodeValue);
document.write("</td><td>");
var audio = x[i].getElementsByTagName("URL")[0].childNodes[0].nodeValue;
document.write('<audio controls><source src="'+audio +'" type="audio/mpeg"></audio>');
document.write("</td></tr>");
}
document.write("</table>");
</script>
XML文件结构简单是这样的:
<CD>
<TITLE>Track one</TITLE>
<DESCRIPTION>Bob Dylan</DESCRIPTION>
<URL>files/test.mp3</URL>
</CD>
我看过一些类似的问题,并发现大多数的错误是在音频路径文件。我应该为音频播放做什么? 感谢
编辑:我已经更新了我的代码:
var path = '/android_asset/www/';
var audio = x[i].getElementsByTagName("URL")[0].childNodes[0].nodeValue;
var audioPath = path+audio;
alert(audioPath);
document.write('<a href="#" onclick="playAudio('+audioPath+')">PLAY</a><br/>');
document.write('<a href="#" onclick="pauseAudio()">PAUSE</a><br/>');
document.write('<a href="#" onclick="stopAudio()">STOP</a>');
document.write('<p id="audio_position"></p>');
警报(audioPath)返回正确的路径和它的工作原理,当我使用此功能:
function onDeviceReady() {
playAudio('/android_asset/www/files/test1.mp3');
}
的audioPath是与上面的路径相同。 但是,当我按播放时,该应用程序不播放声音... 任何想法?
playAudio功能: 功能playAudio(SRC){// 从SRC创建媒体对象 my_media =新媒体(SRC,的onSuccess,onerror的);
// Play audio
my_media.play();
// Update my_media position every second
if (mediaTimer == null) {
mediaTimer = setInterval(function() {
// get my_media position
my_media.getCurrentPosition(
// success callback
function(position) {
if (position > -1) {
setAudioPosition((position) + " sec");
}
},
// error callback
function(e) {
console.log("Error getting pos=" + e);
setAudioPosition("Error: " + e);
}
);
}, 1000);
}
}
找到我的问题了!这是'onclick'。我现在导入jQuery并构建了以下代码:
$(".btPlay").on("click",function (e) {
// body...
console.log("Play");
playAudio(audioPath);
});
它现在可以工作!
你尝试使用相对路径(例如:./files/test.mp3)?您的Android设备上的音量是否会增加(微不足道,但可以帮助...)? – odupont
@odupont音量达到是。 (另外,音轨长度不会移动,因此不会有音频播放)。它看起来像是路径错误,但我不知道我应该使用什么路径。 ./files/test.mp3应该工作?索引文件与文件夹位于同一个文件夹中。 –