0

I have been trying to write a basic 'Jeopardy' game in java, and right now I'm trying to add sound to play when the player get's an answer right or wrong. I have tried to add the sound (placing the sound file in the bin folder and using the code below), but when I try to play the file there is no sound. There is no null pointer exception.

public class Overview{

static AudioClip right, wrong;

//start the game
    public static void guiApp(){

    right = Applet.newAudioClip(Jeopardy.class.getResource("correct.wav"));
    wrong = Applet.newAudioClip(Jeopardy.class.getResource("wrong.wav"));

    right.play();

    intro = new Intro();
    intro.start();

    }

    public static void main (String[ ] args)
    {
    javax.swing.SwingUtilities.invokeLater (new Runnable ( )
    {
        public void run ( )
        {
            guiApp();
        }
    }
    );
    }

}

The following is essentially what is happening in the method called:

public class Intro{

    public Intro(){

    }

    public void start(){
        JFrame frame = new JFrame();

        frame.setSize(100, 100);
        frame.setVisible(true);
    }
}
  • JavaFX, maybe this answer could help you http://stackoverflow.com/questions/22438353/java-select-audio-device-and-play-mp3/22484132#22484132 – Marco Acierno Mar 20 '14 at 16:47
  • Look to the [Java Sound info. page](http://stackoverflow.com/tags/javasound/info) for an easy way to play a short `Clip`. Other tips: 1) For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). 2) It might be that the WAV files use an internal encoding that Java does not understand. Can you upload one to a place where we can download it to check? – Andrew Thompson Mar 21 '14 at 04:05
  • @AndrewThompson - I believe my edit is what you were asking for; the method being called simply creates a frame with some components. No other methods are called until one of several buttons are clicked in the created frame. I'm not sure where I could upload a file, unless you have a suggestion. – user3443018 Mar 22 '14 at 21:33
  • *"I'm not sure where I could upload a file, unless you have a suggestion."* Drop box, Google Share.. A MCTaRE needs to be one source file, with imports. – Andrew Thompson Mar 22 '14 at 23:50

1 Answers1

0

This is something that i use to play sound.

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class SoundPlayer extends Thread
{
 private static final int BUFFER_SIZE = 128000;
 private static File soundFile;
 private static AudioInputStream audioStream;
 private static AudioFormat audioFormat;
 private static SourceDataLine sourceLine;

 private String file;

 public static String turn = "data/bell.wav"; //bell sound for black jack when it is your turn (played once each turn)

 /**
  * Plays the sound of the sent file name
  * @param file Audio File's path
  */
 public SoundPlayer(String file)
 {
     super("SoundPlayer");
     this.file = file;
     start();
 }

 public void run()
 {
     String strFilename = file;

     try {
         soundFile = new File(strFilename);
     } catch (Exception e) {
         e.printStackTrace();
         System.exit(1);
     }

     try {
         audioStream = AudioSystem.getAudioInputStream(soundFile);
     } catch (Exception e){
        e.printStackTrace();
        System.exit(1);
     }

     audioFormat = audioStream.getFormat();

     DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
     try {
         sourceLine = (SourceDataLine) AudioSystem.getLine(info);
         sourceLine.open(audioFormat);
     } catch (LineUnavailableException e) {
         e.printStackTrace();
         System.exit(1);
     } catch (Exception e) {
         e.printStackTrace();
         System.exit(1);
     }

     sourceLine.start();

     int nBytesRead = 0;
     byte[] abData = new byte[BUFFER_SIZE];
     while (nBytesRead != -1) {
         try {
             nBytesRead = audioStream.read(abData, 0, abData.length);
         } catch (IOException e) {
             e.printStackTrace();
         }
         if (nBytesRead >= 0) {
             @SuppressWarnings("unused")
             int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
         }
     }
     sourceLine.drain();
     sourceLine.close();
     this.stop();
 }

public static void main(String[] args)
{
}

}
3kings
  • 838
  • 2
  • 13
  • 28
  • Wow that is complicated compared to using a `Clip` as seen on the [Java Sound info. page](http://stackoverflow.com/tags/javasound/info).. – Andrew Thompson Mar 21 '14 at 04:06