【发布时间】:2013-03-19 14:24:39
【问题描述】:
我正在开发一个基于计时器的 Android 应用程序,该计时器每秒必须播放您之前选择的声音。
问题来了:
- 没有对象可以在
Thread或Runnable中运行Media Player - 我无法调用再现声音的方法
- 我无法在计时器任务上运行它
你能想到什么吗?
谢谢
线程:
公共无效运行(){
while(!detenido) //停止线程的布尔值
{
try {
Servicio servicios = new Servicio();
switch(segundosesperar){ //Int to select the ms to slep
case 0:
this.sleep(1500);
servicios.intento1(); //the metod with " mp.start()"
dormir = 1500;
break;
case 1:
this.sleep(1600);
servicios.intento1();
dormir = 1600;
break;
case 2:
this.sleep(1700);
servicios.intento1();
dormir = 1700;
break;
case 3:
this.sleep(1800);
servicios.intento1();
dormir = 1800;
break;
case 4:
this.sleep(1900);
servicios.intento1();
dormir = 1900;
break;
case 5:
this.sleep(2000);
servicios.intento1();
dormir = 2000;
break;
default:
this.sleep(1750);
servicios.intento1();
dormir = 1750;
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
piedras =piedras+1; //Counter cronometer +1
cron = piedras+"";
//Set the counter in the activity
handler.setHcron(cron);
handler.act();
cronoparaganar = cron;
if ( piedras == piedrasmodo){
tocarsirena = true;
}
}
}
在服务中开始播放声音的方法:
public void intento1(){
mp= MediaPlayer.create(this, R.raw.censura);
mp.start();
}
【问题讨论】:
-
你能分享一些你已经尝试过的代码吗?
标签: android multithreading audio media-player timertask