Skip to main content

Thread: [Java][OpenJDK] Sound clips play only one time


i have written puzzle game in java , decided pick development again. started programming game on windows oracle java , using openjdk on ubuntu 10.04, lucid lynx. ide eclipse. development kit version 6b20-1.9.10-0ubuntu1~10.04.2 (everything installed repositories).

problem in title: sound clips can played once calling play() method clips second time yields no sound, no exceptions thrown either. playing each clip 1 time works .wav files loaded correctly seems. debugging purpose placed printlines in soundhandler.play() method. line "trying (re)play clip" printed everytime sound should played. soundclips[clipid].isrunning() never reports particular clip running.

doing wrong (outside writing bad code )?

imports audio playback:
code:
import javax.sound.sampled.audioformat; import javax.sound.sampled.audioinputstream; import javax.sound.sampled.audiosystem; import javax.sound.sampled.clip; import javax.sound.sampled.dataline; import javax.sound.sampled.lineevent; import javax.sound.sampled.linelistener; import javax.sound.sampled.lineunavailableexception; import javax.sound.sampled.unsupportedaudiofileexception;
the class responsible loading , playing sounds:
code:
class soundhandler implements linelistener{          public static final int rotate = 0;     public static final int mirror = 1;     public static final int place = 2;     public static final int free_piece = 3;     public static final int drop_piece = 4;          private static soundhandler thissoundhandler;          private clip[] soundclips;              private soundhandler(){         soundclips = new clip[5];         java.net.url url;         url = getclass().getresource("/sounds/17892__zippi1__sound_click2.wav");         soundclips[rotate] = getsoundclipfromurl(url);         url = getclass().getresource("/sounds/puzzlegame_mirror.wav");         soundclips[mirror] = getsoundclipfromurl(url);         url = getclass().getresource("/sounds/17893__zippi1__sound_click3.wav");         soundclips[place] = getsoundclipfromurl(url);         url = getclass().getresource("/sounds/15348__hell_s_sound_guy__bubble_pop_.wav");         soundclips[free_piece] = getsoundclipfromurl(url);         url = getclass().getresource("/sounds/13832__adcbicycle__23.wav");         soundclips[drop_piece] = getsoundclipfromurl(url);     }          public static soundhandler getinstance(){         if(thissoundhandler == null){             thissoundhandler = new soundhandler();         }         return thissoundhandler;     }          public clip getsoundclipfromurl(java.net.url url){         clip thisclip;         audioformat af;         audioinputstream ais;         try{             ais = audiosystem.getaudioinputstream(url);             af = ais.getformat();           } catch (ioexception e) {             system.out.println(e.getmessage());             ais = null;             af = null;         } catch (unsupportedaudiofileexception e){             system.out.println("format not supported: " + e.getmessage());             ais = null;             af = null;         }         dataline.info info = new dataline.info(clip.class, af);         try{             thisclip = (clip)audiosystem.getline(info);             thisclip.open(ais);             thisclip.addlinelistener(this);         } catch(lineunavailableexception e){             e.printstacktrace();             thisclip = null;         } catch(ioexception e){             e.printstacktrace();             thisclip = null;         }         return thisclip;     }          public void play(int clipid){         if((clipid >=0) && (clipid < soundclips.length) && (soundclips[clipid] != null)){             system.out.println("trying (re)play clip:");             try{                 if(soundclips[clipid].isrunning()){                     system.out.println("clip still running.");                     soundclips[clipid].stop();                                     }                 soundclips[clipid].setframeposition(0);                             soundclips[clipid].start();             } catch(exception e){                 e.printstacktrace();             }         }     }          public void update(lineevent e){         /* nothing here yet */     } }

a little bump.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [Java][OpenJDK] Sound clips play only one time


Ubuntu

Comments

Popular posts from this blog

How to set the order of FAQs instead of alphabetical

Thread: Get UK Keyboard working

how do I change the e-mail address for my merchant account