0

Audio doesn't play in my Hybrid app.
It works in debug mode, but when built and installed, the apk file doesn't work anymore.

Here is the full JavaScript code to play the audio.
Can anyone help me?

var media = null;
var mediaTimer = null;

    function getPath() {
        var str = location.pathname;
        var i = str.lastIndexOf('/');
        return str.substring(0,i+1);
    }

    function playAudio() {
        var src = 'images/ring.mp3';

        media = new Media (getPath() + src , onSuccess, onError);

        //Play the audio. You can set number of the replaying time here.
        media.play({numberOfLoops:"0"});

        if (mediaTimer == null) {

            mediaTimer = setInterval(function() {

                // Return a current playback position
                media.getCurrentPosition(

                    //A Callback function if it's success
                    function(position) {
                        if (position > -1) {
                            setAudioPosition((position) + " sec");

                                    //If the playback stops at "-0.001" position, replay the audio.
                                    if(position == -0.001){

                                        media.play({numberOfLoops:"infinite"});

                                    }
                        }
                    },
                    //A callback function in case of failure
                    function(e) {

                        console.log("Error getting pos=" + e);
                        setAudioPosition("Error: " + e);

                    }
                );
            }, 1000);
        }
    }

    function pauseAudio() {

        if (media) {
            media.pause();
        }
    }

    function stopAudio() {

        if (media) {
            media.stop();
        }

        clearInterval(mediaTimer);
        mediaTimer = null;
    }

      //A success callback function displaying a success message.
      function onSuccess() {
        stopAudio();

      }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

This is a known issue with Phonegap. It has been solved multiple times with some of the best info posted here: HTML5 audio not playing in PhoneGap App (Possible to use Media?)

Community
  • 1
  • 1
Munsterlander
  • 1,356
  • 1
  • 16
  • 29