Unable to play local audio in PhoneGap Build Project - HELP

  • 4
  • Problem
  • Updated 5 years ago
  • In Progress
I've been trying to solve this for two days without luck.

I have quite a simply app that uses the HTML5 audio tag to play some sound. It works on the web and on the iPhone but the sounds won't play in my Android.

My original mark-up for it was:

But I see from other posts that I have to provide a more explicit address for the sound.

My project is held in a github repository and the structure is basically:

index.html
en/ - english assets
de/ - german assets

I'm building with PhoneGap Build.

I've tried referring to the sounds as:

/andoid_asset/en/phone1.mp3
/andoid_asset/www/en/phone1.mp3 - Do you still have a "www" in PhoneGap Cloud?
/en/phone1.mp3
/www/en/phone1.mp3
./phone1.mp3

But I just can't get the sound to play.

If I put the sound on the web it works:

http://www.metro-boulot-dodo.com/stat...

I'm up against a deadline here and it's driving me mad!

Any help about what the filename of the audio file should be?

Sean
Photo of Sean Clark

Sean Clark

  • 2 Posts
  • 0 Reply Likes

Posted 8 years ago

  • 4
Photo of Sean Clark

Sean Clark

  • 2 Posts
  • 0 Reply Likes
OK. I'm still working on this. I see the filename is definitely:

file:///android_asset/www/en/phone1.mp3

However, even when I just create a simple link to it it won't play. I don't get an error, just no sound. The same files plays perfectly in the browser and via an external web link.

Is there some sort of privacy setting that needs to be added to allow me to play audio files within PhoneGap? Is it a bug?

Sean
Photo of dwadawdwdwfwa

dwadawdwdwfwa

  • 4 Posts
  • 0 Reply Likes
Do you now how what the problem is?? I have the same like you! Thanks
Photo of Erny

Erny

  • 22 Posts
  • 5 Reply Likes
try: /android_asset/www/en/phone1.mp3
Photo of Andrew Lunny

Andrew Lunny

  • 1911 Posts
  • 199 Reply Likes
You should be able to use the Media api to play audio.

Here is a simple example I got working using the Media API, that plays audio on both iOS and Android:
https://gist.github.com/2380994
Photo of Victor Unda

Victor Unda

  • 1 Post
  • 0 Reply Likes
Andrew, thank you for the code. Did you try it using phonegap for IOS?
Thanks,
Photo of dwadawdwdwfwa

dwadawdwdwfwa

  • 4 Posts
  • 0 Reply Likes
Does this also functions with Phonegap Build? Or are there difrences between normal Phonegap and the Build version? Thanks for help!
Photo of Jay Mandevia

Jay Mandevia

  • 1 Post
  • 0 Reply Likes
Thanks you really helped me with a project...
Photo of Aiad

Aiad

  • 46 Posts
  • 2 Reply Likes
Hi, i inserted the code as described and changed ('brobob.mp3') to mine but could not play it, any help, it is very important to me to get sound within my apps.
Photo of Sanmoy Ray

Sanmoy Ray

  • 1 Post
  • 0 Reply Likes
not working for me too .. looks like a hoax.
Photo of Lorenzo De Tomasi

Lorenzo De Tomasi

  • 15 Posts
  • 0 Reply Likes
After long tests on Android 4.04 on Samsung GT-P7500, I have disabled Hydration from Phonegap Build Settings, uninstalled the previous version of the app and installed the new version from the QR code and audio works with both /android_asset/www/... and file:///android_asset/www/...
If I enable hydration again I can't hear audio.
I hope Phonegap Build developers will repair this bug soon! Thank you.
Photo of Dante Miranda

Dante Miranda

  • 1 Post
  • 0 Reply Likes
Lorenzo, Where Do you find the Phonegap Buid Settings? I can't find it... Do you have the file that you disabled Hydration? Can you post it?
Photo of Lorenzo De Tomasi

Lorenzo De Tomasi

  • 15 Posts
  • 0 Reply Likes
Hydration can be disabled from Phonegap Build Website: under settings tab, uncheck hydration.
Photo of Thierry Mattray

Thierry Mattray

  • 16 Posts
  • 0 Reply Likes
Finally i made it work, after several weeks of failure !

Andrew's exemple works for Phonegap 2.9.0, but not for phonegap 3.0.0

the config.xml file must have this lines :

< preference name="phonegap-version" value="2.9.0"/>
< feature name="http://api.phonegap.com/1.0/device"/>
< feature name="http://api.phonegap.com/1.0/media" />

Hope this will help...
Photo of Lorenzo De Tomasi

Lorenzo De Tomasi

  • 15 Posts
  • 0 Reply Likes
Can you paste or, better, link a copy of your config.xml?
Thank you
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi Lorenzo,

Ensure you have the media plugin added to your config.xml for PGB 3.0.

The above would work for 2.9 but wouldn't help with 3.0.

Below is the line for adding Media Plugin to your app:
<gap:plugin name="org.apache.cordova.media" />
Add to your config.xml.

Ismael
Photo of Thierry Mattray

Thierry Mattray

  • 16 Posts
  • 0 Reply Likes
Thanks Ismael,

this page : http://docs.phonegap.com/en/edge/guide_phonegap-build_index.md.html is outdated !

It says <gap:plugin name="Media" value="org.apache.cordova.core.AudioHandler" />

instead of
<gap:plugin name="org.apache.cordova.media" />

So it give you an error "no supported plugin" ...

The same for Device; the good code is :
<gap:plugin name="org.apache.cordova.core.Device"/>


It made me loose many time ... :(

So for Lorenzo,

Andrew's exemple works for me with phonegap 3.0.0 too, with this config.xml file :

 


< widget id="yourdomain.mp3" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
< name>Testmp3< /name>
< description>
TestSound application .
< /description>
< author email="support@phonegap.com" href="http://phonegap.com">
you
< /author>

< preference name="phonegap-version" value="3.0.0" />
< preference name="target-device" value="universal" />
< preference name="fullscreen" value="true" />
< preference name="webviewbounce" value="true" />
< preference name="stay-in-webview" value="false" />
< preference name="ios-statusbarstyle" value="black-opaque" />
< preference name="detect-data-types" value="true" />
< preference name="exit-on-suspend" value="false" />
< preference name="android-minSdkVersion" value="7" />
< preference name="android-installLocation" value="auto" />


<gap:plugin name="org.apache.cordova.media" />
<gap:plugin name="org.apache.cordova.core.Device"/>
< access origin="http://127.0.0.1*" />
< /widget>


(remove the space after each " < ", i put them in order to display the code in this post, otherwise it doesn't work.)
Photo of Mike

Mike

  • 2 Posts
  • 0 Reply Likes
I would really expect the html5 audio tags to work for this situation, but they don't.
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi Mike,

You can use the media plugin to play Audio.

It is a safer choice as it ensures Audio can be played irrespective of device.

Ismael
Photo of Kostas

Kostas

  • 6 Posts
  • 0 Reply Likes
Andrews code doesnt work.
media plugin doesnt play audio.
phonegap build make briliand html5 games native apps , but with no sound......
Photo of Karthik Lmkt

Karthik Lmkt

  • 4 Posts
  • 0 Reply Likes
i am adding audio to my android using phonegap, it works perfectly,

but if i am working continously certain point audio stopped completely.

once close and open a file its works

i dont kwn the pblm please help me
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi Karthik,

Please provide us with your app id if you are building this project with PhoneGap Build.

If you are using PhoneGap(locally) however, recommend asking about the issue in one of the links below.

PhoneGap Google Group: https://groups.google.com/forum/?fromgroups#!forum/phonegap

Cordova @ Stackoverflow: http://stackoverflow.com/questions/tagged/cordova

IRC Channel: Server: irc.freenode.net | Channel: #phonegap

Dedicated PhoneGap Paid Support: http://phonegap.com/support/

Developers Directory: http://people.phonegap.com/

Thanks.
Photo of Karthik Lmkt

Karthik Lmkt

  • 4 Posts
  • 0 Reply Likes
thanks for ur reply,,

if there is any time limit for audio playing,

how to rectify this issue
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
There isn't a time limit however note that when a song gets to the end it will stop.

You will need to loop through to repeat.

That is if the song get's to the end, you can redirect to beginning by running play command again or using the seekTo to return the position back to the beginning.
Photo of Karthik Lmkt

Karthik Lmkt

  • 4 Posts
  • 0 Reply Likes
each button have a separete audio,switch from one button to another first few minutes working properly,after that audio stopped.

if there is any coding pblm r config pblm

i am using phonegap 1.5
Photo of ismael jimoh

ismael jimoh

  • 4116 Posts
  • 192 Reply Likes
Hi Karthik,

I don't quite understand what you mean?

Also to my earlier question, are you building with PGB(cloud service) or locally?

In regards the button, you can tell the buttons to halt previous song before it starts playing.

The play should always activate an audio to be played.

Do clarify  what your question is thanks.
Photo of Karthik Lmkt

Karthik Lmkt

  • 4 Posts
  • 0 Reply Likes
i have audio in assets file,i put the path in json,i called audio from json,totally i have 5 audios,i click next button audio change and plays very wel

the pblm is working only few mins(audio stopped totally)

after that i close and reopen the application audio working properly for few mins
Photo of Erny

Erny

  • 22 Posts
  • 5 Reply Likes
***ANDROID ONLY FIX FOR YOUR LOCAL AUDIO FILES

Android can only hold on to a couple of audio files at a time. So the file must be 'released'. Also you have to give the specific path. If your audio is in the same folder as your index.html file than it would be "/android_asset/www/yoursound.mp3' To make it work in ipnone/wp8 you would have to have the proper address for the file(i guess)..I will be testing next week on WP8 Iphone so I will update this.

Here is a code for local play:
function playAudio(url) {
// Play the audio file
var my_media = new Media("/android_asset/www/');"url,
// success callback
function () {
console.log("playAudio():Audio Success");
///realease it HERE
my_media.release();
},
// error callback
function (err) {
console.log("playAudio():Audio Error: " + err);
}
);
// Play audio
my_media.play();
}

And make sure to have these in the config file
Photo of Sai Swetha A

Sai Swetha A

  • 1 Post
  • 0 Reply Likes
hey hi,
Like there is a folder /android_asset/www/ on android, is there any folder specific to iphone?
Photo of fu

fu

  • 1 Post
  • 0 Reply Likes
"Try this. It's Work"
1. Install:
cordova plugin add org.apache.cordova.media
cordova plugin add org.apache.cordova.file

2. Code:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>PhoneGap Back Button Example</title>

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

var myMedia = null;
var playing = false;

function playAudio() {
if (!playing) {
myMedia.play();
document.getElementById('play').src = "images/pause.png";
playing = true;
} else {
myMedia.pause();
document.getElementById('play').src = "images/play.png";
playing = false;
}
}
function stopAudio() {
myMedia.stop();
playing = false;
document.getElementById('play').src = "images/play.png";
document.getElementById('audio_position').innerHTML = "0.000 sec";
}
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady(){
console.log("Got device ready");
updateMedia();
}

function updateMedia(src) {
// Clean up old file
if (myMedia != null) {
myMedia.release();
}

// Get the new media file
var yourSelect = document.getElementById('playlist');
myMedia = new Media(yourSelect.options[yourSelect.selectedIndex].value, stopAudio, null);
// Update media position every second
var mediaTimer = setInterval(function() {
// get media position
myMedia.getCurrentPosition(
// success callback
function(position) {
if (position > -1) {
document.getElementById('audio_position').innerHTML = (position/1000) + " sec";
}
},
// error callback
function(e) {
console.log("Error getting pos=" + e);
}
);
}, 1000);
}
function setAudioPosition(position) {
document.getElementById('audio_position').innerHTML =position;
}
</script>
<body onload="onLoad()">
<h1>Audio Player</h1>
<p id="audio_position">0.000 sec</p>
<p>
<select id="playlist" onchange="updateMedia()">
<option checked value="/android_asset/www/sounds/monster.mp3">Asset</option>
<option value="sounds/button-1.mp3">SD Card</option>
<option value="sounds/monster.mp3">Internet</option>
</select>
</p>
<a href="#" onclick="playAudio()">PLAY<img id="play" src="images/play.png"></a>
<a href="#" onclick="stopAudio()">STOP<img id="stop" src="images/stop.png"></a>
</body>
</html>

Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
@Fu,
your example is incorrect. This forum is for Phonegap Build, NOT Phonegap CLI. Please do not post CLI examples here.

Please delete you response. AND please do not give answers to four (4) year old questions.

TIA
Jesse
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Please delete you response.
By now, Jesse, you should know that he can't do that anymore, because you posted a reply.