1
MediaPlayer easysong;
MediaPlayer normalsong;
MediaPlayer hardsong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.land_main);
btn3 = (Button)findViewById(R.id.button3);
restartButton = (Button)findViewById(R.id.restartButton);
}
@Override
public void onClick(View v) {
easysong = MediaPlayer.create(MainActivity.this, R.raw.arideniro);
normalsong = MediaPlayer.create(MainActivity.this, R.raw.junior);
hardsong = MediaPlayer.create(MainActivity.this, R.raw.ketsathis);
if (v == btn3) {
btn3.setVisibility(View.GONE);
new Thread(new Runnable() {
public void run() {
if(mstop==1){
if(i==500){
easysong.start();}
else if(i==375){
normalsong.start();
}else if(i==250){
hardsong.start();
}}
while (counter > 0) {
try {
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter--;
runOnUiThread(new Runnable() {
@Override
public void run() {
scoreText.setText(Integer.toString(counter));
}
});
if(i>150){
i/=1.01;}
else if((i>90-(dif/10))){
i-=1;
}
}if (counter==0) {
mChronometer.stop();
playerStop();
runOnUiThread(new Runnable() {
@Override
public void run() {
restartButton.setVisibility(View.VISIBLE);
}
});
}
}
}).start();
}
if(v == restartButton){
counter = 101;
i = 500 - dif;
new Thread(new Runnable() {
public void run() {
if(i==500){
easysong.start();}
else if(i==375){
normalsong.start();
}else if(i==250){
hardsong.start();
}
while (counter > 0) {
try {
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter--;
runOnUiThread(new Runnable() {
@Override
public void run() {
scoreText.setText(Integer.toString(counter));
}
});
if(i>150){
i/=1.01;}
else if(i>90-(dif/10)){
i-=1;
}
}if (counter==0) {
playerStop();
runOnUiThread(new Runnable() {
@Override
public void run() {
restartButton.setVisibility(View.VISIBLE);
}
});
}
}
}).start();
restartButton.setVisibility(View.GONE);
}
}
public void updateTimerText(final String time){
runOnUiThread(new Runnable() {
@Override
public void run() {
mTvTime.setText(time);
}
});
}
@Override
public void onUserLeaveHint() { // This is to stop the music if user presses home button
if(easysong.isPlaying()) {
easysong.stop();
easysong.release();
easysong = null;
} else if(normalsong.isPlaying()) {
normalsong.stop();
normalsong.release();
normalsong = null;
}
else if (hardsong.isPlaying()){
hardsong.stop();
hardsong.release();
hardsong = null;
}
}
public void playerStop(){
if (easysong.isPlaying()) {
easysong.stop();
easysong.release();
easysong = null;
}else if (normalsong.isPlaying()) {
normalsong.stop();
normalsong.release();
normalsong = null;
}else if (hardsong.isPlaying()) {
hardsong.stop();
hardsong.release();
hardsong = null;
}
}
問題は、メディアプレーヤから来て、これは、アプリケーションがフリーズしてもいくつかの点で、私のアプリケーションでそれのためのすべてのコードをある、または歌はいつもませんカウンターが0になると停止します。これは私が直面しているエラーで、いくつかのことを試しましたが、もう何をすべきか分かりません。それで私は何を間違えたのですか?そして、私が間違っていたこととは別に、家のボタンを押しても音楽を止めるようにしましたが、戻るボタンを押すと止める方法はありますか?私がアンドロイドアプリでのMediaPlayerで間違ってやっている何
12-23 23:28:19.780 8897-8897/com.example.user.myapplication D/MediaPlayer: setSubtitleAnchor in MediaPlayer
12-23 23:28:35.193 8897-8897/com.example.user.myapplication W/MediaPlayer: mediaplayer went away with unhandled events
12-23 23:33:21.518 8897-8897/com.example.user.myapplication D/MediaPlayer: create failed:
java.io.IOException: Prepare failed.: status=0x80000000
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1184)
at android.media.MediaPlayer.create(MediaPlayer.java:943)
at android.media.MediaPlayer.create(MediaPlayer.java:914)
at com.example.user.myapplication.MainActivity.onClick(MainActivity.java:114)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
12-23 23:33:22.163 8897-21377/com.example.user.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-11
Process: com.example.user.myapplication, PID: 8897
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference
at com.example.user.myapplication.MainActivity.playerStop(MainActivity.java:207)
at com.example.user.myapplication.MainActivity$2.run(MainActivity.java:167)
at java.lang.Thread.run(Thread.java:761)