audio - Java - How to stop an audioclip from another class -



audio - Java - How to stop an audioclip from another class -

right now, i'm making app involves background audio, , couple of tips other's questions , answers, able create sound work.

but right now, i'm trying utilize button in different class stop audioclip, instantiated in class.

when tested prototype, in same class, worked perfectly.

but in main app, "stop" button in different class (for reason). don't know if you'll want code main app, here's prototype described: help appreciated, thanks.

public class audioplay { clip clip; // constructor public audioplay() { seek { this.clip = audiosystem.getclip(); } grab (lineunavailableexception ex) { logger.getlogger(audioplay.class.getname()).log(level.severe, null, ex); } jframe f = new jframe(); f.setdefaultcloseoperation(jframe.exit_on_close); f.settitle("test sound clip"); f.setsize(300, 200); f.setlayout(new flowlayout()); jbutton button = new jbutton("play"); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { playsound(0); } }); jbutton button2 = new jbutton("stop"); button2.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { clip.stop(); } }); f.add(button); f.add(button2); f.setvisible(true); } public void playsound(int a){ // open sound input stream. string[] sounds = new string[10]; sounds[0]= "/audioplay/sounds/kk.wav"; sounds[1]= "/audioplay/sounds/btn2.wav"; seek { url url = this.getclass().getresource(sounds[a]); audioinputstream audioin = audiosystem.getaudioinputstream(url); // open sound clip , load samples sound input stream. clip.open(audioin); clip.start(); } grab (unsupportedaudiofileexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } grab (lineunavailableexception e) { e.printstacktrace(); } } public static void main(string[] args) { new audioplay(); } }

edit here's actual application first of main class.............

public class operator { static boolean player = false; clip clip; /** * @param args command line arguments */ public static void main(string[] args) { // todo code application logic here welcome on = new welcome(); on.setlocationrelativeto(null); on.setvisible(true); } public operator() { seek { this.clip = audiosystem.getclip(); } grab (lineunavailableexception ex) { logger.getlogger(operator.class.getname()).log(level.severe, null, ex); } } public void playbackground(int a){ string[] sounds = new string[10]; sounds[0]= "/operator/sounds/kk.wav"; sounds[1]= "/operator/sounds/mnt5.wav"; seek { // open sound input stream. url url= this.getclass().getresource(sounds[a]); audioinputstream audioin = audiosystem.getaudioinputstream(url); // sound clip resource. // open sound clip , load samples sound input stream. clip.open(audioin); clip.start(); } grab (unsupportedaudiofileexception | ioexception | lineunavailableexception e) { } } }

now next up, sound begins play

public class mainmenutest extends javax.swing.jpanel { // referencing operator sound = new operator(); static int course; static string coursename; //constructor obviously.......... public mainmenutest() { initcomponents(); } public javax.swing.jpanel jpanel1; @suppresswarnings("unchecked") private void initcomponents() { jpanel1 = new javax.swing.jpanel(); sound.playbackground(0); /* writing stop() action here below, causes sound not play, want, in class sound.clip.stop(); */ //most of components removed, weren't relevant } }

and lastly, class in clip stopped

public final class options extends jpanel { // referencing stuff operator sound = new operator(); private jbutton start; public options() { initcomponents(); } @suppresswarnings("unchecked") private void initcomponents() { stop = new javax.swing.jbutton("stop"); stop.addactionlistener(new actionlistener() { public void stopactionperformed(actionevent e) { //this looks same stop() method wrote before, here, ain't working sound.clip.stop(); }}); } }

as guessed, comments making new objects. making new object having twin. telling 1 twin stop talking not impact other...

operator sound = new operator(); //in code should done once.

do in main mehtod , have other classes need take in constructor or utilize spring or other dependancy injection.

a simple sample:

package play; import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.file; import java.net.url; import java.util.logging.level; import java.util.logging.logger; import javax.sound.sampled.audioinputstream; import javax.sound.sampled.audiosystem; import javax.sound.sampled.clip; import javax.sound.sampled.lineunavailableexception; import javax.swing.jbutton; import javax.swing.jframe; public class audioplay { private clip clip; jframe f = new jframe(); // constructor public audioplay() { seek { this.clip = audiosystem.getclip(); } grab (lineunavailableexception ex) { logger.getlogger(audioplay.class.getname()).log(level.severe, null, ex); } f.setdefaultcloseoperation(jframe.exit_on_close); f.settitle("test sound clip"); f.setsize(300, 200); f.setlayout(new flowlayout()); jbutton button = new jbutton("play"); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { playsound(0); } }); f.add(button); //f.setvisible(true); } public void stopsound() { clip.stop(); //clip.flush(); clip.close(); } public void playsound(int a){ // open sound input stream. string[] sounds = new string[10]; sounds[0]= "rs/tr-3l_na_sus_mf_d4.wav"; sounds[1]= "rs/tr-3l_na_sus_mf_f#4.wav"; file f = new file(sounds[a]); seek { url url = f.touri().tourl(); audioinputstream audioin = audiosystem.getaudioinputstream(url); // open sound clip , load samples sound input stream. clip.open(audioin); clip.start(); } grab (exception e) { e.printstacktrace(); system.out.println(e + " " + f); } } public static void main(string[] args) { audioplay ap = new audioplay(); other oth = new other(ap, ap.f); ap.shw(); } public void shw(){ f.setvisible(true); } }

//and

package play; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; public class other { private audioplay au; private jbutton btnstop; public other(audioplay aupl, jframe f){ this.au = aupl; btnstop = new jbutton("stop"); f.add(btnstop); btnstop.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { au.stopsound(); } }); } }

here see how other accepts reference sound play class. , sound play class made 1 time (new).

fyi : not applicable in general : want create new object if need old 1 or old 1 somehting else. illustration info transfer object 1 entity id = 1 , other has identity 2, , keeping track of them in hashmap or other collection.

also in web apps have session , request scopes - beyond sample. sample main thing in ui want 1 instance of every class, , different classes need same instance talk to. 1 class cannot talk instance 1 while talks instance 2, , expect instance 1 something.

java audio

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -