java - How to make sound loop using this method? -


i did sound in wierd way, think there may way loop it? cuz plays once stops. here code 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 sound { private final static int buffer_size = 12800000; private static file soundfile; private static audioinputstream audiostream; private static audioformat audioformat; private static  sourcedataline sourceline;  /**  *   * @param filename name of file going played  *  */ public static void playsound(string filename){      string strfilename = filename;      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(); } } 

to activate do:

new thread(new runnable() {           public void run() {             sound.playsound("hurt.au");           }         }).start(); 

(if sound.playsound("hurt.au"); game freezes because sound plays on games main thread put sound in it's own thread save game freezing)

so question is, how loop it?

you need sort of test see whether sound playing finished or not. if making game, might suggest using libraries make easier. such slick2d. has inbuilt functions loop sound instead of playing once. if choose not to, have keep track of thread , on every update of game state @ sound object , ask sourceline if has finished playing or not. if has sourceline.start() else no-op. put thread inside sound.playsound method itself, thereby making code little bit less coupled.

http://www.slick2d.org/

i recommend using 3rd party library make easier on though.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -