Not able to catch the IllegalStateException of MediaPlayer












0















My code looks like this:



MediaPlayer mp = new MediaPlayer();
mp.setDataSource( "http://.../abc.mp3" );
mp.setOnPreparedListener( ... mp.start(); ... );
mp.prepareAsync();


Real code has checked if mp is prepared before it starts. Mostly it works fine. But sometimes (if weak connection) it still encounters this error:



MediaPlayer: Error (-38,0)
MediaPlayerNative: start called in state 0, mPlayer(0x7424863b40)


So, I decided to catch it:



try {
mp.start();
} catch (IllegalStateException e) {
Toast.makeText(...).show();
} catch (Throwable e) {
Toast.makeText(...).show();
}


But this code does not work, I cannot catch any exception, and that error still displayed in log cat.










share|improve this question

























  • "and that error still displayed in log cat" The exception is thrown because of that error, not the other way around. So you're not going to get rid of the error log by catching the exception.

    – Michael
    Nov 21 '18 at 9:25











  • @Michael But I still can't catch that error

    – Quyet Tran
    Nov 21 '18 at 9:31
















0















My code looks like this:



MediaPlayer mp = new MediaPlayer();
mp.setDataSource( "http://.../abc.mp3" );
mp.setOnPreparedListener( ... mp.start(); ... );
mp.prepareAsync();


Real code has checked if mp is prepared before it starts. Mostly it works fine. But sometimes (if weak connection) it still encounters this error:



MediaPlayer: Error (-38,0)
MediaPlayerNative: start called in state 0, mPlayer(0x7424863b40)


So, I decided to catch it:



try {
mp.start();
} catch (IllegalStateException e) {
Toast.makeText(...).show();
} catch (Throwable e) {
Toast.makeText(...).show();
}


But this code does not work, I cannot catch any exception, and that error still displayed in log cat.










share|improve this question

























  • "and that error still displayed in log cat" The exception is thrown because of that error, not the other way around. So you're not going to get rid of the error log by catching the exception.

    – Michael
    Nov 21 '18 at 9:25











  • @Michael But I still can't catch that error

    – Quyet Tran
    Nov 21 '18 at 9:31














0












0








0








My code looks like this:



MediaPlayer mp = new MediaPlayer();
mp.setDataSource( "http://.../abc.mp3" );
mp.setOnPreparedListener( ... mp.start(); ... );
mp.prepareAsync();


Real code has checked if mp is prepared before it starts. Mostly it works fine. But sometimes (if weak connection) it still encounters this error:



MediaPlayer: Error (-38,0)
MediaPlayerNative: start called in state 0, mPlayer(0x7424863b40)


So, I decided to catch it:



try {
mp.start();
} catch (IllegalStateException e) {
Toast.makeText(...).show();
} catch (Throwable e) {
Toast.makeText(...).show();
}


But this code does not work, I cannot catch any exception, and that error still displayed in log cat.










share|improve this question
















My code looks like this:



MediaPlayer mp = new MediaPlayer();
mp.setDataSource( "http://.../abc.mp3" );
mp.setOnPreparedListener( ... mp.start(); ... );
mp.prepareAsync();


Real code has checked if mp is prepared before it starts. Mostly it works fine. But sometimes (if weak connection) it still encounters this error:



MediaPlayer: Error (-38,0)
MediaPlayerNative: start called in state 0, mPlayer(0x7424863b40)


So, I decided to catch it:



try {
mp.start();
} catch (IllegalStateException e) {
Toast.makeText(...).show();
} catch (Throwable e) {
Toast.makeText(...).show();
}


But this code does not work, I cannot catch any exception, and that error still displayed in log cat.







android android-mediaplayer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 9:19









Aniruddh Parihar

2,18911027




2,18911027










asked Nov 21 '18 at 9:06









Quyet TranQuyet Tran

741516




741516













  • "and that error still displayed in log cat" The exception is thrown because of that error, not the other way around. So you're not going to get rid of the error log by catching the exception.

    – Michael
    Nov 21 '18 at 9:25











  • @Michael But I still can't catch that error

    – Quyet Tran
    Nov 21 '18 at 9:31



















  • "and that error still displayed in log cat" The exception is thrown because of that error, not the other way around. So you're not going to get rid of the error log by catching the exception.

    – Michael
    Nov 21 '18 at 9:25











  • @Michael But I still can't catch that error

    – Quyet Tran
    Nov 21 '18 at 9:31

















"and that error still displayed in log cat" The exception is thrown because of that error, not the other way around. So you're not going to get rid of the error log by catching the exception.

– Michael
Nov 21 '18 at 9:25





"and that error still displayed in log cat" The exception is thrown because of that error, not the other way around. So you're not going to get rid of the error log by catching the exception.

