Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Wav file doesn't play

I'm trying to make a script that plays a sound file. My wav file is in the same directory as my java file but for some reason my file doesn't actually play? I'm not getting any errors so I'm not sure as to why this isn't working?

Any ideas are welcome!

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

public class Twice {
    public static void main (String[] args) {
        try {
        Clip clip = AudioSystem.getClip();
        clip.open(AudioSystem.getAudioInputStream(new File(args[0])));
        clip.start();
        System.out.println("hey boi...");
        System.out.println(args[0]);
        }
        catch (Exception e) {
            System.out.println("uh oh");
        }
    }
}

1 Answer

args[0] is java program name, so you should not use args[0], instead use args[1]

        clip.open(AudioSystem.getAudioInputStream(new File(args[1])));

i hope it will work :)