pause audio clip in java -


i have code 2 classes mplayer , audio.

mplayer has gui (jbuttons, jpanel , jframe etc), , audio has code play audio.
problem when call pause method in class mplayer, not pause audio.

can me it?

mplayer.java:

import java.awt.*; import javax.swing.*; import java.awt.event.*;  public class mplayer extends jframe implements actionlistener {     imageicon icon1, icon2, icon3, icon4;     jpanel pan1;     jframe frame;     jmenubar bar;     jmenu menu;     jmenuitem menuitem;     jbutton btn1, btn2;      public void initgui() {         bar = new jmenubar();         menu = new jmenu("open");         bar.add(menu);         menuitem = new jmenuitem("open");         menu.add(menuitem);         borderlayout bl = new borderlayout();         flowlayout fl = new flowlayout();          pan1 = new jpanel();         frame = new jframe("music player");         frame.setlayout(bl);         pan1.setlayout(fl);         frame.setjmenubar(bar);          icon1 = new imageicon("./play.jpg");         icon2 = new imageicon("./pause.png");         icon3 = new imageicon("./stop.jpg");         icon4 = new imageicon("./wallpaper.jpg");          frame.getcontentpane().setbackground(color.black);         btn1 = new jbutton(icon1);         btn1.setvisible(true);         btn2 = new jbutton(icon3);         btn2.setvisible(true);         pan1.add(btn1);         pan1.add(btn2);          frame.add(new jlabel(icon4), borderlayout.center);         frame.add(pan1, borderlayout.south);          frame.setdefaultcloseoperation(exit_on_close);         frame.setsize(500, 500);         frame.setlocationrelativeto(null);         frame.setvisible(true);         frame.setfocusable(true);          btn1.addactionlistener(this);         menuitem.addactionlistener(this);     }      public void actionperformed(actionevent e) {         audio aud = new audio();          if (e.getsource() instanceof jmenuitem) {             if (((jmenuitem) (e.getsource())) == menuitem) {                 aud.openfile();             }         }          if (btn1.geticon() == icon1) {             btn1.seticon(icon2);             aud.pause();         } else if (btn1.geticon() == icon2) {             btn1.seticon(icon1);         }     }      public static void main(string[] args) {         mplayer gui = new mplayer();         gui.initgui();     } } 

audio.java:

import javax.sound.sampled.*; import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*;  public class audio {     file file;     audioinputstream audio;     audioformat fmt;     dataline.info dli;     clip c;     jfilechooser jfc;      public void openfile() {         try {             jfc = new jfilechooser();             jfc.showopendialog(null);             file = jfc.getselectedfile();             audio = audiosystem.getaudioinputstream(file);             fmt = audio.getformat();             dli = new dataline.info(clip.class, fmt);             c = (clip) audiosystem.getline(dli);             c = audiosystem.getclip();             c.open(audio);             c.start();         } catch (exception ex) {             ex.getmessage();         }     }      public void pause() {          if (c.isrunning()) {             c.stop();         }     } } 

you doing in same thread. swing applications start on called event dispatch thread - thread handles ui interactions. should not performing long-running tasks (like playing audio) in thread.

spawn new thread play audio, , think you'll find things work better. can manually or can use swingworker this, allow provide regular feedback on "progress" of playing (i.e. can you're x% done file etc, or there 3m25s left etc).

the class documentation swingworker quite , highly suggest read it, regardless of whether or not going use it, has suggestions threads in swing in general.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -