Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop a audio in phonegap?

I'm using phonegap 2.2.0. I tried some code to loop the audio for playing without interruption but it is not working on my android device. It plays only once. Here is my code:

 


    function playAudio(url) {

        var my_media = new Media(url,
            function() {
                console.log("playAudio():Audio Success");
            },
            function(err) {
                console.log("playAudio():Audio Error: "+err);
            },
            function(status) {
                console.log("playAudio():Audio Status: "+status);
            }
        });

        // Play audio
        my_media.play({numberOfLoops:99});
    }


Can you tell me what is wrong here?

like image 276
Arun Avatar asked Dec 18 '12 06:12

Arun


1 Answers

Listen to status change and start paying on MEDIA_STOPPED. If you want the user to stop the sound, use snd.pause() and not snd.stop()

This worked for me:

    document.addEventListener("deviceready", onDeviceReady, false);

    var getPhoneGapPath = function() {
        var path = window.location.pathname;
        path = path.substr( path, path.length - 10 );
        return path;
    };

    var snd = null;

    function onDeviceReady(){
        snd = new Media( getPhoneGapPath() + "sound/funk_game_loop.mp3", onSuccess, onError, onStatus);
        snd.play();
    }

    // onSuccess Callback
    function onSuccess() {
    }
    // onError Callback 
    function onError(error) {
    }
    // onStatus Callback 
    function onStatus(status) {
        if( status==Media.MEDIA_STOPPED ) {
            snd.play();
        }
    }
like image 58
Sidney Veiga Avatar answered Sep 24 '22 11:09

Sidney Veiga