• Advertisement
Sign in to follow this  

[java] Looping music

This topic is 3327 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, this one has been driving me up a tree. Right now, I'm working on a small personal project which I'm getting to the point where it plays music. Problem is, I need to be able to play a simple looping song and for some reason, I can't seem to find the solution to this dilemma. Right now I cooked up a little class that I can get to play sounds but the steps I've taken to try to get it to loop them has sort of blew up in my face. Now it plays a song but starts at the wrong point and plays it ultra-slow. I'm hoping there's a solution to this because it's becoming a pain in the butt. Anyway, Here's what my NWsound(I labeled the packages NW off of the project name.):
package nwsound; import java.io.*; import sun.audio.*; import sun.audio.ContinuousAudioDataStream; import javax.sound.sampled.*; import nwcollection.NWcollectable; import nwmain.NWgame; public class NWtheme implements NWcollectable { String songName, fileLoc; ContinuousAudioDataStream songStream; public NWtheme(String songName) { this.songName = songName; fileLoc = NWgame.dataDirec + "\\songs\\" + songName + ".au"; } public void play() { try{ InputStream fi = new FileInputStream(fileLoc); AudioStream ais = new AudioStream(fi); byte[] songData = new byte[ais.getLength()]; ais.read(songData,0,ais.getLength()); AudioData ad = new AudioData(songData); songStream = new ContinuousAudioDataStream(ad); AudioPlayer.player.start(songStream); ais.close(); fi.close(); }catch(Exception ex){ ex.printStackTrace(); System.exit(0); } } public void stop() { AudioPlayer.player.stop(songStream); } public void dispose() { try{ if(songStream != null) songStream.close(); }catch(Exception ex){ ex.printStackTrace(); System.exit(0); } } public void finalize() { dispose(); try{ super.finalize(); }catch(Throwable ex){ } } public Object getKey(){return songName;} public String returnTypeName(){return "theme";} }

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement