Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve MediaPlayer IllegalStateException issue?

I know there is a lot of questions about this subject, but none of them helped me. I tried ALL but nothing, still getting IllegalStateException in my logcat and app crashes.

Here is my MainActivity.java:

package com.orar.cngcnasaud;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private ListView mDrawerList;
    private DrawerLayout mDrawer;
    private CustomActionBarDrawerToggle mDrawerToggle;
    private String[] menuItems;


    private static final String TAG = "AudioDemo";
    private static final String isPlaying = "Media is Playing"; 
    private static final String notPlaying = "Media has stopped Playing"; 

    MediaPlayer player;
    Button playerButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_drawer);

        player = MediaPlayer.create(this, R.raw.gc);
        player.setLooping(false); // Set looping

        // Get the button from the view
        playerButton = (Button) this.findViewById(R.id.buttonmp);
        playerButton.setText(R.string.stop_label);
        playerButton.setOnClickListener((OnClickListener) this);

        // Begin playing selected media
        demoPlay();

        // Release media instance to system
        player.release();


        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        // set a custom shadow that overlays the main content when the drawer
        // opens
        mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        _initMenu();
        mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
        mDrawer.setDrawerListener(mDrawerToggle);
    }

        public void onClick(View v) {
            Log.d(TAG, "onClick: " + v);
            if (v.getId() == R.id.buttonmp) {
                playPause();
            }

    }




    private void demoPause() {
        // TODO Auto-generated method stub
        player.pause();
        playerButton.setText(R.string.play_label);
        Toast.makeText(this, notPlaying, Toast.LENGTH_LONG).show();
        Log.d(TAG, notPlaying);
}

    private void playPause() {
        // TODO Auto-generated method stub
        if(player.isPlaying()) {
          demoPause();
        } else {
          demoPlay();
        }   
    }

    private void demoPlay() {
        // TODO Auto-generated method stub
        player.start();
        playerButton.setText(R.string.stop_label);
        Toast.makeText(this, isPlaying, Toast.LENGTH_LONG).show();
        Log.d(TAG, isPlaying);
}

This is not the whole activity, my activity has a sliding menu too, but this is the code for my MediaPlayer .. Thanks a lot !

L.E:

LOGCAT:

02-24 19:09:24.011: E/AndroidRuntime(1323): FATAL EXCEPTION: main
02-24 19:09:24.011: E/AndroidRuntime(1323): java.lang.IllegalStateException
02-24 19:09:24.011: E/AndroidRuntime(1323):     at android.media.MediaPlayer.isPlaying(Native Method)
02-24 19:09:24.011: E/AndroidRuntime(1323):     at com.orar.cngcnasaud.MainActivity.playPause(MainActivity.java:90)
02-24 19:09:24.011: E/AndroidRuntime(1323):     at com.orar.cngcnasaud.MainActivity.onClick(MainActivity.java:72)
02-24 19:09:24.011: E/AndroidRuntime(1323):     at android.view.View.performClick(View.java:4204)
02-24 19:09:24.011: E/AndroidRuntime(1323):     at android.view.View$PerformClick.run(View.java:17355)
02-24 19:09:24.011: E/AndroidRuntime(1323):     at android.os.Handler.handleCallback(Handler.java:725)
02-24 19:09:24.011: E/AndroidRuntime(1323):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 19:09:24.011: E/AndroidRuntime(1323):     at android.os.Looper.loop(Looper.java:137)
02-24 19:09:24.011: E/AndroidRuntime(1323):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-24 19:09:24.011: E/AndroidRuntime(1323):     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 19:09:24.011: E/AndroidRuntime(1323):     at java.lang.reflect.Method.invoke(Method.java:511)
02-24 19:09:24.011: E/AndroidRuntime(1323):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-24 19:09:24.011: E/AndroidRuntime(1323):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-24 19:09:24.011: E/AndroidRuntime(1323):     at dalvik.system.NativeStart.main(Native Method)
like image 539
Ezekiel Avatar asked Feb 24 '14 20:02

Ezekiel


People also ask

Why does oncreate throw IllegalStateException when launching a mediaplayer?

You seem to be calling ìsPlaying () method while the player has not yet been initialized. In that case this method throws this IllegalStateException. I think reason is that in your onCreate you actually release (!) the MediaPlayer, see: You should better use the Activity life cycle methods to manage releasing your MediaPlayer:

What is an IllegalStateException?

It is thrown when a method call illegal or a method is called at incorrect time. For example, once we start a thread, we cannot restart the same thread again; if we try to do that, it throws a runtime exception i.e., IllegalStateException.

Why we get IllegalStateException when we call start () method only once?

In the above example if we call start () method only once on thread t then we will not get any java.lang.IllegalStateException because we are not calling the start () method after the starting of thread (i.e we are not calling the start () method at an illegal or an inappropriate time.) Writing code in comment?

What is an example of illegalmonitorstateexception?

Here’s an example of an IllegalMonitorStateException thrown when the Iterator.remove () method is called to remove an element from an ArrayList before calling the next () method:


3 Answers

Here:

 if(player.isPlaying()) {

You seem to be calling ìsPlaying() method while the player has not yet been initialized. In that case this method throws this IllegalStateException.

I think reason is that in your onCreate you actually release (!) the MediaPlayer, see:

   // Release media instance to system
    player.release();

You should better use the Activity life cycle methods to manage releasing your MediaPlayer:

@Override
public void onPause() {
    player.stop();
    player.release();
    super.onPause();
}

@Override
public void onResume() {
    player = MediaPlayer.create(this, R.raw.gc);
    player.setLooping(false);
    player.start(); 
    super.onResume();
} 
like image 95
donfuxx Avatar answered Sep 29 '22 10:09

donfuxx


It sounds like an initialization problem. Although I have not done a ton of research, here is a possible duplicate:

java.lang.IllegalStateException in MediaPlayer.isplaying() method

Here is another duplicate, also pointing to initialization:

android java.lang.IllegalStateException MediaPlayer.isPlaying

And here is the dev docs on MediaPlayer, stating prior to calling isPlaying() you'll need to initialize the player:

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

like image 30
Phoenix Avatar answered Sep 29 '22 09:09

Phoenix


perfectly answered by donfuxx.

In case you don't want the sound to start on the creation/reload of your activity, then remove the player.start in Onresume();

like image 39
Arnaud Avatar answered Sep 29 '22 11:09

Arnaud