1
votes

I am having a small issue on PhoneGap, using the Media plugin to play audio. First of all, I will describe the page and I will include my codes below.

I have multiple mp3 files. Each mp3 file will be played once their respective "Play" button is clicked. After implementing the codes, the mp3 files are playing correctly. However, I am having some issues and they are as follows:

  • When I click on the "Play" button of an mp3 file, it does play. But when I click again to play it, it does not play. The mp3 files are only in some seconds. I want that once they complete, I can click again to play.
  • If I play an mp3 file, the others do not play.

My codes are as follows:

JavaScript

 <script>
        // Wait for device API libraries to load
        document.addEventListener("deviceready", onDeviceReady, false);
        function onDeviceReady() {
        }

        // Audio player
        var mymedia = null;
        var audio = false;

        //Pause event
        document.addEventListener("pause", onPause, false);
        function onPause() {
            mymedia.stop();
            audio = false;
        }

        // Play audio
        function playAudio(src) {
            if (audio === false) {
            mymedia = new Media(src, onSuccess, onError);
            mymedia.play();
            audio = true;
        } 
        }

        // onSuccess Callback
        function onSuccess() {
            console.log("playAudio():Audio Success");
        }

        // onError Callback
        function onError(error) {

        }
      </script>

HTML

<a href="#" class="p" onclick="playAudio('/android_asset/www/one.mp3')"><img src="play.png" alt="play"/></a>

<a href="#" class="p" onclick="playAudio('/android_asset/www/two.mp3')"><img src="play.png" alt="play"/></a>

NOTE: I do not have Pause and Stop buttons.

Again, the audio is playing but if I click again on the "Play" button, it does not play and if I click on another "Play" button of the other audio files, they do not play.

I have little experience with this, so forgive me! Thank you!

1

1 Answers

0
votes

Set audio to false in onSuccess function