首页 文章

Phonegap Build App - 播放音频

提问于
浏览
0

因此,我制作了一个带有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>
'); document.write('<a href="#" onclick="pauseAudio()">PAUSE</a>
'); 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函数:function 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);
});

它现在有效!

1 回答

  • 1

    尝试安装cordova Media插件 org.apache.cordova.mediahttp://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/'

相关问题