applet - Java drum set project -
hello i'm trying make java drum kit code not play sound when click buttons , i'm not sure why. have sound files in file code know thats not problem. here's code...
import java.applet.audioclip; import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.japplet; import javax.swing.jbutton; public class finalprojectst extends japplet implements actionlistener { private jbutton snarebutton; private jbutton hihatbutton; private jbutton bassbutton; private jbutton cymbalsbutton; private audioclip snare; private audioclip hihat; private audioclip bass; private audioclip cymbals; public void init() { setlayout(new flowlayout()); samplebuttons(); snare = getaudioclip(getcodebase(), "snare.wav"); hihat = getaudioclip(getcodebase(), "hihat.wav"); bass = getaudioclip(getcodebase(), "kick.wav"); cymbals = getaudioclip(getcodebase(), "crash.wav"); } private void samplebuttons() { snarebutton = new jbutton("snare"); hihatbutton = new jbutton("hi hat"); bassbutton = new jbutton("kick"); cymbalsbutton = new jbutton("cymbals"); snarebutton.addactionlistener(new buttonlistener()); hihatbutton.addactionlistener(new buttonlistener()); bassbutton.addactionlistener(new buttonlistener()); cymbalsbutton.addactionlistener(new buttonlistener()); add(snarebutton); add(hihatbutton); add(bassbutton); add(cymbalsbutton); } public void actionperformed(actionevent e) { if (e.getsource() == snarebutton) snare.play(); if (e.getsource() == hihatbutton) hihat.play(); if (e.getsource() == bassbutton) bass.play(); if (e.getsource() == cymbalsbutton) cymbals.play(); } }
there lot of bad things code, in short "file:/tmp/" return getcodebase() . pointing *.wav file nothing.
with code first button play funny wav sound of hours web.
import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; import java.net.malformedurlexception; import java.net.url; public class finalprojectst extends japplet implements actionlistener { private jbutton snarebutton; private jbutton hihatbutton; private jbutton bassbutton; private jbutton cymbalsbutton; private audioclip snare; private audioclip hihat; private audioclip bass; private audioclip cymbals; public void init() { setlayout (new flowlayout()); samplebuttons(); try { snare = getaudioclip(new url("http://download.wavetlan.com/svv/media/http/wav/media-convert/media-convert_test1_alaw_mono_vbr_8ss_16000hz.wav") ); //getcodebase(), "snare.wav"); } catch (malformedurlexception e) { e.printstacktrace(); } hihat = getaudioclip(getcodebase(), "hihat.wav"); bass = getaudioclip(getcodebase(), "kick.wav"); cymbals = getaudioclip(getcodebase(), "crash.wav"); } private void samplebuttons() { snarebutton = new jbutton("snare"); hihatbutton = new jbutton("hi hat"); bassbutton = new jbutton("kick"); cymbalsbutton = new jbutton("cymbals"); snarebutton.addactionlistener(this); //new buttonlistener() hihatbutton.addactionlistener(this); bassbutton.addactionlistener(this); cymbalsbutton.addactionlistener(this); add(snarebutton); add(hihatbutton); add(bassbutton); add(cymbalsbutton); } public void actionperformed(actionevent e) { if (e.getsource() == snarebutton) { snare.play(); system.out.println(getcodebase()); } if (e.getsource() == hihatbutton) hihat.play(); if (e.getsource() == bassbutton) bass.play(); if (e.getsource() == cymbalsbutton) cymbals.play(); } }
the reason works because changed missing buttonlistener() "this" (you don't need if have class). , have working path existing url first button. plays sound fine.
i suggest way accurate url need getcodebase() or other method.
Comments
Post a Comment