【问题标题】:Thread and mediaplayer problems, Timer with sound线程和媒体播放器问题,带声音的计时器
【发布时间】:2013-03-19 14:24:39
【问题描述】:

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

问题来了:

  • 没有对象可以在ThreadRunnable 中运行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


【解决方案1】:

您可以在额外的Service 中运行MediaPlayer

查看this 了解有关Service 的信息。
此外,this link 可能有助于处理带有ServiceMediaPlayer

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

【讨论】:

  • 你的意思是从线程中调用再现声音的服务方法?
  • 是的,您可能会调用一个等待一秒钟然后调用函数的线程。这可能不是解决问题的最佳版本,但它应该可以工作。
  • 我做了,但没有用,请参阅描述中的代码(我已经编辑了帖子)
  • 您首先必须从您的活动中启动服务。看看here看看如何处理服务
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多