作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个简单的多项选择测验应用程序(从教程开始,我想给它提供背景音乐),我想问的是,当我在回答时单击.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/
我需要用这样的数据构建一个表: ┌────────┬───────────┬────────┐ │ ID │ Name │ Age │ ├────
我是一名优秀的程序员,十分优秀!