2

I'm working on a simple Mp3 player. A.mp3 is at the sdcard.

When I click the button play noting happens, why?

I don't know what's wrong with it.

Here's the MainActivity file:

    public class MainActivity extends Activity implements OnClickListener{
       private Button play;
       private Button pause;
       private Button stop;
       private MediaPlayer mediaPlayer = new MediaPlayer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        play =(Button)findViewById(R.id.play);
        pause = (Button) findViewById(R.id.pause);
        stop = (Button) findViewById(R.id.stop);
        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        stop.setOnClickListener(this);

        initMediaPlayer(); 

    }
    private void initMediaPlayer()
    {
        try {
            File file =new   File(Environment.getExternalStorageDirectory(),"A.mp3");
            mediaPlayer.setDataSource(file.getPath());
            mediaPlayer.prepareAsync();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.play:
            if (!mediaPlayer.isPlaying()) {
                mediaPlayer.start(); 
            }
            break;
        case R.id.pause:
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause(); 
            }
            break;
        case R.id.stop:
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.reset(); 
                initMediaPlayer();
            }
            break;
        default:
            break;
        }
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();

        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

 }

Here is the Layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<Button
    android:id="@+id/play"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Play" />
<Button
    android:id="@+id/pause"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Pause" />
<Button
    android:id="@+id/stop"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Stop" />

I found that the path of sdcard is "/storage/sdcard1/". so I use mediaPlayer.setDataSource("/storage/sdcard1/A.mp3");But it's the same..setDataSource did not run

vanke xu
  • 109
  • 1
  • 8

1 Answers1

0

Going by your replies in the comments, I have updated my answer.
1..

private void initMediaPlayer()
{
    try {
        File file =new   File(Environment.getExternalStorageDirectory(),"A.mp3");
        mediaPlayer.setDataSource(file.getPath());
        mediaPlayer.prepare(); // Replace this line.

    } catch (Exception e) {
        e.printStackTrace();
    }
}

You could try using

public void prepareAsync ()

instead of prepare. As prepare() uses synchronous threading which can block the program.

http://developer.android.com/reference/android/media/MediaPlayer.html#prepareAsync()

Keep using the Async Prepare method, it is more efficient.

2..
Also if you set an OnErrorListener, this will check for errors when you are setting the data source.

public void setOnErrorListener (MediaPlayer.OnErrorListener listener)

http://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener%28android.media.MediaPlayer.OnErrorListener%29

3..
If you are showing up an error:

Tag MediaPlayer start called in state 0;Tag MediaPlayer error(-38,0)

This is showing that the state of the MediaPlayer is not inialised. You can see the states here:

http://developer.android.com/reference/android/media/MediaPlayer.html

4..
Ensure that you have your parameter to set data source in the correct format:

setDataSource(FileDescriptor), or setDataSource(String), or setDataSource(Context, Uri), or setDataSource(FileDescriptor, long, long) transfers a MediaPlayer object in the Idle state to the Initialized state.

5..
Then using an OnPrepared() Listener http://developer.android.com/reference/android/media/MediaPlayer.OnPreparedListener.html

You can then safely call mediaplayer.start()

6..
Also see this question Media Player called in state 0, error (-38,0)

7..
If this doesn't fix the problem, let me know.

Community
  • 1
  • 1
  • I use Log.d().And I found that initMediaPlayer() did not run.why? – vanke xu Jun 18 '15 at 13:18
  • I've try,it's the same. – vanke xu Jun 18 '15 at 14:53
  • Thanks so much..I found that .mediaPlayer.setDataSource() did not run,So maybe the path is wrong..I use android handset to launch the app..I don't know where do I put the A.mp3 file..I just copy from computer into the sdcard. – vanke xu Jun 19 '15 at 05:58
  • Thanks ...I've read..But I still don't know how to get the correct path of my A.mp3..I just put it in sdcard.But I am not sure the correct path of the sdcard.I've try many paths I searched on the website,but still not working... – vanke xu Jun 20 '15 at 07:46