Help with sounds in Libgdx

Asked

Viewed 442 times

0

I’m using this library for games , and wanted to know how I could do to put sounds to play during the game and with preset time?

  • 2

    https://github.com/libgdx/libgdx/wiki/Streaming-music

  • 1

    as such predefined time ?

  • set , for example , in 5 seconds play a song , in another 5 seconds play another

2 answers

2


When I’m developing audio applications for android the use of threads is inevitable, the threads allow to perform functions in a concurrent manner, an example is the use of the Runnable, for asynchronous execution 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 Mili-seconds 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);
           }
};

0

Try this it times 2 seconds 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 each call improve the code by adding a value Boolean :

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", "");
        }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.