How to play mp3 on Android using Phonegap Build

  • 1
  • Question
  • Updated 6 years ago
  • Answered
I want to know, how can I play mp3 audio file on Andoid using Phonegap Build?

I put my mp3 file in a folder, then using the tag to put it on my page but it is not playing.


<audio controls loop>
<source src="demo.mp3" type="audio/mpeg">
</audio>


Help!
Photo of ali ja

ali ja

  • 57 Posts
  • 0 Reply Likes

Posted 6 years ago

  • 1
Photo of ali ja

ali ja

  • 57 Posts
  • 0 Reply Likes
I have a good news, well not totally a best one. It is working.I missed the cordova.js file. The documentation does not mention it.

I managed to play it. Yes, it is playing now. However, pause and stop are not working. Here are my full codes:


<script>
var my_media = new Media(src, onSuccess, onError);

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

// Pause audio
function pauseAudio(url) {
var my_media = new Media(url,
function() {
console.log("pauseAudio():Audio Success");
},
function(err) {
console.log("pauseAudio():Audio Error: "+err);
}
);
my_media.pause();
}

// Stop audio
function stopAudio(url) {
var my_media = new Media(url,
function() {
console.log("stopAudio():Audio Success");
},
function(err) {
console.log("stopAudio():Audio Error: "+err);
}
);
my_media.stop();
}
</script>



<a onclick="playAudio('/android_asset/www/demo.mp3');">Lecture</a>
<a onclick="pauseAudio('/android_asset/www/demo.mp3');">Pause</a>
<a onclick="stopAudio('/android_asset/www/demo.mp3');">Stop</a>


Why the pause and stop are not working?
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Because you are instantiating a new object in every function, instead of using the current Media object's pause and stop methods.
Photo of ali ja

ali ja

  • 57 Posts
  • 0 Reply Likes
So, I have to chnage only for the pause and stop, right? In short, it will be like this?


function pauseAudio() {
myMedia.pause();
}

function stopAudio() {
myMedia.stop();
}
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
I'd suggest going through some javascript tutorials before trying to continue with PhoneGap.
Photo of ali ja

ali ja

  • 57 Posts
  • 0 Reply Likes
I am just following what is included on the documentations. I am not at fault if Phonegap's documentations are not well detailed. Documentations are made to follow and for beginners if I am not mistaken.

I have successfully tried to play this, I am getting issues only on this pause and stop things. I came here for help, and my question can also help many others.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Well, yes, I agree that the PGB documentation is not of prize winning quality. But even if it was, it would still require some basic knowledge of OO, HTML and JavaScript.
In this thread, you have shown that you can copy and paste JavaScript code, but have difficulties applying examples in your own code and understanding sample code.

The problem is, apparently, not PGB, but JavaScript in general. That's why John suggested that as a good starting point, before attempting to apply insufficient knowledge to imperfect documentation. I agree with him.
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
Have you figured out how to see the output from javascript console.log statements when you run this app on your device?
Photo of ali ja

ali ja

  • 57 Posts
  • 0 Reply Likes
Well, I have successfully made it play, pause and stop by following this tutorial:
http://docs.phonegap.com/en/3.3.0/cor...

However, I have modified it in my own way which is good now. Anyway, after spending hours on this, now it is working perfectly.

Thank you to each of you for assisting me.
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
Congratulations! Please post your code here to help out the next person that is trying to do the same thing.
Photo of ali ja

ali ja

  • 57 Posts
  • 0 Reply Likes
The codes are the same like on the documentation I posted above. Except, I changed my buttons, removed the pause and duration, that's all.

The only issue is, the audio starts playing by itself while the page loads. A bug in the documentation it seems!
Photo of ali ja

ali ja

  • 57 Posts
  • 0 Reply Likes
i just removed that device ready function where it is calling the playAudio, this is why the audio starts playing automatically.

Now, everything is fine and everything is working well.

Again, thank you to each of you!

This conversation is no longer open for comments or replies.