JINOOHHH
JINOOHHH

Reputation: 3

Attempt to call getDuration in wrong state: mPlayer=0x0, mCurrentState=1 How can I solve it?

I am a novice developer trying to make an audio player on android.

On Youtube (https://www.youtube.com/watch?v=hNbXrlrWzGY&list=PL9vy4y29rrd4x5pAbowit8gpjsXAai0yF&index=8),

I did the same way I implemented it at 18:19, but it was played in Youtube video, but I didn't.

I also tried applying the content here Media Player called in state 0, error (-38,0),

but the music still doesn't play.

Here is my code.

    musicPlayer = new MediaPlayer();
    MediaController controller = new MediaController(this);
    try {
        musicPlayer.setDataSource(song.getPath());
        musicPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    String duration = millisecondsToString(musicPlayer.getDuration());`

What's the difference between that YouTube and the way I did it?

Thank you.

++I invoke start() in onClick()

public void onClick(View v) {
    switch(v.getId()) {
        case R.id.playbtn :
            if(musicPlayer.isPlaying()) {
                musicPlayer.pause();
                playbtn.setBackgroundResource(R.drawable.resume);

            } else {
                musicPlayer.start();
                playbtn.setBackgroundResource(R.drawable.play);

            }
            break;

Upvotes: 0

Views: 3334

Answers (1)

lantian
lantian

Reputation: 176

Firstly sorry for my English.

Are you have invoked the method: player.start() ? .

If not,You need invoked it when player prepare successed.

It may be have a listener for when prepare Ready callback.

Invoke the player.start() when received the event.

Or invoke the player.autoStart() like it.

The method name maybe not to exit but maybe similar

Upvotes: 2

Related Questions