Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda