Java audio plays fine when run alone, not when run through separate class -
so class plays sound file 'theme a.wav' fine when ran itself.
this 'playaudio.class'
import java.io.*; import sun.audio.*; public class playaudio { public static playsound() { string sound = "theme a.wav"; inputstream audio = new fileinputstream(new file(sound)); audiostream astream = new audiostream(audio); audioplayer.player.start(astream); system.out.println("loaded playaudio.class; playing audio"); } public static void main(string args[]) { new playsound(); } }
however, play button found in main class, fails call 'playaudio.class when clicked. worked fine before added audio code (everything 'system.out.println("loaded playaudio.class; playing audio");' line). error didn't show until "throws exception" snippet added first class.
//play button play = new jbutton(new imageicon ("uiimages/play.png")); play.setbackground(color.white); play.setfocuspainted(false); //sets action play play.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { playaudio.playsound(); } });
any help? exception compiler throws.
1 error found: file: c:\users\hunter\workspace\src\rejomedia.java [line: 74] error: unhandled exception type java.lang.exception
put try/catch around playaudio.main(null) or inside main (instead of throwing).
the playaudio.main() call throw action event listener , eaten silently. silent exceptions absolutely worst thing can have in java code.
my guess it's throwing out because can't find file more (possibly "." directory moved because starting differently or using different main)
if that's case, 1 way "fix" supply full path.
if seeing exception, please add post , can assist further.
got it. it's build error. when "throw" exception main aren't handling in method calls main.
same solution gave above. wrap call main in try/catch:
try { playaudio.main(null); } catch(exception e) { system.out.println("caught exception:"+ e.getmessage()); e.printstacktrace(); }
Comments
Post a Comment