0


I am trying to play background music for my game using javax.sound. The problem is that only the first half second of the song is playing before stopping. I want the entire song to play. I would attach the .wav file on this post but StackOverflow won't allow files to be attached.

AudioInputStream audioIn = AudioSystem.getAudioInputStream(new File("path/to/file/sound.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
//I've tried both of the following:
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);

Thanks for the help.

akarshkumar0101
  • 367
  • 2
  • 10
  • If you're letting `clip` fall out of scope, it's going to be garbage collected. – Powerlord Sep 01 '17 at 23:52
  • Oh, I understand, thank you! What would be the best way to block the method until a sound finishes playing? I don't like the idea of using a while loop checking if the sound is finished. @Powerlord – akarshkumar0101 Sep 01 '17 at 23:54
  • `clip.drain()` might be what you're looking for. – TNT Sep 01 '17 at 23:55
  • I am using clip.drain() and it isn't playing fully. I also tried clip.start() then clip.drain()... not working either. – akarshkumar0101 Sep 01 '17 at 23:56
  • The answer [here](https://stackoverflow.com/questions/11919009/using-javax-sound-sampled-clip-to-play-loop-and-stop-mutiple-sounds-in-a-game), and the [page it references](http://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html) should help. – Chris Jaekl Sep 02 '17 at 06:19

0 Answers0