0

我正在开发一个基于计时器的 Android 应用程序,该计时器每秒必须播放您之前选择的声音。

这是问题所在:

  • 没有对象可以Media Player在 aThread或 a中运行Runnable
  • 我不能调用重现声音的方法
  • 我无法在计时器任务上运行它

你能想到什么吗?

谢谢

线:

public void run(){
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;
            }
          } 
            }

在服务中启动播放声音的方法:

公共无效意图1(){

    mp= MediaPlayer.create(this, R.raw.censura);
    mp.start();

}
4

1 回答 1

1

您可以运行一个MediaPlayer额外的Service.

查看信息以获取有关Service.
此外,此链接对于处理MediaPlayer带有Service.

然后,您可以调用您的函数Service来处理MediaPlayer.

于 2013-03-19T14:32:38.010 回答