Reputation: 73
I am working on an app using Cordova. In this app I have 30 pages and it should play the sound on every swipe. It works fine until about 20 pages and then I start getting this error
MEDIA_PLAYER_STATE_ERROR
Based on my search on this forum I believe it's coz of the media not being released but the examples I have seen are java based. I am having hard time figuring out where to call release Media.
Would appreciate any help.Thank you.
Below is my current code
function playAudio(src) {
// Create Media object from src
try{
my_media = new Media(src,onSuccess,onError,status_change);
// Play audio
my_media.play();
// Update my_media position every second
if (mediaTimer == null) {
mediaTimer = setInterval(function() {
// get my_media position
my_media.getCurrentPosition(
// success callback
function(position) {
if (position > -1) {
setAudioPosition((position) + " sec");
}
},
// error callback
function(e) {
console.log("Error getting pos=" + e);
setAudioPosition("Error: " + e);
}
);
}, 1000);
}
} catch (e){
console.log("In catch");
releaseMedia();
}
}
function pauseAudio() {
if (my_media) {
my_media.pause();
}
}
// Stop audio
//
function stopAudio() {
if (my_media) {
my_media.stop();
}
clearInterval(mediaTimer);
mediaTimer = null;
}
// onSuccess Callback
//
function onSuccess() {
console.log("playAudio():Audio Success");
}
// onError Callback
//
function onError(error) {
console.log('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
releaseMedia();
}
function releaseMedia(){
if (my_media) {
my_media.release();
}
clearInterval(mediaTimer);
mediaTimer = null;
}
function status_change(code) {
switch (code) {
case Media.MEDIA_STOPPED : console.log("stopped"); break;
case Media.MEDIA_STARTING : console.log("starting"); break;
case Media.MEDIA_RUNNING : console.log("running"); break;
case Media.MEDIA_ERROR: console.log("error");break;
}
}
Error Log in Logcat
"starting",
E/MediaPlayer? error (-19, 0)
V/MediaPlayer? callback application
V/MediaPlayer? Received seek complete
V/MediaPlayer? All seeks complete - return to regularly scheduled program
V/MediaPlayer? back from callback
V/MediaPlayer? getDuration_l
07-13 13:19:35.080 17446-17458/com.aa.apps V/MediaPlayer? callback application
07-13 13:19:35.080 17446-17458/com.aa.apps V/MediaPlayer? back from callback
E/MediaPlayer? Attempt to call getDuration without a valid mediaplayer
V/MediaPlayer? message received msg=100, ext1=-38, ext2=0
E/MediaPlayer? error (-38, 0)
V/MediaPlayer? callback application
V/MediaPlayer? back from callback
V/MediaPlayer-JNI? getDuration: 0 (msec)
E/MediaPlayer? Error (-19,0)
D/AudioPlayer? on completion is calling stopped
E/MediaPlayer? Error (-38,0)
D/AudioPlayer? on completion is calling stopped
V/MediaPlayer-JNI? getCurrentPosition: 0 (msec)
E/MediaPlayer? isPlaying: called in state MEDIA_PLAYER_STATE_ERROR
V/MediaPlayer-JNI? isPlaying: 0
I/chromium? [INFO:CONSOLE(98)] "running",
Upvotes: 1
Views: 2639
Reputation: 73
Adding below code before creating Media in playAudio(src) fixed the issue.
if(my_media != null){
releaseMedia();
}
Upvotes: 1