0

I`m trying to put some music or a short clip of a sound to my project. I try some helps from here. It kinda work, but I could not hear anything. It just wrote that its playing.

I found a lot of examples, but nothing is working for me. Maybe you can help me with that AudioStream, AudioPlayer. Many people are using it. How can I use it in my Java Eclipse 4.4.0?

import java.io.File;

import java.io.FileReader;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JFrame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import sun.audio.*;



public class zaciatky {


    public static void main(String[] args) throws LineUnavailableException {
        // TODO Auto-generated method stub
        try {
            AudioInputStream audoAudioInputStream=AudioSystem.getAudioInputStream(new File("D:\\Betka\\Dokumenty\\INF\\JAVA\\Saxik\\Music\\saxik.wav"));
            Clip songik=AudioSystem.getClip(); 
            songik.open(audoAudioInputStream);
            songik.start();




            System.out.println("Song is playing...");
            System.out.println(songik.getFormat());
            if(songik.isActive()) System.out.println("is active");
            songik.stop();
            if(songik.isActive()) System.out.println("is active");
            else System.out.println("not active");

        } catch (UnsupportedAudioFileException e) {
            // TODO Auto-generated catch block
            System.out.println("Error occures with playing the sound.");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Error occures with playing the sound.");
            e.printStackTrace();
        }
    }

}
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • 1
    meta: java and javascript are two different languages. – Mousa Halaseh Aug 30 '18 at 13:02
  • 1
    `Clip.start` just starts the music it does not wait for it to finish. You need to wait for it to finish before calling `Clip.stop` - see for example [this question](https://stackoverflow.com/q/557903/2670892) – greg-449 Aug 30 '18 at 13:27
  • It is there just for information about playing sound. It writes that its on, but I cant hear it. Thats the problem :/ – Betka Hajná Aug 30 '18 at 15:09
  • To expand on greg-449’s point: you are starting playback of your sound and then immediately stopping it. As an experiment (not a permanent solution), try putting a Thread.sleep call before songik.stop(). – VGR Aug 30 '18 at 16:03

0 Answers0