cordova-plugin-media icon indicating copy to clipboard operation
cordova-plugin-media copied to clipboard

How can we loop ?

Open koraysels opened this issue 5 years ago • 4 comments

How to loop ? We can set numberOfLoops property in the call to .play() but it does nothing. I need perfect looping inside my app ... anybody have a solution?

koraysels avatar May 21 '19 10:05 koraysels

https://software.intel.com/es-es/forums/intel-xdk/topic/562553

aoreinoso avatar Jun 22 '19 08:06 aoreinoso

Couple of details for anyone trying to get looping to work:

On iOS It works great IF you use a local resource media file or download the file and reference it as a cdvfile:// before playing. It will not work for an internet url. That will cause this plugin to use AVPlayer which doesn't support looping out of the box (you can kind of rig it in javascript, but I wouldn't trust it's stability).

To get a local file to loop forever (until stop is called or the app is closed) set {numberOfLoops: -1} on play()

On Android numberOfLoops does nothing.

I tried many javascript ideas to get around this. You can definitely rig it, but you will get the occasional skip and it's not very reliable. It tried fading one out while fading in the next with setVolume(), this works but you can tell what's going on (not seamless) and I don't think it would be a solid solution.

I tried rewriting the native part of this plugin to reuse 2 MediaPlayers and specify the next via setNextMediaPlayer(). This worked, but chopped off the very end of the audio file before looping, not seamless.

After a lot of trial and error I would definitely suggest exoplayer2 for android gapless looping. I hesitated to add it, but it only ads 2mb to your apk and is very feature rich to use it again elsewhere. It's able to loop files without downloading them first and it can also read metadata in mp3s if it is set to get them to loop correctly (which is often a problem in with looping mp3s). Only downside, exoplayer doesn't work for iOS but this plugin, a local file, and play({numberOfLoops:-1}) does.

Creating audio files that don't have audible pops is another thing you'll have to consider.

Happy loopin'!

primerib avatar Oct 29 '19 06:10 primerib

//to listen to plugin stop events fileBg.onStatusUpdate.subscribe(status => { if (status == 4) { fileBg.play(); } });

xuyonghua avatar Oct 31 '19 10:10 xuyonghua

Couple of details for anyone trying to get looping to work:

On iOS It works great IF you use a local resource media file or download the file and reference it as a cdvfile:// before playing. It will not work for an internet url. That will cause this plugin to use AVPlayer which doesn't support looping out of the box (you can kind of rig it in javascript, but I wouldn't trust it's stability).

To get a local file to loop forever (until stop is called or the app is closed) set {numberOfLoops: -1} on play()

On Android numberOfLoops does nothing.

I tried many javascript ideas to get around this. You can definitely rig it, but you will get the occasional skip and it's not very reliable. It tried fading one out while fading in the next with setVolume(), this works but you can tell what's going on (not seamless) and I don't think it would be a solid solution.

I tried rewriting the native part of this plugin to reuse 2 MediaPlayers and specify the next via setNextMediaPlayer(). This worked, but chopped off the very end of the audio file before looping, not seamless.

After a lot of trial and error I would definitely suggest exoplayer2 for android gapless looping. I hesitated to add it, but it only ads 2mb to your apk and is very feature rich to use it again elsewhere. It's able to loop files without downloading them first and it can also read metadata in mp3s if it is set to get them to loop correctly (which is often a problem in with looping mp3s). Only downside, exoplayer doesn't work for iOS but this plugin, a local file, and play({numberOfLoops:-1}) does.

Creating audio files that don't have audible pops is another thing you'll have to consider.

Happy loopin'!

I have been using the Audio WEB API untill now.. It has bugs when going into the background etc.. It has been very performant to loop and also for polyphonic audio. But I need to get rid of the bugs it us causing. SO i switched to Capacitor instead of cordova and now I need to find a way to reference the assets directly but I can't seem to get the correct path..

[UPDATE] Okay I figured out how to load the files .. but man... the looping is terrible... gaps all over the place and no synchronization.. Also when I try to play multiple sounds at once.. It is a disaster ... (I am making a music sample app with a sequencer) -> when I use web audio API it works flawllesly.. only the sond is gone there when the app is sent to background.. which is a totally different problem but still ..

koraysels avatar Jan 14 '20 09:01 koraysels