Not able to catch the IllegalStateException of MediaPlayer
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.

add a comment |
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.

"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
add a comment |
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.

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.


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
add a comment |
"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
add a comment |
2 Answers
2
active
oldest
votes
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.
I've tried bothprepare
andprepareAsync
. 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
thenstart
should work fine, but there are also other cases that can throwIllegalStateException
, 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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
I've tried bothprepare
andprepareAsync
. 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
thenstart
should work fine, but there are also other cases that can throwIllegalStateException
, 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
add a comment |
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.
I've tried bothprepare
andprepareAsync
. 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
thenstart
should work fine, but there are also other cases that can throwIllegalStateException
, 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
add a comment |
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.
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.
edited Nov 21 '18 at 9:37
answered Nov 21 '18 at 9:14


AaronAaron
1,7332212
1,7332212
I've tried bothprepare
andprepareAsync
. 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
thenstart
should work fine, but there are also other cases that can throwIllegalStateException
, 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
add a comment |
I've tried bothprepare
andprepareAsync
. 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
thenstart
should work fine, but there are also other cases that can throwIllegalStateException
, 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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 24 '18 at 6:03


Quyet TranQuyet Tran
741516
741516
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
"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