Audio Loop - Android

1,332 views
Skip to first unread message

misterbassman

unread,
Feb 1, 2011, 8:31:10 AM2/1/11
to phonegap
Hi,

I'm trying play an audio file in a loop I have found the code
myvar.play({numberOfLoops:5});

However my audio file is only being played once. I have tried using a
wav file and an mp3 file.

My exact code is:

var sine1khz = new Media('/android_asset/www/audio/1khz.wav');
sine1khz.play({numberOfLoops:-1});

Simon MacDonald

unread,
Feb 1, 2011, 6:25:37 PM2/1/11
to phon...@googlegroups.com
Looping is not implemented on Android. IIRC it only works that way on
iPhone. We'll be looking at the Media API in 0.9.5.

You should open a ticket on lighthouse as an enhancement.

Simon

On Tuesday, February 1, 2011, misterbassman

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com
>

--
Simon Mac Donald
http://hi.im/simonmacdonald

neoPhillip

unread,
Feb 5, 2011, 12:15:16 PM2/5/11
to phonegap
I ran into the same issue, I used a work around that is not truly
looping but works ok.

Here is my soundOn Function.
Notice that the "loop" is in the new Media status section.
function soundsOn(url){

$('#off').css('display', 'none');
$('#on').css('display', 'block');
src = "/android_asset/www/audio/file.mp3";
if (url) {
src = url;
}
// Stop playing if src is different from currently playing source
if (src != audioSrc) {
if (media1 != null) {
stopAudio();
media1 = null;
}
}
if (media1 == null) {
media1 = new Media(src,
function() {
console.log("playAudio():Audio Success");
},
function(err) {
console.log("playAudio():Audio Error: "+err);
},
function(status) {
console.log("playAudio():Audio Status: "+status);
// Loop
if(status==4){
soundsOn(src);
}

});
media1.play();
}
audioSrc = src;


}

I hope that helps

-Phillip


On Feb 1, 8:31 am, misterbassman <dansulliva...@googlemail.com> wrote:
> Hi,
>
> I'm trying play anaudiofile in a loop I have found the code
> myvar.play({numberOfLoops:5});
>
> However myaudiofile is only being played once.  I have tried using a

virgil

unread,
Mar 5, 2012, 6:48:15 PM3/5/12
to phon...@googlegroups.com
Hi

The code of neoPhillip runs , but after 5 minutes of looping, it stops I think
the memory gets an overflow.

How can I free memory to avoid this ??

Thanks

Tushar Lal

unread,
Mar 6, 2012, 6:04:21 AM3/6/12
to phon...@googlegroups.com
Try using the audio tag with a loop attribute with the location of audio file in the "src". This will prevent the memory overflow problem.

Hope this helps.

Regards:
Tushar 

virk

unread,
Mar 6, 2012, 7:43:37 AM3/6/12
to phonegap
Hi

But if I want to control "Start" and "Stop" from independent buttons?

Michael Ma

unread,
Mar 13, 2012, 7:01:34 AM3/13/12
to phonegap
The audio tag doesn't work very well if I load the audio file
dynamically in JQM. it only play once and can't be played again.
I use a very ugly trick to remove the node and add it again in a
interval . I hope someone here has some other nicer solution.

Andrew Trice

unread,
Mar 13, 2012, 12:29:14 PM3/13/12
to phonegap
I created a native plugin for better control of audio playback...
PGLowLatencyAudio. It supports loopping on both Android and iOS if
you use the load. You may want to give it a try.

You can download it here:
iOS: https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/LowLatencyAudio
Android: https://github.com/phonegap/phonegap-plugins/tree/master/Android/LowLatencyAudio

Read more about it here:
http://www.tricedesigns.com/2012/01/25/low-latency-polyphonic-audio-in-phonegap/

Thanks,
Andrew Trice

Will

unread,
Oct 31, 2012, 4:34:47 AM10/31/12
to phon...@googlegroups.com
Hi again. Apparently reinserting the com folder fixed that problem. Although I'm only able to get sounds FX to play and not audio. I simply tweaked your drum machine example, taking out "FX" for "Audio" in the first "preload" line:

            PGLowLatencyAudio.preloadAudio('bass', 'sound/T1_music.mp3');

then I added my own sound file, about a 2 min clip. However when I hit the bass button there's no sound. There's no complaints by logcat. I'm using cordova 2.1.0 though. I don't know if that is an issue.

Any Ideas?

thanks

Will


On Monday, October 29, 2012 7:08:34 PM UTC-7, Will wrote:
Hey Andrew, this sounds like a great plugin! I'm pretty new to android/phonegap development so sorry if this is very newb question. I added the 3 java files to my project folder under com/phonegap/ and PGLowLatencyAudio.js to my www folder, next to my index.hmtl. Added the script line to my index.html and the line to my plugins in config.html, but for some reason I'm getting this error when I try to call a function:

Uncaught ReferenceError: PGLowLatencyAudio is not defined at file:///android_asset/www/index.html:76

Unless I misplaced something I'm not sure whats wrong. My project doesnt seem to have plugins.xml, but instead theres a "plugins" section in the congif.xml file. Could this be the problem?

thanks


Will
Reply all
Reply to author
Forward
0 new messages