Home Can't play sound in java using clip
Reply: 2

Can't play sound in java using clip

Jovan Dukic
1#
Jovan Dukic Published in 2018-02-11 19:08:46Z

I want to play some sound in java using this code bellow. That sound is in WAV format so I think that this code should work fine, but instead of playing sound it simply does nothing. There is no even error on my console. So can someone help me to make this code play sound. (this sound is included inside one package in my src file)

 public static void main(String[] args) {

 new Thread(new Runnable() {

        @Override
        public void run() {

            try {

                Clip clip = AudioSystem.getClip();

                File file = new File("C:\\Users\\Jovan\\Desktop\\song.wav");

                AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);

                clip.open(inputStream);

                clip.start();

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });
 }
gpasch
2#
gpasch Reply to 2018-02-11 20:16:47Z

You have to wait for the clip to play and end. You can also create some Listener but that's more complicated. When the clip finishes playing (isActive() is false) you end.

public class P {

 public static void main(String[] args) {

 new Thread(new Runnable() {

        @Override
        public void run() {

            try {

            System.out.println("started");

                Clip clip = AudioSystem.getClip();

                File file = new File(".......................wav");

                AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);

                clip.open(inputStream);

                clip.start();

                while(clip.isOpen()) {
                  try { Thread.sleep(2000); } catch(InterruptedException ie) {}
                  if(!clip.isActive()) break;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }).start();
 }

 }
moondaisy
3#
moondaisy Reply to 2018-02-11 20:38:38Z

You have just started this thread, so create an object:

import java.io.*;
import  javax.sound.sampled.*;

class x {
    public static void main(String[] args) {

        Thread b= new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Clip clip = AudioSystem.getClip();
                    File file = new File("G:/skit/msg.wav");
                    AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);
                    clip.open(inputStream);
                    clip.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        b.start();
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303351 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO