我知道有很多关于这个问题的问题,但没有一个对我有帮助.我尝试了所有但没有,仍然在我的logcat和应用程序崩溃中得到IllegalStateException.
这是我的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,isPlaying); }
这不是整个活动,我的活动也有滑动菜单,但这是我的MediaPlayer的代码..非常感谢!
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)
解决方法
这里:
if(player.isPlaying()) {
在播放器尚未初始化时,您似乎正在调用“播放()方法”.在这种情况下,此方法抛出此IllegalStateException.
我认为理由是你在onCreate中实际发布了(!)MediaPlayer,请参阅:
// Release media instance to system player.release();
您应该更好地使用Activity生命周期方法来管理释放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(); }