0

カラオケアプリを作りました。しかし、エラーなしで 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 here. logcat のエラー どうすればこれを修正できますか。何も思いつきません。誰でも私を助けることができますか?お願いします !!!

4

1 に答える 1

2

アクティビティが終了する前に、メディア プレーヤーを停止して解放する必要があります。これをアクティビティに追加してみてください:

@Override
protected void onDestroy() {
  super.onDestroy();
  if(mMediaPlayer != null)
  {
    mMediaPlayer.stop();
    mMediaPlayer.release();
  }
}
于 2012-12-17T16:47:26.917 に答える