如何解决MediaPlayer IllegalStateException问题?

时间:2014-02-24 20:33:34

标签: android media-player

我知道有很多关于这个问题的问题,但没有一个问题对我有帮助。我尝试了ALL但没有,在我的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, Toast.LENGTH_LONG).show();
        Log.d(TAG, 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)

3 个答案:

答案 0 :(得分:9)

下面:

 if(player.isPlaying()) {

在播放器尚未初始化时,您似乎正在调用ìsPlaying()方法。在这种情况下,此方法抛出此IllegalStateException。

我认为原因是你的onCreate实际上释放了(!)MediaPlayer,请参阅:

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

您最好使用活动生命周期方法来管理发布您的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();
} 

答案 1 :(得分:0)

这听起来像是一个初始化问题。虽然我没有做过大量的研究,但这里有一个可能的重复:

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

这是另一个副本,也指向初始化:

android java.lang.IllegalStateException MediaPlayer.isPlaying

这是MediaPlayer上的开发文档,在调用isPlaying()之前说明你需要初始化播放器:

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

答案 2 :(得分:0)

donfuxx完美回答。

如果您不想在创建/重新加载活动时开始播放声音,请在player.start中删除Onresume();