0

Je crée une application de quiz dans laquelle il y a un jeu. Mais je trouve que le son continue à jouer même quand l'application est fermée. Deux fois j'utilise mediaplayer.Le son continue à jouer même lorsque l'application est supprimée

1.en cliquant sur le bouton d'image.

2. en changeant la question.

La question est changée toutes les 15 secondes et après avoir tué l'application, le son est joué toutes les 15 secondes.

imageButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
       mediaPlayer.start(); 

     } 
    }); 
    btnA.setOnClickListener(this); 
    btnB.setOnClickListener(this); 
    btnC.setOnClickListener(this); 
    btnD.setOnClickListener(this); 


    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 

} 

@Override 
protected void onResume() 
{ 
    super.onResume(); 

    questionPlay = db.getMorseQuestionMode(mode); 
    totalQuestion = questionPlay.size(); 

    mCountDown = new CountDownTimer(TIMEOUT, INTERVAL) { 
     @Override 
     public void onTick(long millisUntilFinished) { 
      progressBar.setProgress(progressValue); 
      progressValue++; 

     } 

     @Override 
     public void onFinish() { 
      mCountDown.cancel(); 
      showQuestion(++index); 
     } 
    }; 
    showQuestion(index); 
} 


private void showQuestion(int index) { 

    if (index < totalQuestion) { 
     thisQuestion++; 
     txtQuestion.setText(String.format("%d/%d", thisQuestion, totalQuestion)); 
     progressBar.setProgress(0); 
     progressValue = 0; 

     int QusId = this.getResources().getIdentifier(questionPlay.get(index).getQus().toString(), "raw", getPackageName()); 
     btnA.setText(questionPlay.get(index).getAnswerA()); 
     btnB.setText(questionPlay.get(index).getAnswerB()); 
     btnC.setText(questionPlay.get(index).getAnswerC()); 
     btnD.setText(questionPlay.get(index).getAnswerD()); 
     mCountDown.start(); 
     mediaPlayer= MediaPlayer.create(this,QusId); 
     mediaPlayer.start(); 


    } else { 
     Intent intent = new Intent(this, Done.class); 
     Bundle dataSend = new Bundle(); 
     dataSend.putInt("SCORE", score); 
     dataSend.putInt("TOTAL", totalQuestion); 
     dataSend.putInt("CORRECT", correctAnswer); 
     intent.putExtras(dataSend); 
     startActivity(intent); 
     finish(); 
    } 

} 


@Override 
public void onClick(View v) { 

    mCountDown.cancel(); 
    if (index < totalQuestion) { 
     Button clickedButton = (Button) v; 
     if (clickedButton.getText().equals(questionPlay.get(index).getCorrectAnswer())) { 

      score += 10; // increase score 
      correctAnswer++; //increase correct answer 
      showQuestion(++index); 

     } else { 
      vibrator.vibrate(50); 
      showQuestion(++index); // If choose right , just go to next question 
     } 
     txtScore.setText(String.format("%d", score)); 

    } 

} 


//CLicking back Button 
public void onBackPressed() { 
    showExitAlertBox(); 
} 

public void showExitAlertBox() { 

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this); 
    alertDialog.setMessage("You want to quit the play?"); 

    //Yes Quit then go to category page 
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int button) { 
      Intent intent = new Intent(getApplicationContext(), CategorySecond.class); 
      startActivity(intent); 
     } 

    }); 

    //no Quit stay on same page 
    alertDialog.setNegativeButton("NO", null); 
    alertDialog.show(); 
    mediaPlayer.stop(); 
} 
} 

erreur nous Affichage de

W/MediaPlayer-JNI: MediaPlayer finalisé sans être libéré

+0

appel 'mediaPlayer.reset(); mediaPlayer.release(); mediaPlayer = null' dans la méthode 'onDestroy()'. – Piyush

Répondre

2

Annuler votre minuterie et arrêter MediaPlayer si vous jouez dans onStop

@Override 
public void onStop() { 
    if (mediaPlayer.isPlaying()) 
     mediaPlayer.stop(); 

    if(mCountDown != null) 
     mCountDown.cancel(); 
    super.onStop(); 
} 
+1

Cela a fonctionné merci beaucoup –

+0

@PurshottamGusain si cela a fonctionné s'il vous plaît l'accepter afin qu'il sera utile pour les autres. –

+0

Je l'ai déjà fait ... Je l'ai fait à nouveau –

0

Vous avez pas libéré le MediaPlayer correctement.

Utilisez

protected void onStop(){ 
    mediaPlayer.release(); 
    mediaPlayer = null; 
} 

appeler cette méthode où jamais nécessaire.

+0

en utilisant ce son ne fonctionne pas –

+0

Ensuite, vous ne l'appelez pas où il n'est pas nécessaire – Akshay

+0

pouvez-vous s'il vous plaît pointer où devrais-je l'ajouter –

1

poignée détruire

@Override 
public void onDestroy() { 
    if (mediaPlayer.isPlaying()) 
     mediaPlayer.stop(); 
     mediaPlayer.release(); 
     mediaPlayer = null; 

    if(mCountDown != null) 
     mCountDown.cancel(); 

}