Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
How do I play an audio in a loop in Java?

I want to play just an audio in a loop everytime this has finished. I have to say that is just backgroud music and it's running in a thread. Here's the code:

package Generator;

import javazoom.jl.player.Player;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javazoom.jl.decoder.JavaLayerException;

public class Reproductor {
       
    Player reproductor;
        
    public void reproducir() throws FileNotFoundException, JavaLayerException {
        reproductor = new Player(new FileInputStream("C:\\Users\\Leonardo\\Desktop\\config generator\\Recursos\\Triage at dawn.mp3"));    
        new Thread  () {
           @Override
           public void run() {
               try {   
                    reproductor.play();        //This just play the audio for the first time.
               } catch (JavaLayerException ex) {
                   
               }
           } 
        }.start();
    }    
        
    public void parar() {
        if(reproductor != null) {
            reproductor.close();
        }
    }
over 4 years ago · Santiago Trujillo
4 Respostas
Responde à pergunta

0

i have a solution for you. make a new java file and name it anything. Then do this code:


class Main{
 public static void main(String[] args)
 {
    int length = 10000000000, numberOfTimes = 5;
    Reproducer reproducer = new Reproducer();
    for(int j =0;j < numberOfTimes;i++){
    reproducer.reproducir();
    for(int i =0;i < length;i++){}
    }
 }
}
over 4 years ago · Santiago Trujillo Relatório

0

I found the source code: https://github.com/umjammer/jlayer/tree/master/src/main/java/javazoom/jl/player/advanced

You should use an AdvancedPlayer instead of a Player. It allows you to register a PlaybackListener which will receive a PlaybackEvent when the music stops.

This code should do the trick:

package Generator;

import javazoom.jl.player.Player;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javazoom.jl.decoder.JavaLayerException;

public class Reproductor {
       
    Player reproductor;
        
    public void reproducir() throws FileNotFoundException, JavaLayerException {
        reproductor = new Player(new FileInputStream("C:\\Users\\Leonardo\\Desktop\\config generator\\Recursos\\Triage at dawn.mp3"));    

       PlaybackListener myListener = new PlaybackListener() {
        public void playbackStarted(PlaybackEvent evt){
            // Do nothing
        }
        public void playbackFinished(PlaybackEvent evt){
            // Restart
            reproductor.play(); 
        }
        };
        reproductor.setPlayBackListener( myListener  );
        new Thread  () {
           @Override
           public void run() {
               try {
                    reproductor.play();        //This just play the audio for the first time.
               } catch (JavaLayerException ex) {
                   
               }
           } 
        }.start();
    }    
        
    public void parar() {
        if(reproductor != null) {
            reproductor.close();
        }
    }
over 4 years ago · Santiago Trujillo Relatório

0

I see that you are using Javazoom.

Looking at documentation for this library that I found on github, there is a class that should work better: AdvancedPlayer in combination with a PlaybackListener.

If this is similar to most listeners in Java, it can be set to sense when the audio has finished, and on that event call another play. It's been a long time since I worked with this library though, and all I did was use it for converting ogg/vorbis files to PCM, so I can't help you with the specific code needed.

If you can use wav files, it would be a lot easier to use Java's Clip class and it's loop() method. Conversions of mp3 files to wavs can be done with a tool such as Audacity, if your application is such where the assets can be prepped beforehand.

If you know exactly what code you are going to play, another possibility would be to determine the length which may be present in some header info, or again by opening it in Audacity, and store the duration info in your project. Then, for the playback code, add a Thread.sleep() for the duration before looping. This probably won't be very reliable.

over 4 years ago · Santiago Trujillo Relatório

0

While I was trying to solve some issues, i found an easier way to do it. Check this link for the solution. My code ended like this :


package Generator;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

public class Reproductor extends Thread{
       
    private String ruta;
    private boolean bucle;
    private Player reproductor;
        
    public Reproductor(String ruta, boolean bucle) {
        this.ruta = ruta;
        this.bucle = bucle;
    }    
        
    public void run() {
        try {
            do{
                FileInputStream buff = new FileInputStream(ruta);
                    reproductor = new Player(buff);
                    reproductor.play();
                try {
                    reproductor.play();
                } catch (JavaLayerException ex) {}
            }while(bucle);
        }   catch(FileNotFoundException | JavaLayerException ioe) {}  
    }
            
    public void parar() {
        bucle = false;
        reproductor.close();
        this.interrupt();
    }  
    
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda