android - Phonegap Build App-播放音频

标签 android cordova audio

因此,我制作了一个带有phonegap构建的应用程序,其目标是读取一个xml文件,该文件包含有关音轨的信息,并包括每个音轨的路径。
我继续将xml文件的数据输入到html中,然后才能正常工作。将项目导入phonegap构建后,不再播放音频。控件可以正常工作,但是音频无法播放。

这是代码:

<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>

我读过一些类似的问题,发现大多数错误都在音频文件的PATH中。我应该怎么做才能播放音频?
谢谢

编辑:我更新了我的代码:
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>');

alert(audioPath)返回正确的路径,当我使用此功能时它可以工作:
function onDeviceReady() {
        playAudio('/android_asset/www/files/test1.mp3');
    }

audioPath与上面的路径相同。
但是当我按Play时,该应用程序不会播放声音...
有任何想法吗?

playAudio功能:
函数playAudio(src){
//从src创建Media对象
my_media = new 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);
});

现在可以使用了!

最佳答案

尝试安装cordova Media插件org.apache.cordova.media(http://docs.phonegap.com/en/edge/cordova_media_media.md.html)

然后尝试:

//src is your path to your file
var my_media = new Media(src, function(){
    alert("Success");
}, function(){
    alert("Fail");
});

my_media.play();

尝试使用yuo已经尝试过的所有路径,但是此插件对我适用于在线文件。

编辑:您可能需要在src前面添加'/android_asset/www/'

关于android - Phonegap Build App-播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30731020/

相关文章:

java - 编码标签中的 Sax 解析

javascript - 使用 Dropbox API V2 + Cordova 将文件上传到 Dropbox

javascript - 即时更改音量

python - 从树莓派上的 ADC 芯片读取原始音频值

android - fragment Activity : proper fragment instantiation

android - 有没有办法从传入的修饰符中检索值?

javascript - Android/Phonegap - onClick() 不工作

audio - FFMPEG Encode 编码成 .3g2 最好的质量?

java - Android webservice访问错误

node.js - Microsoft Visual Studio Cordova 工具无法构建应用程序