adaliszk
adaliszk

Reputation: 604

Cordova Media stop working

I tried to find a solution, but have not found anything that would finally solved this problem. So, the problem is pretty basic, there are many places where I found him and many places resolved it. Anyway the problem is that, the Cordova Media plugin after 50-100 successfully played sound has stop working on Android 4.2.2

adamos42@a42book:~$ cordova -v
3.5.0-0.2.6

adamos42@a42book:~/place/of/project$ cordova plugins
com.badrit.MacAddress 0.1.0 "MacAddress"
com.megster.cordova.bluetoothserial 0.3.0 "Bluetooth Serial"
com.randdusing.bluetoothle 1.0.2 "Bluetooth LE"
org.apache.cordova.battery-status 0.2.10 "Battery"
org.apache.cordova.device 0.2.11-dev "Device"
org.apache.cordova.file 1.2.0 "File"
org.apache.cordova.file-transfer 0.4.5-dev "File Transfer"
org.apache.cordova.geolocation 0.3.8 "Geolocation"
org.apache.cordova.media 0.2.12-dev "Media"
org.apache.cordova.media-capture 0.3.2-dev "Capture"
org.apache.cordova.network-information 0.2.11-dev "Network Information"
org.apache.cordova.vibration 0.3.10-dev "Vibration"

My code:

var SOUND = true;
var myMedia = null;
var mediaSRC = '';
var LastSnd = '';

function sound(snd)
{
    if(SOUND)
    {       
        LastSnd = snd;
        mediaSRC = "/android_asset/www/sound/"+snd+".mp3";
        console.log("Media::load('"+mediaSRC+"');");

        myMedia = new Media(mediaSRC, media_success, media_error);
        myMedia.play();

        return true;
    }
    return false;
}

function media_success()
{
    console.debug('Media::success(\''+LastSnd+'\');');

    myMedia.stop();
    myMedia.release();

    mediaSRC = "";
}

function media_error(error)
{
    console.error('Media::error(): '+json_encode(error));

    if(mediaSRC != "" && error.message != undefined)    
        alert(error.message);
}

So every success media.play() will trigger the media_success function which release the Media output on Android device, my program use this sound function sereveal times, most of the times the cordova break the playback and play the lattest sound and every sound trigger the media_error where the error is: {code:0}

I need a little help from those who have managed to use it, what it is the problem with my code? One more thing: In the logcat output has a weird thing:

D/MediaPlayer(xxxxx): Don't notify duration to com.adamos42.application!

Upvotes: 1

Views: 1491

Answers (1)

adaliszk
adaliszk

Reputation: 604

Now I try use this way and it seems working:

var SOUND = true;
var myMedia = null;
var mediaSRC = '';
var LastSnd = '';

function sound(snd)
{
    if(SOUND)
    {       
        LastSnd = snd;
        mediaSRC = "/android_asset/www/sound/"+snd+".mp3";
        console.log("Media::load('"+mediaSRC+"');");

        if(myMedia != null) myMedia.release();

        myMedia = new Media(mediaSRC, media_success, media_error);
        myMedia.play();

        return true;
    }
    return false;
}

function media_success()
{
    console.debug('Media::success(\''+LastSnd+'\');');

    //myMedia.stop();
    //myMedia.release();

    mediaSRC = "";
}

function media_error(error)
{
    console.error('Media::error(): '+json_encode(error));

    if(mediaSRC != "" && error.message != undefined)    
        alert(error.message);
}

Upvotes: 1

Related Questions