Java Android MediaPlayer最终定稿,未发布
我正在创建一个测验应用程序。在回答了10到12个问题后,Medvoice不会在回答了正确或错误的问题后提交。我从MediaPlayer(-19,0)和设置缓存中获得此错误,名称=声音效果已启用,值=0。请引导我克服这个错误。谢谢Java Android MediaPlayer最终定稿,未发布,java,android,audio,Java,Android,Audio,我正在创建一个测验应用程序。在回答了10到12个问题后,Medvoice不会在回答了正确或错误的问题后提交。我从MediaPlayer(-19,0)和设置缓存中获得此错误,名称=声音效果已启用,值=0。请引导我克服这个错误。谢谢 我正在使用这个代码。声音可以播放8到10次,然后声音消失。如果我们等一秒钟,过了一段时间再点击按钮,声音就会开始播放。而且过了一段时间它就崩溃了 package com.kids_fun; import java.util.Arrays; import java.u
我正在使用这个代码。声音可以播放8到10次,然后声音消失。如果我们等一秒钟,过了一段时间再点击按钮,声音就会开始播放。而且过了一段时间它就崩溃了
package com.kids_fun;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class HardAddition extends Activity {
RadioGroup options;
RadioButton answer1,answer2, answer3;
TextView Num1,Num2,Counter, Answer,check_answer1;
int Result, z, Score=00;
final Random rand= new Random();
CountDownTimer timer1;
Button Submit,Next;
ImageView Image;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hard__addition);
// Getting RadioButtons, TextViews
options=(RadioGroup)findViewById(R.id.Hard_Add_RdGrp);
answer1=(RadioButton)findViewById(R.id.Hard_Add_answer1);
answer2=(RadioButton)findViewById(R.id.Hard_Add_Answer2);
answer3=(RadioButton)findViewById(R.id.Hard_Add_Answer3);
Num1=(TextView)findViewById(R.id.Hard_Add_Num1_ID);
Num2=(TextView)findViewById(R.id.Hard_Add_Num2_ID);
Counter=(TextView)findViewById(R.id.Hard_Additon_Counter);
Answer=(TextView)findViewById(R.id.Hard_Additon_Answer);
check_answer1=(TextView)findViewById(R.id.Hard_Addition_checked_answer);
Image=(ImageView)findViewById(R.id.Hard_Addition_ImageView);
//Setting Timer
timer1 = new CountDownTimer(15000, 1000) {
public void onTick(long millisUntilFinished) {
Counter.setText(""
+ formatTime(millisUntilFinished));
}
private String formatTime(long millis) {
// TODO Auto-generated method stub
String output = "00";
long seconds = millis / 1000;
//long minutes = seconds / 60;
seconds = seconds % 60;
//minutes = minutes % 60;
String secondsD = String.valueOf(seconds);
//String minutesD = String.valueOf(minutes);
if (seconds < 10)
{ secondsD = "0" + seconds;
}
//if (minutes < 10)
// minutesD = "0" + minutes;
output =secondsD;
return output;
}
public void onFinish() {
Counter.setText("");
//Counter_Dialog();
CustomDialogClass cdd=new CustomDialogClass(HardAddition.this);
cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
cdd.show();
}
};
//Calling Method to generate Numbers Randomly
getRandom();
// Starting Timer
timer1.start();
options.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
check_answer();
}
});
// Submit Button to check the answer
Submit=(Button)findViewById(R.id.Hrd_AddOK_Btn_ID);;
Submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MediaPlayer mp=MediaPlayer.create(HardAddition.this, R.raw.tick);
mp.start();
timer1.cancel();
Submit.setVisibility(View.INVISIBLE);
//Calling Method which will check the answer defined below
isCorrect();
}
});
}
//Method to Generate Numbers Randomly
public void getRandom()
{
int FirstNum= rand.nextInt((1000-500)+1)+500;
Num1.setText(String.valueOf(FirstNum));
int SecondNum= rand.nextInt((5000-1000)+1)+1000;
Num2.setText(String.valueOf(SecondNum));
int x= Integer.parseInt(Num1.getText().toString());
int y= Integer.parseInt(Num2.getText().toString());
Result=x+y;
answer2.setText(String.valueOf(Result));
int option_value1;
do{
option_value1 =rand.nextInt((2000-500)+1)+500;
}
while(option_value1==Result);
answer1.setText(String.valueOf(option_value1));
int option_value2;
do{
option_value2 =rand.nextInt((3000-1500)+1)+1500;
}
while(option_value2==Result);
answer3.setText(String.valueOf(option_value2));
int a=Integer.parseInt(answer1.getText().toString());
int b=Integer.parseInt(answer2.getText().toString());
int c=Integer.parseInt(answer3.getText().toString());
Integer[] options = {a, b , c };
Collections.shuffle(Arrays.asList(options));
// assign the value choice
a = options[0];
b = options[1];
c = options[2];
answer1.setText(String.valueOf(a));
answer2.setText(String.valueOf(b));
answer3.setText(String.valueOf(c));
}
// Method to check the answer
public void isCorrect()
{
if(answer1.isChecked()==false && answer2.isChecked()==false && answer3.isChecked()==false)
{
Toast.makeText(getApplicationContext(), "Please Select One Option", Toast.LENGTH_SHORT).show();
Submit.setVisibility(View.VISIBLE);
}
else if(answer1.isChecked())
{
Image.setVisibility(View.VISIBLE);
z=Integer.parseInt(answer1.getText().toString());
if(z==Result)
{
//Displaying Image
Image.setBackgroundResource(R.drawable.a);
//Playing Sound
mp=MediaPlayer.create(HardAddition.this, R.raw.wow);
mp.start();
//Increamenting Score
increamentscore();
//Generating New Question
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Questions();
answer1.setChecked(false);
answer2.setChecked(false);
answer3.setChecked(false);
mp.stop();
mp.release();
}
}, 3500);
}
else{
//Displaying Image
Image.setBackgroundResource(R.drawable.r);
//Playing Sound
mp=MediaPlayer.create(HardAddition.this, R.raw.okay);
mp.start();
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Questions();
answer1.setChecked(false);
answer2.setChecked(false);
answer3.setChecked(false);
mp.stop();
mp.release();
}
}, 3500);
}
}
else if(answer2.isChecked())
{ Image.setVisibility(View.VISIBLE);
z=Integer.parseInt(answer2.getText().toString());
if(z==Result)
{
//Displaying Image
Image.setBackgroundResource(R.drawable.b);
//Playing Sound
mp=MediaPlayer.create(HardAddition.this, R.raw.wow);
mp.start();
Increamenting Score
increamentscore();
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Questions();
answer1.setChecked(false);
answer2.setChecked(false);
answer3.setChecked(false);
mp.stop();
mp.release();
}
}, 3500);
}
else{
//Displaying Image
Image.setBackgroundResource(R.drawable.r);
//Playing Sound
mp=MediaPlayer.create(HardAddition.this, R.raw.okay);
mp.start();
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Questions();
answer1.setChecked(false);
answer2.setChecked(false);
answer3.setChecked(false);
mp.stop();
mp.release();
}
}, 3500);
}
}
else if(answer3.isChecked())
{
Image.setVisibility(View.VISIBLE);
z=Integer.parseInt(answer3.getText().toString());
if(z==Result)
{
//Displaying Image
Image.setBackgroundResource(R.drawable.a);
// Playing Sound
mp=MediaPlayer.create(HardAddition.this, R.raw.wow);
mp.start();
increamentscore();
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Questions();
answer1.setChecked(false);
answer2.setChecked(false);
answer3.setChecked(false);
mp.stop();
mp.release();
}
}, 3500);
}
else{
//Displaying Image
Image.setBackgroundResource(R.drawable.r);
//Playing Sound
mp=MediaPlayer.create(HardAddition.this, R.raw.okay);
mp.start();
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Questions();
answer1.setChecked(false);
answer2.setChecked(false);
answer3.setChecked(false);
mp.stop();
mp.release();
}
}, 3500);
}
}
}
public void Counter_Dialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Time is Up !!!")
.setMessage(
"Your Time is Up, Press Next to Move to the Next Question")
// .setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Next",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
mp=MediaPlayer.create(getApplicationContext(), R.raw.tick);
mp.start();
Questions();
//timer1.start();
answer1.setChecked(false);
answer2.setChecked(false);
answer3.setChecked(false);
}
})
// .setNegativeButton("No", null)
.show();
}
// Increamenting Score
public int increamentscore() {
if(Score<99)
{
Score = Score + 1;
Answer.setText(String.valueOf(Score));
}
else{
Score=0;
}
return Score;
}
// Generating New Question
public void Questions() {
Image.setVisibility(View.GONE);
getRandom();
timer1.start();
options.clearCheck();
check_answer1.setText(String.valueOf(""));
Submit.setVisibility(View.VISIBLE);
}
// Checking the value of Checked Answer by the User
public void check_answer()
{ int y;
switch(options.getCheckedRadioButtonId())
{
case R.id.Hard_Add_answer1:
y=Integer.parseInt(answer1.getText().toString());
check_answer1.setText(String.valueOf(y));
break;
case R.id.Hard_Add_Answer2:
y=Integer.parseInt(answer2.getText().toString());
check_answer1.setText(String.valueOf(y));
break;
case R.id.Hard_Add_Answer3:
y=Integer.parseInt(answer3.getText().toString());
check_answer1.setText(String.valueOf(y));
break;
}
}
// Dialog which will display on When Time is UP
public class CustomDialogClass extends Dialog implementsandroid.view.View.OnClickListener
{
public Activity c;
public Dialog d;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_box );
Next = (Button) findViewById(R.id.dialog_Next);
Next.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dialog_Next:
mp=MediaPlayer.create(getApplicationContext(), R.raw.tick);
mp.start();
Questions();
break;
default:
break;
}
dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hard__addition, menu);
return true;
}
}
package com.kids\u fun;
导入java.util.array;
导入java.util.Collections;
导入java.util.Random;
导入android.media.MediaPlayer;
导入android.os.Bundle;
导入android.os.CountDownTimer;
导入android.os.Handler;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.Dialog;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.graphics.Color;
导入android.graphics.drawable.ColorDrawable;
导入android.view.Menu;
导入android.view.view;
导入android.view.Window;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.RadioButton;
导入android.widget.RadioGroup;
导入android.widget.RadioGroup.OnCheckedChangeListener;
导入android.widget.TextView;
导入android.widget.Toast;
公共类硬件添加扩展了活动{
组选项;
无线按钮应答器1、应答器2、应答器3;
text查看Num1、Num2、计数器、答案、检查答案1;
int结果,z,得分=00;
最终随机数=新随机数();
倒计时计时器1;
按钮提交,下一步;
图像视图图像;
MediaPlayer mp;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u hard\u addition);
//获取单选按钮、文本视图
选项=(放射组)findViewById(R.id.Hard\u Add\u RdGrp);
answer1=(单选按钮)findViewById(R.id.Hard\u Add\u answer1);
answer2=(单选按钮)findViewById(R.id.Hard\u Add\u answer2);
answer3=(单选按钮)findViewById(R.id.Hard\u Add\u answer3);
Num1=(TextView)findViewById(R.id.Hard\u Add\u Num1\u id);
Num2=(TextView)findViewById(R.id.Hard\u Add\u Num2\u id);
计数器=(TextView)findViewById(R.id.Hard\u Additon\u计数器);
答案=(TextView)findViewById(R.id.Hard\u Additon\u答案);
check_answer1=(TextView)findViewById(R.id.Hard_Addition_checked_answer);
Image=(ImageView)findViewById(R.id.Hard\u Addition\u ImageView);
//设定计时器
计时器1=新的倒计时计时器(15000,1000){
公共void onTick(长毫秒未完成){
Counter.setText(“”)
+格式化时间(毫秒级);
}
专用字符串格式化时间(长毫秒){
//TODO自动生成的方法存根
字符串输出=“00”;
长秒=毫秒/1000;
//长分钟=秒/60;
秒=秒%60;
//分钟=分钟%60;
String secondsD=String.valueOf(秒);
//String minutesD=String.valueOf(分钟);
如果(秒<10)
{secondsD=“0”+秒;
}
//如果(分钟<10)
//分钟数d=“0”+分钟;
输出=秒SD;
返回输出;
}
公共无效onFinish(){
Counter.setText(“”);
//计数器_对话框();
CustomDialogClass cdd=新的CustomDialogClass(HardAddition.this);
cdd.getWindow().setBackgroundDrawable(新的ColorDrawable(Color.TRANSPARENT));
cdd.show();
}
};
//随机生成数字的调用方法
getRandom();
//启动计时器
timer1.start();
options.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(放射组arg0,int arg1){
//TODO自动生成的方法存根
检查答案();
}
});
//提交按钮检查答案
提交=(按钮)findViewById(R.id.Hrd_AddOK_Btn_id);;
Submit.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
MediaPlayer mp=MediaPlayer.create(HardAddition.this,R.raw.tick);
mp.start();
timer1.cancel();
Submit.setVisibility(View.INVISIBLE);
//调用方法,该方法将检查下面定义的答案
isCorrect();
}
});
}
//随机生成数字的方法
public void getRandom()
{
int FirstNum=rand.nextInt((1000-500)+1)+500;
Num1.setText(String.valueOf(FirstNum));
int SecondNum=rand.nextInt((5000-1000)+1)+1000;
Num2.setText(String.valueOf(SecondNum));
intx=Integer.parseInt(Num1.getText().toString());
int y=Integer.parseInt(Num2.getText().toString());
结果=x+y;
answer2.setText(String.valueOf(Result));
int选项_值1;
做{
选项_值1=兰特下一次((2000-500)+1)+500;
}
while(选项_值1==结果);
answer1.setText(String.valueOf(option_value1));
int选项_值2;
做{
选项_值2=rand.nextInt((3000-1500)+1)+1500;
}
while(选项_值2==结果);
answer3.setText(String.valueOf(option_value2));
int a=Integer.parseInt(answer1.getText().toString());
intb=Integer.parseInt(answer2.getText().toString());
int c=Integer.parseInt(answer3.getText().toString());
整数[]选项={a,b,c};
Collections.shuffle(Arrays.asList(options));
//指定值选项
a=选项[0];
b=选项[1];
c=选项[2];
回答1.setText(String.valueOf(a));
回答2.setText(String.valueOf(b));
答案3.setText(String.valueOf(c));
}
//方法检查答案
公共空间是正确的
{
if(answer1.isChecked()==false&&answer2.isChecked()==false&&answer3.isChecked()==false)
{
烤面包
MediaPlayer mp = new MediaPlayer();
mp.prepare();
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});