I am using cordova 6.4.0
with cordova-plugin-media
to stream radio stations in an android app. Unfortunately, there is a case where the application no longer responds.
Suppose that the user wants to transmit the radio station, but while the stream is loading, he wants to interrupt it (for example, because the stream is disconnected or takes a very long time).
In this case, I can not cancel the process!
media = new Media("http://direct.franceinfo.fr/live/franceinfo-midfi.mp3?ID=f9fbk29m84", mediaPlayerSuccess, mediaPlayerFail, mediaPlayerStatus); media.play();
Now I want to cancel the flow buffering process, but I cannot. Functions:
media.pause(); media.stop();
throw error messages into the ADB log and call the mediaPlayer-onError callback.
D/AudioPlayer( 3362): AudioPlayer Error: pausePlaying() called during invalid state: 1 ... D/AudioPlayer( 3362): AudioPlayer Error: stopPlaying() called during invalid state: 1
The media.release()
command stops loading a stream! However, only freeing the thread without stopping it causes other, rather large problems:
In most cases, the system responds very slowly and hangs for a few seconds if you call media.release()
on a media object. But if you do this often, the system ends up hanging . This means that he no longer uses remote control commands. The adb log is still working, but in this case no errors are displayed. Only the POWER button works (it locks and unlocks the screen). The only way to restore this state is to reboot the device.
How can I cancel a media stream if it is not already playing? Is this a bug in the plugin?
The attached code snippet that I use to process media streaming logic. As described above ... it basically works, but it slows down or even freezes the device if you call it several times.
function radioControl(action, media_src){ //media_src is a webradio-streamurl. if(action == 'play') { // Initial Play if(media === null){ mediaCreateObject(media_src); } // If we get PLAY but on antoher station else if(media.src != media_src){ mediaReleaseRessources(); mediaCreateObject(media_src); } //interrupt_timer = false; if(media === null){ mediaCreateObject(media_src); } media.play(); } else if (action === 'pause') { //If we get "pause", but it didn't even start yet if(media._duration == -1){ mediaReleaseRessources(); } else{ media.pause(); } } } function mediaCreateObject(media_src){ media = new Media(media_src, mediaPlayerSuccess, mediaPlayerFail, mediaPlayerStatus); } function mediaReleaseRessources(){ media.release(); }