androidionic-frameworkcordova-media-plugin

Why Ionic 1 Media Plugin Error {"code" : 1} after playing N times? (android)


I know this is strange, but this is my observation:

I managed to play some music with the Media Plugin in Ionic 1 using:

//Method 1: just plugin
mediaRes = new Media(myMusicPath, onMediaSuccess, onMediaError, onMediaStatus);

//Method 2: ngCordova
mediaRes = $cordovaMedia.newMedia(myMusicPath)
mediaRes.play().then(onMediaSuccess, onMediaError, onMediaStatus);

As read from other posts, the path is crucial:

"/android_asset/www/music/mymusic.mp3"

Basically the path name has been printed on my HTML so it won't be wrong.

For the first N times, it works properly. Then, on the (N + 1)th, it fails with error {"code":1}.

Why this happens? Do I need to somehow clear cache or something before re-initializing the variable? Currently I re-initialize the variable every time i run it.

EDIT:

Seems that it is OK to play infinitely if I just perform 'play()' without re-initializing the same variable again. Would continue observing...


Solution

  • release()

    Releases the underlying operating system’s audio resources. This is particularly important for Android, since there are a finite amount of OpenCore instances for media playback. Applications should call the release function for any Media resource that is no longer needed.

    Apparently, the release function is required. And that solves the problem.