如何在没有此错误的情况下退出我的应用程序

时间:2012-12-17 16:39:01

标签: android android-intent android-activity media-player

我创建了卡拉OK应用程序。但我无法阻止MediaPlayer出错。当我按回按钮回家时,我得到“抱歉,应用程序已停止工作”如果我尝试开始另一项活动,同样的事情会发生

public class Main extends Activity implements MediaPlayerControl {
private MediaController mMediaController;
private MediaPlayer mMediaPlayer;
Handler mHandler = new Handler();
public TextView subtitles,subtitles2;

static Context context;

@SuppressLint("HandlerLeak")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.Main);

    subtitles = (TextView) findViewById(R.id.subs2);
    subtitles2 = (TextView) findViewById(R.id.subs21);

    mMediaPlayer = new MediaPlayer();
    mMediaController = new MediaController(this)
    /*
     * {
     * 
     * @Override public void hide() { mMediaController.show(0); } }
     */;

    mMediaController.setMediaPlayer(Main.this);
    mMediaController.setAnchorView(findViewById(R.id.AudioView));
    mMediaController.setPrevNextListeners(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Next button clicked
            Intent intent = new Intent(Main.this, Hello.class);
            startActivity(intent);

        }
    }, new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Previous button clicked
            Intent intent = new Intent(Main.this, Sorry.class);
            startActivity(intent);
        }
    });

    // String audioFile = "" ;
    try {
        mMediaPlayer.setDataSource(this, Uri
                .parse("android.resource://com.app.suadmon/raw/buchatri"));
        mMediaPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }

    mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            final long currentPos = mMediaPlayer.getCurrentPosition();
            if (currentPos < 5130) {
                subtitles.setText("1111+");

            } else if (currentPos > 5130 && currentPos < 10572) {
                subtitles.setText("555+");

            } else if (currentPos > 10572 && currentPos < 10597) {
                subtitles.setText("666+");

            } else if (currentPos > 15312 && currentPos < 18478) {
                subtitles.setText("777+");

            } else if (currentPos > 18478 && currentPos < 24191) {
                subtitles.setText("888+");

            } else if (currentPos > 24191 && currentPos < 28137) {
                subtitles.setText("999+");

            } else if (currentPos > 28137 && currentPos < 29500) {
                subtitles.setText("Thank you");
                subtitles2.setText(".............");
            } 
            mHandler.sendEmptyMessageDelayed(0, 1);
        }
    };
    mHandler.sendEmptyMessage(0);

    mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            mHandler.post(new Runnable() {
                public void run() {
                    mMediaController.show(0);
                    // mMediaPlayer.reset();
                    mMediaPlayer.start();
                }
            });
        }
    });

    mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub
            Intent stopplay = new Intent(Main.this, Hello.class);
            startActivity(stopplay);
        }
    });
}

protected void onDestroy() {
    super.onDestroy();
    mMediaPlayer.stop();
    mMediaPlayer.release();
}
public void releaseMediaPlayer(){
    if(mMediaPlayer != null){
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

public boolean canPause() {
    return true;
}

public boolean canSeekBackward() {
    return true;
}

public boolean canSeekForward() {
    return true;
}

public int getBufferPercentage() {
    int percentage = (mMediaPlayer.getCurrentPosition() * 100)
            / mMediaPlayer.getDuration();
    return percentage;
}

public int getCurrentPosition() {
    return mMediaPlayer.getCurrentPosition();
}

public int getDuration() {
    return mMediaPlayer.getDuration();
}

public boolean isPlaying() {
    return mMediaPlayer.isPlaying();
}

public void seekTo(int pos) {
    mMediaPlayer.seekTo(pos);

}

@Override
public void pause() {
    // TODO Auto-generated method stub
    mMediaPlayer.pause();

}

protected void onPause() {
    super.onPause();
    mMediaPlayer.getCurrentPosition();
    mMediaPlayer.pause();
}

protected void onResume() {
    super.onResume();
    mMediaPlayer.seekTo(0);
    mMediaPlayer.start();
}

// public void onUserLeaveHint(){
// mMediaPlayer.stop();
// super.onUserLeaveHint();
// }
/*
 * public void stopPlayback(){ mMediaPlayer.stop(); }
 */

public void start() {
    mMediaPlayer.start();
}

public boolean onTouchEvent(MotionEvent event) {
    mMediaController.show(0);
    return false;
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    finish();
    Intent intent = new Intent(Main.this, Show.class);
    startActivity(intent);
    super.onBackPressed();
}

}

和Log Cat's在这里。 Error in logcat 我怎样才能解决这个问题。我不知道。 谁能帮我。请!!!

1 个答案:

答案 0 :(得分:2)

您需要在活动结束前停止并释放媒体播放器。尝试将此添加到您的活动中:

@Override
protected void onDestroy() {
  super.onDestroy();
  if(mMediaPlayer != null)
  {
    mMediaPlayer.stop();
    mMediaPlayer.release();
  }
}