0

I'm developing an Android application that is based on a timer that every second must play a sound that you selected before.

Here is the problem:

  • No objects can run Media Player in a Thread or a Runnable
  • I can not call methods that reproduce the sound
  • I can not run it on a timer task

Can you think of anything?

Thanks

thread:

public void run(){
while(!detenido) //Bolean for stop the thread {

        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;
            }
          } 
            }

Metod in the service to start a play sound:

public void intento1(){

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

}
CristianCV
  • 342
  • 1
  • 5
  • 17

1 Answers1

1

You can run a MediaPlayer in an extra Service.

Check this out for information about Service.
Also, this link might be useful to handle the MediaPlayer with a Service.

You could then call the functions of your Service to handle the MediaPlayer.

MalaKa
  • 3,734
  • 2
  • 18
  • 31
  • You mean from the thread to call the service method that reproduces the sound? – CristianCV Mar 19 '13 at 17:21
  • Yes, you might call a Thread that waits for a second and then calls a function. This might not be the best version to handle the problem, but it should work. – MalaKa Mar 19 '13 at 17:27
  • i do it, but no works, see the code in the description ( i already edit the post) – CristianCV Mar 19 '13 at 19:50
  • you first have to start the service out of your activity. have a look [here](http://www.edumobile.org/android/android-programming-tutorials/use-of-services/) to see how to handle services – MalaKa Mar 20 '13 at 21:00