Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MediaPlayer Error (-38, 0) "stop called in state 0"

I have looked at various different websites looking for a solution for this problem in my code. It is a basic audio player with 3 buttons: play, pause and stop. Play and pause work ok and so does stop but once the audio has been stopped it refuses to start again, throwing an error:

E/MediaPlayer: stop called in state 0
E/MediaPlayer: error (-38, 0)
E/MediaPlayer: Error (-38,0)

This is my MainActivity.java:

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import java.io.IOException;

public class MainActivity extends Activity implements MediaPlayer.OnPreparedListener {

    static Button playButton;
    static Button pauseButton;
    static Button stopButton;
    static SeekBar seekBar;
    private static MediaPlayer player;
static Handler handler;
static Uri audio;

@Override
protected void onCreate(Bundle savedInstanceState) {
    player = MediaPlayer.create(this, R.raw.airbourne_runnin_wild);
    Uri audio = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.airbourne_runnin_wild);
    try {
        player.setDataSource(getAssets().openFd("raw/airbourne_runnin_wild.mp3").getFileDescriptor());
    } catch (IOException e) {
        e.printStackTrace();
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    playButton = (Button) findViewById(R.id.playButton);
    pauseButton = (Button) findViewById(R.id.pauseButton);
    stopButton = (Button) findViewById(R.id.stopButton);
    seekBar = (SeekBar) findViewById(R.id.seekBar);

    player.setOnPreparedListener(this);

    handler = new Handler();

    seekBar.setMax(player.getDuration());

    playButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (!player.isPlaying()) {
                try {
                    player.setDataSource(getAssets().openFd("raw/airbourne_runnin_wild.mp3").getFileDescriptor());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                player.start();
                updateSeekBar();
            }
        }
    });

    pauseButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (player.isPlaying()) {
                player.pause();
            }
        }
    });

    stopButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (player.isPlaying()) {
                player.stop();
                seekBar.setProgress(0);
                seekBar.invalidate();
                try {
                    player.setDataSource(getAssets().openFd("raw/airbourne_runnin_wild.mp3").getFileDescriptor());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            if (b) {
                seekChanged(seekBar, i, b);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            player.stop();
            seekBar.setProgress(0);
            //player.prepareAsync();
            seekBar.invalidate();
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onPrepared(MediaPlayer mediaPlayer) {
    playButton.setEnabled(true);
}

public void seekChanged(SeekBar seekBar, int progress, boolean fromUser) {
    player.seekTo(progress);
}

public void updateSeekBar() {
    seekBar.setProgress(player.getCurrentPosition());

    if (player.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                updateSeekBar();
            }
        };
        handler.postDelayed(notification, 1500);
    }
}
}

And activity_main.xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Play"
            android:id="@+id/playButton"
            android:layout_gravity="center_horizontal|top"
            android:longClickable="false"
            android:layout_weight="1"
            android:layout_column="8"
            android:enabled="false"/>
</TableRow>

<TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Pause"
            android:id="@+id/pauseButton"
            android:layout_gravity="center_horizontal|top"
            android:layout_weight="1"
            android:layout_column="8"
            android:layout_span="7"/>
</TableRow>

<TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop"
            android:id="@+id/stopButton"
            android:layout_column="8"
            android:layout_span="24"
            android:layout_weight="1"/>
</TableRow>

<TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
</TableRow>

<TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <SeekBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/seekBar"
            android:layout_column="8"
            android:layout_span="24"
            android:layout_weight="1"/>
</TableRow>

If anyone could help me resolve this error, it would be much appreciated. I have already searched round for solutions and tried multiple, but none seemed to work properly.

EDIT: I changed a little bit of the code:

My question is still about the error, and this is now my edited code:

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import java.io.IOException;

public class MainActivity extends Activity implements MediaPlayer.OnPreparedListener {

static Button playButton;
static Button pauseButton;
static Button stopButton;
static SeekBar seekBar;
private static MediaPlayer player;
static Handler handler;
static Uri audio;
static boolean canMakeCall = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    player = MediaPlayer.create(this, R.raw.airbourne_runnin_wild);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    playButton = (Button) findViewById(R.id.playButton);
    pauseButton = (Button) findViewById(R.id.pauseButton);
    stopButton = (Button) findViewById(R.id.stopButton);
    seekBar = (SeekBar) findViewById(R.id.seekBar);

    if (canMakeCall = true) {
        player.setOnPreparedListener(this);

        handler = new Handler();

        seekBar.setMax(player.getDuration());

        playButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (!player.isPlaying()) {
                    player.start();
                    updateSeekBar();
                }
            }
        });

        pauseButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (player.isPlaying()) {
                    player.pause();
                }
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (player.isPlaying()) {
                    player.stop();
                    seekBar.setProgress(0);
                    seekBar.invalidate();
                    try {
                        player.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (b) {
                    seekChanged(seekBar, i, b);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mediaPlayer.stop();
                //player.stop();

                seekBar.setProgress(0);
                //player.prepareAsync();
                seekBar.invalidate();
            }
        });
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onPrepared(MediaPlayer mediaPlayer) {
    playButton.setEnabled(true);
    canMakeCall = true;
}

public void seekChanged(SeekBar seekBar, int progress, boolean fromUser) {
    player.seekTo(progress);
}

public void updateSeekBar() {
    seekBar.setProgress(player.getCurrentPosition());

    if (player.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                updateSeekBar();
            }
        };
        handler.postDelayed(notification, 1500);
    }
}
}

I have tried getting the basics working without a seekbar too, just to make sure that isn't causing an error but it lies somewhere within the logic of the play/pause/stop code because it was producing the same error even with the seekbar code omitted.

like image 225
uberdum05 Avatar asked Jul 18 '13 13:07

uberdum05


1 Answers

The problem is once you've stopped it, you setDataSource again. You have to then call prepare()

player.setDataSource(getAssets().openFd("raw/airbourne_runnin_wild.mp3").getFileDescriptor());
player.prepare();

Anytime you call setDataSource() on it you will need to prepare() it again. Think of it as loading the file, getting ready for playback.

By the way I notice you mix up calling MediaPlayer.create() and also call setDataSource() multiple times for the same file. There's no need to do this. Once the MediaPlayer is created and prepared (which calling .create() does for you), you can just call .stop(), .pause(), .play() on it, no need to reset it all the time.

like image 130
Ken Wolf Avatar answered Oct 01 '22 19:10

Ken Wolf