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();
}
}
CodePudding user response:
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 ){}
}
}
}
CodePudding user response:
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();
}
}
CodePudding user response:
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.