HTML5 audio not playing in PhoneGap App (Possible to use Media?)

前端 未结 7 1045
执笔经年
执笔经年 2020-12-06 14:05

Working on a basic sound board for a trip abroad. Set up everything to work fine in Browsers but wanted to use PhoneGap Build/GitHub to make it downloadable as cell service

相关标签:
7条回答
  • 2020-12-06 14:45

    I think you should say "Works great in desktop browser", it is more a matter of support of media in the android default browser than an issue with phonegap.

    According to this page, the android browser supports mp3 on some device, and ogg on others. Maybe your issue is that you're trying to use mp3 on a device only supporting ogg for media in the browser?

    Using phonegap media plugin should solve the problem.

    The full example in the documentation page is quite clear. You just have to copy in your page the playAudio, onSuccess and onError functions and then call playAudio whenever you want to play a file.

    The html of your page could look like this :

    <button class="button" id="btnYes">Yes</button>
    <button class="button" id="btnNo">No</button> 
    

    and the javasctipt (using jquery):

    document.addEventListener("deviceready", function () {
        $("#btnYes").on("click", function () {
            playAudio(audio/basic/yes.mp3);
        });
        $("#btnNo").on("click", function () {
            playAudio(audio/basic/no.mp3);
        });
    }, false);
    var my_media = null;
    var mediaTimer = null;
    function playAudio(src) {
        // Create Media object from src
        my_media = new Media(src, onSuccess, onError);
        // Play audio
        my_media.play();
    }
    // onSuccess Callback
    function onSuccess() {
        console.log("playAudio():Audio Success");
    }
    // onError Callback
    function onError(error) {
        alert('code: ' + error.code + '\n' +
            'message: ' + error.message + '\n');
    }
    

    The calls to playAudio could also be in the onclick directly in the html, but I prefered to use jquery to start listening for clicks only once phonegap is ready.

    Don't forget to include cordova.js, and if you're using phonegap 3.x don't forget to install the org.apache.cordova.media plugin

    Edit:

    path audio/basic/yes.mp3 assumes that the file is in the audio folder at the root of the sd card.

    use /android_asset/www/audio/basic/yes.mp3 if the audio folder is located in the phonegap www folder.

    0 讨论(0)
提交回复
热议问题