因此,我製作了一個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應該工作?索引文件與文件夾位於同一個文件夾中。 –