gpt4 book ai didi

android - 单击对话框中的肯定按钮时如何从头开始播放音频背景?

转载 作者:行者123 更新时间:2023-12-03 01:42:20 24 4
gpt4 key购买 nike

我正在创建一个简单的多项选择测验应用程序(从教程开始,我想给它提供背景音乐),我想问的是,当我在回答时单击.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();
}
}

最佳答案

只需使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();
}
}
})

关于android - 单击对话框中的肯定按钮时如何从头开始播放音频背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46246766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com