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;
}
}
문제는 MediaPlayer를에서 오는이 응용 프로그램도 정지 어떤 시점에서, 내 응용 프로그램에 대한 모든 코드를, 또는 노래가 항상하지 않습니다 카운터가 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)