我是学生为我的学校做项目。当我的应用程序显示警报时,我正在尝试播放声音,但一段时间后它会崩溃。 我的程序是每隔一段时间从Web服务器获取某些数据,以便更新并根据配置的异常显示警报消息
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button speed;
private TextView result;
private TextView sSpeed;
Timer timer;
TimerTask timerTask;
MediaPlayer mpAlarm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.result);
sSpeed = (TextView) findViewById(R.id.sSpeed);
speed = (Button) findViewById(R.id.get_button);
speed.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startTimer();
}
});
mpAlarm = MediaPlayer.create(this, R.raw.alarm);
View speedButton = this.findViewById(R.id.get_button);
speedButton.setOnClickListener(this);
View aboutButton = this.findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = this.findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.get_button:
startTimer();
getWebsite();
break;
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit_button:
stopTimer();
finish();
break;
}
}
private void getWebsite(){
new Thread(new Runnable() {
@Override
public void run() {
final StringBuilder builder = new StringBuilder();
try{
Document doc = Jsoup.connect("http://10.0.2.2:8080/Start_Stop_buttons_UTF8.html").get();
// Elements element = doc.getElementsByTag("p");
Elements element = doc.select("p");
//String title = doc.title();
builder.append(title).append("\n");
for (Element tag : element){
builder.append("\n\n").append(tag.text());
}
/*for(Element speed: element){
element.toString();
}*/
}catch(IOException e){
//e.printStackTrace();
builder.append("Error : ").append(e.getMessage()).append("\n");
}
runOnUiThread(new Runnable() {
@Override
public void run() {
String a = builder.toString(); // parse data from html into new string
a = a.substring(a.indexOf(":")+1, a.indexOf("Control")).trim();//trim string content
String b = builder.toString();
b = b.substring(11,b.indexOf(":")+1).trim();
// Toast.makeText(MainActivity.this, a.subSequence(0,a.length()-1), Toast.LENGTH_LONG).show();
double speed = Double.parseDouble(a);//convert string into double
if (speed<1000){ //acceptable rate is 1000-1500
mpAlarm.start();
Toast.makeText(MainActivity.this,"Alert!\nFan is too slow!\nPlease contact admin and fix issue.", Toast.LENGTH_SHORT).show();
Log.i("HTML text","too slow");
}
else if((speed> 1500)){
mpAlarm.start();
Toast.makeText(MainActivity.this,"Alert!\nFan is too fast!\nPlease contact admin and fix issue.", Toast.LENGTH_SHORT).show();
Log.i("HTML text","too fast!");
}
else{
mpAlarm.release();
}
result.setText(a);
sSpeed.setText(b);
}
});
}
}).start();
}
protected void onResume(){
super.onResume();
}
public void onDestroy() {
super.onDestroy();
mpAlarm.release();
}
public void startTimer(){
if(timer != null) return;
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
getWebsite();
}
};
timer.schedule(timerTask,2000,3000);
}
public void stopTimer(){
if(timer != null){
timer.cancel();
timer.purge();
timer = null;
}
}
}
我尝试在onCreate中移除mpAlarm.release()并且它可以工作,但是当在测试过程中从警报显示转换回正常时声音与显示器不同步,即即使应该播放也会持续播放一段时间改变后没有警觉。
else if((speed> 1500)){
mpAlarm.start();
Toast.makeText(MainActivity.this,"Alert!\nFan is too fast!\nPlease contact admin and fix issue.", Toast.LENGTH_SHORT).show();
Log.i("HTML text","too fast!");
}
result.setText(a);
sSpeed.setText(b);
错误日志:
08-08 15:26:59.740 6219-6219/com.sp.i40project W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xaccccb20)
08-08 15:26:59.740 6219-6219/com.sp.i40project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sp.i40project, PID: 6219
java.lang.IllegalStateException
at android.media.MediaPlayer._start(Native Method)
at android.media.MediaPlayer.start(MediaPlayer.java:1064)
at com.sp.i40project.MainActivity$2$1.run(MainActivity.java:115)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
如何解决此问题或是否有更好的方法来实现我想要的功能?
P.S。我只是通过更改我的Web服务器中的值来测试我的程序,以显示正常数据和警报时。