How I can check when music has ended - java

I have a JFrame with 2 buttons: Turn On and Turn Off
My problem is when a song has ended, I can't check that it ended and to play it again
How can I check it? Thank you so much
Below is the way that I play sound on Swing
class MP3 {
private Player player;
private String filename;
public MP3(String filename) {
this.filename = filename;
}
public void stop() {
if (player != null)
player.close();
}
public void play() {
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename));
player = new Player(bis);
} catch (FileNotFoundException | JavaLayerException ex) {
System.out.println(ex);
}
new Thread(new Runnable() {
#Override
public void run() {
try {
player.play();
} catch (Exception ex) {
System.out.println(ex);
}
}
}).start();
}
}
And Event when I click buttons
private void btnPlayActionPerformed(java.awt.event.ActionEvent evt) {
sound = new MP3("src/Sound/02 - Cold Pizza.mp3");
sound.play();
btnPlay.setVisible(false);
btnStop.setVisible(true);
}
private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {
sound.stop();
btnStop.setVisible(false);
btnPlay.setVisible(true);
}
sound is a instance of MP3 class in main class

I did it this way. I made the mp3 file a wav file. Then I made two objects
AudioInputStream songReader;
Clip song;
try{
songReader =
AudioSystem.getAudioInputStream(
getClass().getResource("wally.wav"));
song = AudioSystem.getClip();
song.open(songReader);
}catch (Exception e){
System.out.println("won't work");
}
try{
song.stop();
song.setFramePosition(0);
}catch(Exception e){
System.out.println("Error playing your music");
}
try{
song.start();
song.loop(song.LOOP_CONTINUOUSLY);
}catch(Exception e){
System.out.println("Won't work");
}
This makes it play infinitely, but if you need you can stop it at any point with song.stop(). Hopefully, that will work for you.

Related

Run audio files on a .jar

