arielfaur / ionic-audio

An audio player for Ionic 3 and Angular 4. Works with HTML 5 audio or native audio using Cordova Media plugin.
http://arielfaur.github.io/ionic-audio/2.0/index.html
MIT License
321 stars 163 forks source link

Is there a way to implement autoplay??? #189

Open outoftheboxbz opened 6 years ago

outoftheboxbz commented 6 years ago

The plugin works well for me. I also have it working along with ionic music controls and background mode. Just wanted to know if there is a way to allow my audio to autoplay

rdai commented 6 years ago

Maybe you can put some code in onTrackFinished function to look for the next item in this._audioProvider.tracks array?

codinronan commented 6 years ago

You can't do this via ionic audio, period. This is not an issue with this fantastic library; rather the problem is with the devices. Even if you implement something like the above you will run into the following:

  1. On Android, without a WAKE_LOCK, your device will go to sleep mid-song and you can't wake it up programmatically. You can solve that one simply by adding the WAKE_LOCK permission to your app. Even still, you will run into issues such as no control over the buffering behavior, stuttering on bad connections, etc. On most older Android devices, HTML5 audio and the native audio track both wait until most of the song is downloaded to begin playback. On Android API < 23, the CoreAudio implementation actually waits for the entire song, which is a terrible user experience.

  2. iOS is more insidious. Whether you use the WebAudioTrack or CordovaAudioTrack, iOS intentionally cripples both of them to not be able to continue playing once a song ends. Period. It maintains something called an audio session, and the session ends once a song completes; even if you attempt to keep the session alive, the OS knows that you tried to start a song without user interaction and prevents playback. You cannot fix this via ionic-audio nor cordova-plugin-media.

Because of this I have created a new cordova plugin, called cordova-plugin-playlist that resolves both of the above issues. On Android it uses Google's own ExoPlayer, which has far superior playback and buffering capabilities, and on iOS it uses the AVQueuePlayer, which is specially designed to allow continuous playback of discontinuous audio sources.

In the UI I still use ionic-audio as the interface to controls, I just shim the commands back into my plugin. I suppose I could actually make the tracks directly compatible so you can pass them into this library's controls.

I won't link to it here out of respect for arielfaur, I have used his work for almost 2 years and love it. If you're interested it is on npm.

arielfaur commented 6 years ago

@codinronan Thanks for the kind words and please go ahead and post the link to the plugin. It is great that you found a way to overcome those OS limitations. On the other hand, I am having trouble with the definition of autoplay which can have several meanings depending on the context. It could mean to start playback automatically upon app launch, or it could also mean to automatically skip to the next song. The playlist use case is the one I have coded in the built-in demo, although it seems some people are still having trouble making it work.

sansopheakh commented 4 years ago

@codinronan Thanks for the kind words and please go ahead and post the link to the plugin. It is great that you found a way to overcome those OS limitations. On the other hand, I am having trouble with the definition of autoplay which can have several meanings depending on the context. It could mean to start playback automatically upon app launch, or it could also mean to automatically skip to the next song. The playlist use case is the one I have coded in the built-in demo, although it seems some people are still having trouble making it work.

@arielfaur i'm looking for the code to start play the sound automatically upon app launch also. If you resolved please alert me. Thank bro.

arielfaur commented 4 years ago

@sansopheakh Sorry, I haven't been able to keep up with this lately. Hope I can release a new version soon. Contributors are welcome 😄