Alex Vathana
Alex Vathana

Reputation: 1

Could not play a clip sound

I'm new to programming and I couldnt figure out how to play a sound clip. The code run smoothly but there is no sound coming out from intellij.

Here's my code

package ProjectWumpus;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
public class testClass {

    public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
        File file = new File("C:\\Users\\Correct_Answer_Sound_Effect.wav");
        AudioInputStream audiostream = AudioSystem.getAudioInputStream(file);
        Clip clip = AudioSystem.getClip();
        clip.open(audiostream);
        clip.start();

My audio from my pc is working fine.

Upvotes: 0

Views: 124

Answers (1)

Phil Freihofner
Phil Freihofner

Reputation: 7910

In the comments, both respondents pointed out that the program closes before the Clip has a chance to play. Clips immediately return control back to the main thread. The code which executes the playback is on a daemon thread. Daemon threads will not hold open a program that is ready to close.

FWIW, Here is perhaps a better way to test. In the following code a simple GUI: a button that plays the sound. This is a more typical of how clips are used.

public class TestClip {
        
    public static void main(String[] args) {
 
        EventQueue.invokeLater(new Runnable(){
        
            public void run()
            {   
                DemoFrame frame = new DemoFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }   
}
    
class DemoFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private Clip clip;
    
    public DemoFrame() {
        
        setSize(300, 100);          
        JPanel panel = new JPanel();
        JButton button = new JButton("Play clip");
        button.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                clip.setFramePosition(0);
                clip.start();
            }
        });
        
        panel.add(button);
        add(panel);

        // Set up the Clip
        URL url = this.getClass().getResource("mySound.wav");
        try {
            AudioInputStream ais = AudioSystem.getAudioInputStream(url);
            clip = AudioSystem.getClip();
            clip.open(ais);
            
        } catch ( LineUnavailableException e) {
            e.printStackTrace();
        } catch (UnsupportedAudioFileException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

I recommend using URL over File for the getAudioInputStream method. In this case, it's assumed the audio resource is in the same directory as the class that is calling it. A URL has the benefit of working when the class is packaged in a jar (unlike File).

Upvotes: 0

Related Questions