Good evening, please someone could tell me how the audio files are executed in a .jar
I have tried to call them using getClass (). GetResource () but I can not use it since an error appears.
This is my implementation for playing sounds:
public class Sonido extends Thread {
private Clip sonido;
private boolean seguir;
public Sonido(String rutaArchivo){
try {
sonido = AudioSystem.getClip();
sonido.open(AudioSystem.getAudioInputStream(new File(rutaArchivo)));
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
Logger.getLogger(PanelBotones.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
#SuppressWarnings("SleepWhileInLoop")
public void run() {
seguir = true;
do {
sonido.loop(0);
try {
Thread.sleep(1000);
} catch (InterruptedException interruptedException) {
Logger.getLogger(PanelBotones.class.getName()).log(Level.SEVERE, null,
interruptedException);
}
} while (seguir == true && sonido.isActive());
// Se cierra el clip.
sonido.close();
}
public void parar() {
seguir = false;
}
}
And this is the event method where you created the new clip
#Override
public void actionPerformed(ActionEvent e) {
String cadena = e.getActionCommand();
if (cadena.equalsIgnoreCase(ROJO)) {
interfazPrincipal.imagenUno();
sonido = new Sonido("./recursos/sonido/rojo.wav");
Thread cancionFondo;
cancionFondo = new Thread(sonido);
cancionFondo.start();
}
I would be appreciated for your help, thank you.
Try this instead:
sonido.open(AudioSystem.getAudioInputStream(Sonido.class.getClassLoader().getResource(rutaArchivo)));
sonido = new Sonido("my/package/rojo.wav");

Java music not working

I made a java program and wanted a background music so I used this function
public static void playSound(final String url) {
new Thread(new Runnable() {
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem
.getAudioInputStream(getClass().getResourceAsStream("/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
only problem is when I try to run the 27 minute wav file, it speeds it up really fast

AudioClip Issues when trying to play sounds on JFrame

I've been trying to play sound1 and sound2 using AudioClips. This is my code:
import javax.sound.sampled.*;
public class Sound {
private Clip clip;
public static final Sound sound1 = new Sound("src/Sounds/Classic_Horror_2.wav");
public static final Sound sound2 = new Sound("src/Sounds/Classic Horror 3.mp3");
public Sound (String fileName) {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(Sound.class.getResource(fileName));
clip = AudioSystem.getClip();
clip.open(ais);
} catch (Exception e) {
e.printStackTrace();
}
}
public void play() {
try {
if (clip != null) {
new Thread() {
public void run() {
synchronized (clip) {
clip.stop();
clip.setFramePosition(0);
clip.start();
}
}
}.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void stop(){
if(clip == null) return;
clip.stop();
}
public void loop() {
try {
if (clip != null) {
new Thread() {
public void run() {
synchronized (clip) {
clip.stop();
clip.setFramePosition(0);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
}
}.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean isActive(){
return clip.isActive();
}
}
When I try to do Sound.sound1.loop(); or Sound.sound1.play(); I get a null pointer exception. Same goes for sound2. Here are the exact errors messages:
java.lang.NullPointerException
at com.sun.media.sound.StandardMidiFileReader.getSequence(StandardMidiFileReader.java:207)
at javax.sound.midi.MidiSystem.getSequence(MidiSystem.java:841)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:178)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1147)
at Sound.<init>(Sound.java:12)
at Sound.<clinit>(Sound.java:7)
at IntroductionComponent.<init>(IntroductionComponent.java:54)
at IntroductionGUI.main(IntroductionGUI.java:9)
java.lang.NullPointerException
at com.sun.media.sound.StandardMidiFileReader.getSequence(StandardMidiFileReader.java:207)
at javax.sound.midi.MidiSystem.getSequence(MidiSystem.java:841)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:178)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1147)
at Sound.<init>(Sound.java:12)
at Sound.<clinit>(Sound.java:8)
at IntroductionComponent.<init>(IntroductionComponent.java:54)
at IntroductionGUI.main(IntroductionGUI.java:9)
I have repeatedly browsed stackoverflow to look for possible fixes, however it may be that fileName returns null constantly, or that my URL's are somehow incorrect. That said, for all I know it could be anything. Any suggestions on how to fix this?
The inclusion of src in your paths...
public static final Sound sound1 = new Sound("src/Sounds/Classic_Horror_2.wav");
public static final Sound sound2 = new Sound("src/Sounds/Classic Horror 3.mp3");
is your key problem, src won't exist once the application is build and package, you should never refer to src ever.
You should probably be using something more like...
public static final Sound sound1 = new Sound("/Sounds/Classic_Horror_2.wav");
public static final Sound sound2 = new Sound("/Sounds/Classic Horror 3.mp3");
public Sound (String fileName) {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(getClass().getResource(fileName));
clip = AudioSystem.getClip();
clip.open(ais);
} catch (Exception e) {
e.printStackTrace();
}
}
Personally, I'd make Sound require a URL...
public Sound (URL url) {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
clip = AudioSystem.getClip();
clip.open(ais);
} catch (Exception e) {
e.printStackTrace();
}
}
This way there's no confusion over what String means and you can pass a reference from embedded resources, file references or even from the net
Also from memory, clip.start() creates it's own thread, so there's no need to create your own

Stop one thread and start the other?

I am having trouble stopping one thread and starting another one. I have created two threads. One of them is first run from the main method. It then takes user input and if the desired input is provided, thread one (sound) is supposed to stop and start thread two (sound2). From the code below, thread two does start playing after the correct input is provided during thread one. But thread one does not stop. I can hear audio files from both thread one and thread two playing. Please help.
public class crytask {
public static void main(String args[]) throws InterruptedException {
Runnable sound = new sound();
final Thread soundthread = new Thread(sound);
soundthread.start();
int count = 0;
System.out.println("Enter");
Scanner reader = new Scanner(System.in);
int a = reader.nextInt();
if (a==12)
soundthread.interrupt();
System.out.println("this thread is cancelled");
Runnable sound2 = new sound2();
final Thread soundthread2 = new Thread(sound2);
soundthread2.start();
int count2 = 0;
System.out.println("Enter");
Scanner reader2 = new Scanner(System.in);
int b = reader2.nextInt();
if (b==12)
soundthread2.interrupt();
}}
class sound implements Runnable {
#Override
public void run() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("/Users/babe/Desktop/Con1.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
Thread.sleep(clip.getMicrosecondLength() / 1000);
} catch(InterruptedException ex) {
System.out.println("Cancelled 1!");
} catch (Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
}
}
class sound2 implements Runnable {
#Override
public void run() {
try {
AudioInputStream audioInputStream2 = AudioSystem.getAudioInputStream(new File("/Users/babe/Desktop/Con1.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream2);
clip.start();
Thread.sleep(clip.getMicrosecondLength() / 1000);
} catch(InterruptedException ex) {
System.out.println("Cancelled 2!");
} catch (Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
}
}
Try stopping the clip when the thread is interrupted, adding clip.close() to the catch clause that catches the interruptedException, like this:
class sound implements Runnable {
#Override
public void run() {
Clip clip = null; // take the declaration of the clip variable out of the try - catch
try {
AudioInputStream audioInputStream =
AudioSystem.getAudioInputStream(new File("/Users/babe/Desktop/Con1.wav").getAbsoluteFile());
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
Thread.sleep(clip.getMicrosecondLength() / 1000);
} catch(InterruptedException ex) {
clip.stop(); // <--- ADD THIS LINE
System.out.println("Cancelled 1!");
} catch (Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
}
}

Interrupting a thread doesn't work after pushing a (applet) button

I made a simple programm to let java play some .mp3 files.
Using JLayer to get this right.
The problem is that i can't interrupt my player after pushing a button.
my code in actionperformed:
class MyThread extends Thread {
#Override
public void run() {
try {
File file = new File(source);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
try {
Thread.sleep(1);
try {
Player player = new Player(bis);
player.play();
} catch (JavaLayerException ex) {
}
} catch (InterruptedException e) {
System.out.println("Came");
}
} catch (IOException e) {
lab2.setText("error, is the pathway correct?");
}
}
}
MyThread mythread = new MyThread();
if (event.getSource() == input) {
source = input.getText();
source = source.replace("\\", "\\\\");
lab1.setText(source);
}
if (event.getSource() == but1) {
mythread.start();
}
if (event.getSource() == but2) {
but2.setLabel("stop");
mythread.interrupt();
but2.setLabel("stopped");
}
}
}
When I add the mythread.interrupt(); directly behind the interrupt works and my system gives me the "came" output.
But if I use my button called input, it wont work.
Seems that player class doesnt have a stop method, but you can try with AdvancedPlayer:
http://www.javazoom.net/javalayer/docs/docs1.0/javazoom/jl/player/advanced/AdvancedPlayer.html
and the stop() method.

Categories