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)