2

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();
        }
    }
LeonN
  • 29
  • 3

4 Answers4

2

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();
        }
    }
StephaneM
  • 4,779
  • 1
  • 16
  • 33
  • Thanks, your answer was really useful, however I had to make some changes in the code. I had to redefine the `reproductor` variable in the `playbackFinished` method. I was getting an exception, I think it was because once the sound finished its reproduction, a variable called position was set to `null`, so calling `reproductor` was calling a new content. – LeonN Dec 15 '21 at 15:17
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++){}
    }
 }
}
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.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
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();
    }  
    
}
LeonN
  • 29
  • 3