Help with sounds in LibGdx

0

I am using this library for games, and wanted to know how I could do to put sounds to play during the game and with pre-set time?

    
asked by anonymous 07.04.2017 / 03:59

2 answers

2

When I'm developing audio apps for android it is inevitable to use threads, threads allow to run functions concurrently, an example is the use of Runnable , to run asynchronously is by far my favorite, you can still use a Handle to perform scheduled (scheduled) tasks in the background.

A simple example of how to schedule an audio to play every 500 milliseconds using threads would be:

Sound sound = Gdx.audio.newSound(Gdx.files.internal("ederwander.mp3"));
final Handler ederhandler = new Handler();


//executa pela primeira vez e chama a thread para rodar em background
public void TocarAudio() {
        ederHandler.postDelayed(agendaAudio, 500);
}   

/**
* Executa thread em Background
* */

private Runnable agendaAudio = new Runnable() {
           public void run() {

              //Toca audio
              sound.play(1.0f);

              // thread será executado novamente após 500 mili segundos
              ederHandler.postDelayed(this, 500);
           }
};
    
08.04.2017 / 03:13
0

Try this it times 2 and activates something, you can activate a sound and stop, if you want more than 2 seconds just change the 2000 if you want it to happen only once every time you improve the code by adding a boolean value:

import com.badlogic.gdx.utils.TimeUtils;

public class MeuScreen implements Screen{

public MeuScreen() {
        super();
        startTime = TimeUtils.millis();
    }



public void render(float delta) {
        //Isso vai fazer algo acontecer depois de 2 segundos 
        if (TimeUtils.millis()>(startTime+2000)){
            Gdx.app.log("Algo aconteceu", "");
        }

}
    
11.05.2017 / 12:54