Phonegap Sound stops playing after calling playAudio () 30 to 40 times

In my application, I gave an audio response when I clicked the mouse. This is how i did

i called a function like this

<center><input id="click_flip" type="button" value="Click Me" class="clickme" onclick="callplay()"></center>

here is the function

<script type="text/javascript" charset="utf-8">

        function callplay()
        {
                                         if(voice=="male")
                                        playAudio('/android_asset/www/Mobile/sound/Male/'+rand1+'.mp3');
                                        else
                                        playAudio('/android_asset/www/Mobile/sound/Female/'+rand1+'.mp3');
        }
        // Audio player
        //

      var my_media = null;

        // Play audio
        //
        function playAudio(src) {




                // Create Media object from src

                my_media = new Media(src, onSuccess, onError);
            // else play current audio
            // Play audio
            my_media.play();



        }





        // onSuccess Callback
        //
        function onSuccess() {
            console.log("playAudio():Audio Success");
        }

        // onError Callback 
        //
        function onError(error) {
         //   alert('code: '    + error.code    + '\n' + 
                  'message: ' + error.message + '\n');
        }


        </script>

But when I repeat the button, click a few (about 30-40) times. The sound gives no answers.

After that, using this link I added this function

if(my_media){ 
   my_media.stop();
   my_media.release();
 }

also tried this

function playAudio(url) {
    try {

        var my_media = new Media(url,
            // success callback
            function () {
                **my_media.release();**
            },
            // error callback
            function (err) {
                **my_media.release();**
            });

        // Play audio
        my_media.play();
    } catch (e) {
        alert(e.message);
    }
}

but does not work. Please suggest

+4
source share
2 answers

I have the same problem, now I am trying to use this method and it seems to work:

var my_media = null;

function playAudio(src)
{
    if(my_media != null) my_media.release();
    my_media = new Media(src);
    my_media.play();
}

, , android. -, , . , Media , Media.

0

. API. API.

35-40 .

:

HTML

    <audio id="successSound" 
      src="/android_asset/www/audio/correct.mp3" 
      type="audio/mpeg">
     </audio>



var my_media = null;
var mediaTimer = null;

function playAudio(id) {
    var audioElement = document.getElementById(id);
    var src = audioElement.getAttribute('src');
    // Create Media object from src
    // alert(src);
    // alert(getPathMedia());
    my_media = new Media(src, onSuccess, onError);

    // 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);
    }
}

// Pause audio
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() {
    // alert('success');
}

// onError Callback
function onError(error) {
    switch(error.code){
        case MediaError.MEDIA_ERR_ABORTED:
        alert('MEDIA_ERR_ABORTED code: '    + error.code);
        break;
        case MediaError.MEDIA_ERR_NETWORK:
        alert('MEDIA_ERR_NETWORK code: '    + error.code);
        break;
        case MediaError.MEDIA_ERR_DECODE:
        alert('MEDIA_ERR_DECODE code: '    + error.code);
        break;
        case MediaError.MEDIA_ERR_NONE_SUPPORTED:
        alert('MEDIA_ERR_NONE_SUPPORTED code: '    + error.code);
        break;
        default:
        {
            alert('Un Known: '    + error.code);
            navigator.notification.vibrate(2000);
            playAudio("errorSound");
        }
    }
}

function setAudioPosition(position) {
    document.getElementById('audio_position').innerHTML = position;
}

:

var failCounter = 0;
var successCounter = 0;

var srcSuccess = "/android_asset/www/audio/correct.mp3";
var srcFail = "/android_asset/www/audio/error_long.mp3";

var my_media_success = null;
var my_media_fail = null;
function playAudioSuccess() {
    // stopAudio(my_media);
    if (my_media_success != null) {
        my_media_success.release();
    }
    successCounter = successCounter + 1;
    // Create Media object from src
    // alert(src);
    // alert(getPathMedia());
    try {
        my_media_success = new Media(srcSuccess, onSuccess, onError);
        // my_media.setVolume('1.0');
        // if (successCounter >= 35) {
        //     alert("success count " + successCounter + " total counting " + ( successCounter + failCounter));
        // }
        // Play audio
        my_media_success.play();

    } catch (err) {
        alert(err);
    }

}

function playAudioFail() {
    try {
        // stopAudio(my_media);
        if (my_media_fail != null) {
            my_media_fail.release();
        }

        failCounter = failCounter + 1;
        // Create Media object from src
        // alert(src);
        // alert(getPathMedia());
        my_media_fail = new Media(srcFail, onSuccess, onError);
        // my_media_fail.setVolume('1.0');
        // if (failCounter >= 35) {
        //     alert("fail count " + failCounter + " total counting " + ( successCounter + failCounter));
        // }

        // Play audio
        my_media_fail.play();
    } catch (err) {
        alert(err);
    }
}

// Pause audio
function pauseAudio() {
    if (my_media) {
        my_media.pause();
    }
}

// Stop audio
function stopAudio(my_media) {
    if (my_media) {
        my_media.stop();
    }
    // clearInterval(mediaTimer);
    // mediaTimer = null;
}

// onSuccess Callback
//
function onSuccess() {
    // alert('success');
}

// onError Callback
function onError(error) {
    switch(error.code){
        case MediaError.MEDIA_ERR_ABORTED:
        alert('MEDIA_ERR_ABORTED code: '    + error.code);
        break;
        case MediaError.MEDIA_ERR_NETWORK:
        alert('MEDIA_ERR_NETWORK code: '    + error.code);
        break;
        case MediaError.MEDIA_ERR_DECODE:
        alert('MEDIA_ERR_DECODE code: '    + error.code);
        break;
        case MediaError.MEDIA_ERR_NONE_SUPPORTED:
        alert('MEDIA_ERR_NONE_SUPPORTED code: '    + error.code);
        break;
        default:
        {
            alert('Un Known: '    + error.code);
            navigator.notification.vibrate(1000);
            setTimeout(function() {
                playAudioFail();
            }, delayInMilliseconds);
        }
    }
}

function setAudioPosition(position) {
    document.getElementById('audio_position').innerHTML = position;
}

!

1) getElementById, src, .

2) ()

3) , ...

4) setAudioPosition, .

( ), , , .

5) , , , , ,

- , , ,

navigator.notification.vibrate(1000);
            setTimeout(function() {
                playAudioFail();
            }, delayInMilliseconds);

6) release,

(leasson ) , , .

if (my_media_fail != null) {
            my_media_fail.release();
        }

7) javascript try catch )) W3School

0

Source: https://habr.com/ru/post/1534034/


All Articles