My question is about a problem that I continually come across when dealing with the media player not only with audio files but also video files. When the "play button" is pressed more that about 10 times without restarting the application, the app will stop and the LogCat will display the error message "E/MediaPlayer: error (1, -19)".
I have searched Stack as well as the Android developer pages on the media player, but cannot figure out how to remedy this problem. The code below seems to get me to about 16 presses of the play button but no more. This is significantly more than other methods I have tried.
This particular code plays short animal sounds for my nephew to recognize. I feel like I am missing something with the media player, but cannot put my finger on it.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient"
tools:context="com.curiousca.griddemo.MainActivity">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:id="@+id/bluejay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_draw"
android:layout_margin="5dp"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowWeight="1"
android:onClick="onClick"
android:text="Bluejay" />
</GridLayout>
`
public void onClick(View view) {
int id = view.getId();
String songId = "";
songId = view.getResources().getResourceEntryName(id);
int resourceId = getResources().getIdentifier(songId, "raw", getPackageName());
MediaPlayer mediaPlayer = MediaPlayer.create(this, resourceId);
if (mediaPlayer != null){
mediaPlayer.start();
}
//Log.i("Click", String.valueOf(view.getId()));
Log.i("tap", songId);
}
swooby,
Instantiate the Media player
private static MediaPlayer mediaPlayer;
public static MediaPlayer getMediaPlayer() {
return mediaPlayer;
It's best to place the code in a try/catch block to handle any exceptions that occur
try {
if (btn == firstBird){
stopPlaying();
mediaPlayer = MediaPlayer.create(getActivity()
.getApplicationContext(),R.raw.bird_one);
mediaPlayer.start();
mediaPlayer.setLooping(true);
timerTextView.setText("15:00");
catch (IllegalStateException e){
stopPlaying();
}
Create extra methods to prevent rewriting code
A "stopPlaying()" method is helpful to stop and release the media player. This method should be called to ensure the media player is not previously engaged or playing from another activity, fragment, or current activity/fragment.
public void stopPlaying() {
if (mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
If you are looping the sound within a countdown timer, be sure to use a method like this...
@Override
public void onFinish() {
resetTimer();
stopPlaying();
}
Lastly include an onCompletion() method within your "stop button" code
if (btn == stopPlay){
onCompletion(mediaPlayer);
}
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
This way the media player is stopped and released for the next operation.