如何在对话框中单击正面按钮时重播音频背景?

时间:2017-09-15 20:01:59

标签: android audio

我正在创建一个简单的多项选择测验应用程序(来自教程,我想给它一个背景音乐),我想问的是当我重播背景音频/媒体播放器时当答案不正确时(不是独立按钮)点击.setPositiveButton,所以当我想通过按对话框按钮重复MainActivity时,正在运行的媒体播放器将停止并从头开始重新开始,这是我的代码:



package com.fernando.android;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.media.MediaPlayer;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    Button jawab1, jawab2, jawab3, jawab4;

    TextView nilai, pertanyaan;

    private Pertanyaan mPertanyaan = new Pertanyaan();

    private String mJawab;
    private int mNilai = 0;
    private int mPertanyaanLength = mPertanyaan.mPertanyaan.length;

    Random r;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MediaPlayer mp;

        mp = MediaPlayer.create(this, R.raw.canon);
        mp.setLooping(false);
        mp.setVolume(0,7);
        mp.start();

        r = new Random();

        jawab1 = (Button) findViewById (R.id.jawab1);
        jawab2 = (Button) findViewById (R.id.jawab2);
        jawab3 = (Button) findViewById (R.id.jawab3);
        jawab4 = (Button) findViewById (R.id.jawab4);

        nilai = (TextView) findViewById (R.id.nilai);
        pertanyaan = (TextView) findViewById (R.id.pertanyaan);

        nilai.setText("Nilai : " + mNilai);

        updatePertanyaan(r.nextInt(mPertanyaanLength));

        jawab1.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View view)  {
                if(jawab1.getText() == mJawab) {
                    mNilai++;
                    nilai.setText("Nilai : " + mNilai);
                    updatePertanyaan(r.nextInt(mPertanyaanLength));
                } else {
                    Tamat();
                }
            }
        });

        jawab2.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View view)  {
                if(jawab2.getText() == mJawab) {
                    mNilai++;
                    nilai.setText("Nilai : " + mNilai);
                    updatePertanyaan(r.nextInt(mPertanyaanLength));
                } else {
                    Tamat();
                }
            }
        });

        jawab3.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View view)  {
                if(jawab3.getText() == mJawab) {
                    mNilai++;
                    nilai.setText("Nilai : " + mNilai);
                    updatePertanyaan(r.nextInt(mPertanyaanLength));
                } else {
                    Tamat();
                }
            }
        });

        jawab4.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View view)  {
                if(jawab4.getText() == mJawab) {
                    mNilai++;
                    nilai.setText("Nilai : " + mNilai);
                    updatePertanyaan(r.nextInt(mPertanyaanLength));
                } else {
                    Tamat();
                }
            }
        });
    }

    private void updatePertanyaan(int num) {
        pertanyaan.setText(mPertanyaan.getPertanyaan(num));
        jawab1.setText(mPertanyaan.getPilih1(num));
        jawab2.setText(mPertanyaan.getPilih2(num));
        jawab3.setText(mPertanyaan.getPilih3(num));
        jawab4.setText(mPertanyaan.getPilih4(num));

        mJawab = mPertanyaan.getJawabannya(num);

    }

    private void Tamat(){


        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
        alertDialogBuilder
                .setMessage("Kerja Bagus!!! Nilaimu " + mNilai + " Poin...")
                .setCancelable(false)
                .setPositiveButton("ULANGI LAGI",
                //i want to replay the audio when pressing the "ULANGI LAGI" dialog btn
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which)
                            {
                                startActivity(new Intent(getApplicationContext(), MainActivity.class));
                            }
                        })
                .setNegativeButton("KELUAR",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();

                            }
                        });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}




1 个答案:

答案 0 :(得分:0)

只需将您的MediaPlayer对象设为全局变量(在OnCreate之外,就像您的按钮一样),然后在对话框正面按钮点击监听器上再次启动音乐。

MediaPlayer mp;

在您的正面按钮监听器上,

.setPositiveButton("ULANGI LAGI", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which){
       // your code
       if (mp != null) {
         mp.start();
       }                     
   }
})