– Michael
Nov 21 '18 at 9:25













@Michael But I still can't catch that error

– Quyet Tran
Nov 21 '18 at 9:31





@Michael But I still can't catch that error

– Quyet Tran
Nov 21 '18 at 9:31












2 Answers
2






active

oldest

votes


















1














You should use prepare instead of prepareAsync if you intend to call start immediately afterward. You get IllegalStateException because it may be out-of-sync when you call start, as it prepares asynchronously and has not finished the transaction yet.



According to prepareAsync:




Prepares the player for playback, asynchronously. After setting the
datasource and the display surface, you need to either call prepare()
or prepareAsync(). For streams, you should call prepareAsync(), which
returns immediately, rather than blocking until enough data has been
buffered.




And prepare:




Prepares the player for playback, synchronously. After setting the
datasource and the display surface, you need to either call prepare()
or prepareAsync(). For files, it is OK to call prepare(), which blocks
until MediaPlayer is ready for playback.




Again, prepare allows you to call start immediately because it runs synchronously. However, there may be other cases that can throw IllegalStateException, please refer to its valid and invalid states.






share|improve this answer


























  • I've tried both prepare and prepareAsync. They worked fine in most time. But sometimes did not (rarely). And I would like to handle these case

    – Quyet Tran
    Nov 21 '18 at 9:26











  • Understood, prepare then start should work fine, but there are also other cases that can throw IllegalStateException, please refer developer.android.com/reference/android/media/… and check your code again. :)

    – Aaron
    Nov 21 '18 at 9:35











  • Thank you. I forgot to release the media players when the activity stops. And this error occurred after an exact number of times I restart the activity

    – Quyet Tran
    Nov 24 '18 at 6:07



















0














I've tried hard to find out what is wrong. And I found that I didn't release the media players when the activity stops. Because the media players are still located in memory so after an exact number of times I restart the activity, the new media players cannot load. And in another activity I use SoundPool, it also cannot load more streams.



@Override
protected void onStop() {
super.onStop();

for (int i = 0; i < mediaPlayerMap.size(); i++) {
int key = mediaPlayerMap.keyAt(i);
MediaPlayer mp = mediaPlayerMap.get(key);
if (mp != null) {
mp.release();
mp = null;
}
}
}


Logcat:



E/AudioFlinger: no more track names available
E/AudioFlinger: createTrack_l() initCheck failed -12; no control block?
E/AudioTrack: AudioFlinger could not create track, status: -12
E/SoundPool: Error creating AudioTrack


Refs:



SoundPool error: no more track names available



https://groups.google.com/forum/#!msg/android-platform/tyITQ09vV3s/jwNdyI2-7iYJ






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408528%2fnot-able-to-catch-the-illegalstateexception-of-mediaplayer%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You should use prepare instead of prepareAsync if you intend to call start immediately afterward. You get IllegalStateException because it may be out-of-sync when you call start, as it prepares asynchronously and has not finished the transaction yet.



    According to prepareAsync:




    Prepares the player for playback, asynchronously. After setting the
    datasource and the display surface, you need to either call prepare()
    or prepareAsync(). For streams, you should call prepareAsync(), which
    returns immediately, rather than blocking until enough data has been
    buffered.




    And prepare:




    Prepares the player for playback, synchronously. After setting the
    datasource and the display surface, you need to either call prepare()
    or prepareAsync(). For files, it is OK to call prepare(), which blocks
    until MediaPlayer is ready for playback.




    Again, prepare allows you to call start immediately because it runs synchronously. However, there may be other cases that can throw IllegalStateException, please refer to its valid and invalid states.






    share|improve this answer


























    • I've tried both prepare and prepareAsync. They worked fine in most time. But sometimes did not (rarely). And I would like to handle these case

      – Quyet Tran
      Nov 21 '18 at 9:26











    • Understood, prepare then start should work fine, but there are also other cases that can throw IllegalStateException, please refer developer.android.com/reference/android/media/… and check your code again. :)

      – Aaron
      Nov 21 '18 at 9:35











    • Thank you. I forgot to release the media players when the activity stops. And this error occurred after an exact number of times I restart the activity

      – Quyet Tran
      Nov 24 '18 at 6:07
















    1














    You should use prepare instead of prepareAsync if you intend to call start immediately afterward. You get IllegalStateException because it may be out-of-sync when you call start, as it prepares asynchronously and has not finished the transaction yet.



    According to prepareAsync:




    Prepares the player for playback, asynchronously. After setting the
    datasource and the display surface, you need to either call prepare()
    or prepareAsync(). For streams, you should call prepareAsync(), which
    returns immediately, rather than blocking until enough data has been
    buffered.




    And prepare:




    Prepares the player for playback, synchronously. After setting the
    datasource and the display surface, you need to either call prepare()
    or prepareAsync(). For files, it is OK to call prepare(), which blocks
    until MediaPlayer is ready for playback.




    Again, prepare allows you to call start immediately because it runs synchronously. However, there may be other cases that can throw IllegalStateException, please refer to its valid and invalid states.






    share|improve this answer


























    • I've tried both prepare and prepareAsync. They worked fine in most time. But sometimes did not (rarely). And I would like to handle these case

      – Quyet Tran
      Nov 21 '18 at 9:26











    • Understood, prepare then start should work fine, but there are also other cases that can throw IllegalStateException, please refer developer.android.com/reference/android/media/… and check your code again. :)

      – Aaron
      Nov 21 '18 at 9:35











    • Thank you. I forgot to release the media players when the activity stops. And this error occurred after an exact number of times I restart the activity

      – Quyet Tran
      Nov 24 '18 at 6:07














    1












    1








    1







    You should use prepare instead of prepareAsync if you intend to call start immediately afterward. You get IllegalStateException because it may be out-of-sync when you call start, as it prepares asynchronously and has not finished the transaction yet.



    According to prepareAsync:




    Prepares the player for playback, asynchronously. After setting the
    datasource and the display surface, you need to either call prepare()
    or prepareAsync(). For streams, you should call prepareAsync(), which
    returns immediately, rather than blocking until enough data has been
    buffered.




    And prepare:




    Prepares the player for playback, synchronously. After setting the
    datasource and the display surface, you need to either call prepare()
    or prepareAsync(). For files, it is OK to call prepare(), which blocks
    until MediaPlayer is ready for playback.




    Again, prepare allows you to call start immediately because it runs synchronously. However, there may be other cases that can throw IllegalStateException, please refer to its valid and invalid states.






    share|improve this answer















    You should use prepare instead of prepareAsync if you intend to call start immediately afterward. You get IllegalStateException because it may be out-of-sync when you call start, as it prepares asynchronously and has not finished the transaction yet.



    According to prepareAsync:




    Prepares the player for playback, asynchronously. After setting the
    datasource and the display surface, you need to either call prepare()
    or prepareAsync(). For streams, you should call prepareAsync(), which
    returns immediately, rather than blocking until enough data has been
    buffered.




    And prepare:




    Prepares the player for playback, synchronously. After setting the
    datasource and the display surface, you need to either call prepare()
    or prepareAsync(). For files, it is OK to call prepare(), which blocks
    until MediaPlayer is ready for playback.




    Again, prepare allows you to call start immediately because it runs synchronously. However, there may be other cases that can throw IllegalStateException, please refer to its valid and invalid states.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 9:37

























    answered Nov 21 '18 at 9:14









    AaronAaron

    1,7332212




    1,7332212













    • I've tried both prepare and prepareAsync. They worked fine in most time. But sometimes did not (rarely). And I would like to handle these case

      – Quyet Tran
      Nov 21 '18 at 9:26











    • Understood, prepare then start should work fine, but there are also other cases that can throw IllegalStateException, please refer developer.android.com/reference/android/media/… and check your code again. :)

      – Aaron
      Nov 21 '18 at 9:35











    • Thank you. I forgot to release the media players when the activity stops. And this error occurred after an exact number of times I restart the activity

      – Quyet Tran
      Nov 24 '18 at 6:07



















    • I've tried both prepare and prepareAsync. They worked fine in most time. But sometimes did not (rarely). And I would like to handle these case

      – Quyet Tran
      Nov 21 '18 at 9:26











    • Understood, prepare then start should work fine, but there are also other cases that can throw IllegalStateException, please refer developer.android.com/reference/android/media/… and check your code again. :)

      – Aaron
      Nov 21 '18 at 9:35











    • Thank you. I forgot to release the media players when the activity stops. And this error occurred after an exact number of times I restart the activity

      – Quyet Tran
      Nov 24 '18 at 6:07

















    I've tried both prepare and prepareAsync. They worked fine in most time. But sometimes did not (rarely). And I would like to handle these case

    – Quyet Tran
    Nov 21 '18 at 9:26





    I've tried both prepare and prepareAsync. They worked fine in most time. But sometimes did not (rarely). And I would like to handle these case

    – Quyet Tran
    Nov 21 '18 at 9:26













    Understood, prepare then start should work fine, but there are also other cases that can throw IllegalStateException, please refer developer.android.com/reference/android/media/… and check your code again. :)

    – Aaron
    Nov 21 '18 at 9:35





    Understood, prepare then start should work fine, but there are also other cases that can throw IllegalStateException, please refer developer.android.com/reference/android/media/… and check your code again. :)

    – Aaron
    Nov 21 '18 at 9:35













    Thank you. I forgot to release the media players when the activity stops. And this error occurred after an exact number of times I restart the activity

    – Quyet Tran
    Nov 24 '18 at 6:07





    Thank you. I forgot to release the media players when the activity stops. And this error occurred after an exact number of times I restart the activity

    – Quyet Tran
    Nov 24 '18 at 6:07













    0














    I've tried hard to find out what is wrong. And I found that I didn't release the media players when the activity stops. Because the media players are still located in memory so after an exact number of times I restart the activity, the new media players cannot load. And in another activity I use SoundPool, it also cannot load more streams.



    @Override
    protected void onStop() {
    super.onStop();

    for (int i = 0; i < mediaPlayerMap.size(); i++) {
    int key = mediaPlayerMap.keyAt(i);
    MediaPlayer mp = mediaPlayerMap.get(key);
    if (mp != null) {
    mp.release();
    mp = null;
    }
    }
    }


    Logcat:



    E/AudioFlinger: no more track names available
    E/AudioFlinger: createTrack_l() initCheck failed -12; no control block?
    E/AudioTrack: AudioFlinger could not create track, status: -12
    E/SoundPool: Error creating AudioTrack


    Refs:



    SoundPool error: no more track names available



    https://groups.google.com/forum/#!msg/android-platform/tyITQ09vV3s/jwNdyI2-7iYJ






    share|improve this answer




























      0














      I've tried hard to find out what is wrong. And I found that I didn't release the media players when the activity stops. Because the media players are still located in memory so after an exact number of times I restart the activity, the new media players cannot load. And in another activity I use SoundPool, it also cannot load more streams.



      @Override
      protected void onStop() {
      super.onStop();

      for (int i = 0; i < mediaPlayerMap.size(); i++) {
      int key = mediaPlayerMap.keyAt(i);
      MediaPlayer mp = mediaPlayerMap.get(key);
      if (mp != null) {
      mp.release();
      mp = null;
      }
      }
      }


      Logcat:



      E/AudioFlinger: no more track names available
      E/AudioFlinger: createTrack_l() initCheck failed -12; no control block?
      E/AudioTrack: AudioFlinger could not create track, status: -12
      E/SoundPool: Error creating AudioTrack


      Refs:



      SoundPool error: no more track names available



      https://groups.google.com/forum/#!msg/android-platform/tyITQ09vV3s/jwNdyI2-7iYJ






      share|improve this answer


























        0












        0








        0







        I've tried hard to find out what is wrong. And I found that I didn't release the media players when the activity stops. Because the media players are still located in memory so after an exact number of times I restart the activity, the new media players cannot load. And in another activity I use SoundPool, it also cannot load more streams.



        @Override
        protected void onStop() {
        super.onStop();

        for (int i = 0; i < mediaPlayerMap.size(); i++) {
        int key = mediaPlayerMap.keyAt(i);
        MediaPlayer mp = mediaPlayerMap.get(key);
        if (mp != null) {
        mp.release();
        mp = null;
        }
        }
        }


        Logcat:



        E/AudioFlinger: no more track names available
        E/AudioFlinger: createTrack_l() initCheck failed -12; no control block?
        E/AudioTrack: AudioFlinger could not create track, status: -12
        E/SoundPool: Error creating AudioTrack


        Refs:



        SoundPool error: no more track names available



        https://groups.google.com/forum/#!msg/android-platform/tyITQ09vV3s/jwNdyI2-7iYJ






        share|improve this answer













        I've tried hard to find out what is wrong. And I found that I didn't release the media players when the activity stops. Because the media players are still located in memory so after an exact number of times I restart the activity, the new media players cannot load. And in another activity I use SoundPool, it also cannot load more streams.



        @Override
        protected void onStop() {
        super.onStop();

        for (int i = 0; i < mediaPlayerMap.size(); i++) {
        int key = mediaPlayerMap.keyAt(i);
        MediaPlayer mp = mediaPlayerMap.get(key);
        if (mp != null) {
        mp.release();
        mp = null;
        }
        }
        }


        Logcat:



        E/AudioFlinger: no more track names available
        E/AudioFlinger: createTrack_l() initCheck failed -12; no control block?
        E/AudioTrack: AudioFlinger could not create track, status: -12
        E/SoundPool: Error creating AudioTrack


        Refs:



        SoundPool error: no more track names available



        https://groups.google.com/forum/#!msg/android-platform/tyITQ09vV3s/jwNdyI2-7iYJ







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 6:03









        Quyet TranQuyet Tran

        741516




        741516






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408528%2fnot-able-to-catch-the-illegalstateexception-of-mediaplayer%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith