2013-03-19 26 views
0

我正在開發基於計時器的Android應用程序,每秒必須播放您之前選擇的聲音。線程和媒體播放器的問題,定時器的聲音

這裏的問題是:

  • 沒有對象可以在Thread運行Media PlayerRunnable
  • 我不能調用播放聲音的方法
  • 我不能在計時器任務運行

你能想到什麼嗎?

感謝

線:(!detenido)

公共無效的run(){
而// Bolean爲停止線程 {

 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(); 

} 
+1

你能分享一些你已經試過的代碼嗎? – Ganesh 2013-03-19 14:36:04

+0

我已經做到了 – CristianCV 2013-03-19 19:48:38

回答

1

您可以運行一個MediaPlayer在額外Service

檢查this瞭解有關Service的信息。
另外,this link可能有用於處理MediaPlayerService

然後,您可以調用Service的函數來處理MediaPlayer

+0

你是指從線程調用再現聲音的服務方法? – CristianCV 2013-03-19 17:21:33

+0

是的,你可能會調用一個等待一秒的線程,然後調用一個函數。這可能不是處理該問題的最佳版本,但它應該可以工作。 – MalaKa 2013-03-19 17:27:22

+0

我這樣做,但沒有作品,請參閱說明中的代碼(我已經編輯帖子) – CristianCV 2013-03-19 19:50:12