netbeans - Sound problems in Java -


i have questions playing sound in java , hope can me out.
1. how can stop playing sound "stop" button?
2. how can slow down (or cooldown time) sound?
3. want create option frame can adjust volume , have mute option, how can that?
code:

    private void bgm() {         try {             file file = new file(apppath + "\\src\\bgm.wav");             clip clip = audiosystem.getclip();             clip.open(audiosystem.getaudioinputstream(file));             clip.start();         } catch (exception e) {             system.err.println(e.getmessage());         }     }   

any appreciated, and, have nice day!

you're working in object oriented programming lanuage, let's take advantage of , encapsulate management of clip/audio simple class...

public class audioplayer {      private clip clip;      public audioplayer(url url) throws ioexception, lineunavailableexception, unsupportedaudiofileexception {         clip = audiosystem.getclip();         clip.open(audiosystem.getaudioinputstream(url.openstream()));     }      public boolean isplaying() {         return clip != null && clip.isrunning();     }      public void play() {         if (clip != null && !clip.isrunning()) {             clip.start();         }     }      public void stop() {         if (clip != null && clip.isrunning()) {             clip.stop();         }     }      public void dispose() {         try {             clip.close();         } {             clip = null;         }     }  } 

now, use it, need create class instance field allow access value anywhere within class want use it...

private audioplayer bgmplayer; 

then, when need it, create instance of audioplayer , assign variable

try {     bgmplayer = new audioplayer(getclass().getresource("/bgm.wav")); } catch (ioexception | lineunavailableexception | unsupportedaudiofileexception ex) {     ex.printstacktrace(); } 

now, when need to, call bgmplayer.play() or bgmplayer.stop()


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -