import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; import javazoom.jlgui.basicplayer.*; class JavaMediaPlayer extends JPanel implements BasicPlayerListener{ private JProgressBar jpb; private JButton play; private BasicPlayer player=new BasicPlayer();; private String imagePath="images/"; private static int p=0; private static boolean playBoolean=true; JavaMediaPlayer(){ setLayout(null); player.addBasicPlayerListener(this); play=new JButton(new ImageIcon(imagePath+"pause.png")); play.setToolTipText("pause");play.setBounds(2,2,20,20); play.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ if(playBoolean){ pausePlaying(); play.setToolTipText("Play"); play.setIcon(new ImageIcon(imagePath+"play.png")); playBoolean=false; return; } if(playBoolean==false){ resumePlaying(); play.setToolTipText("Pause"); play.setIcon(new ImageIcon(imagePath+"pause.png")); playBoolean=true; return; } } }); jpb=new JProgressBar(); jpb.setBorderPainted(true); jpb.setBounds(25,2,280,20); add(play); add(jpb); } public void startPlaying(String fileName){ try{ play.setIcon(new ImageIcon(imagePath+"pause.png")); play.setToolTipText("Pause"); playBoolean=true; player.open(new File(fileName)); player.play();//actually play file } catch(Exception d){ System.out.println(d); } } private void pausePlaying(){ try{ player.pause(); } catch(Exception e){ System.out.println(e); } } private void stopPlaying(){ try{ player.stop(); jpb.setValue(0); } catch(Exception d){ System.out.println(d); } } private void resumePlaying(){ try{ player.resume(); } catch(Exception d){ System.out.println(d); } } public void opened(Object arg0, Map map) { String k=map.get("audio.length.frames").toString(); jpb.setMaximum(Integer.parseInt(k)-15); } public void progress(int arg0, long arg1, byte[] arg2, Map map) { String s=map.get("mp3.frame").toString(); int k=(p-(Integer.parseInt(s))*(-1)); jpb.setValue(k); } public void stateUpdated(BasicPlayerEvent bpev) { if(bpev.getCode()==BasicPlayerEvent.STOPPED){ // Something here } } public void setController(BasicController arg0) { // throw new UnsupportedOperationException("Not supported yet."); } }
and the initializing code of the above code for java media player is as follows you can use it as you want it, its just for demonstration.
import javax.swing.*; class Main { public static void main(String[] args) { JFrame f=new JFrame(); JavaMediaPlayer jmp=new JavaMediaPlayer(); f.add(jmp); f.setDefaultCloseOperation(3); f.setVisible(true); f.setSize(330,60); f.setTitle("Java Media Player - Tutorial Jinni"); jmp.startPlaying("PATH_TO_MP3_FILE"); } }
after you run it should be somewhat like this.
You can download a netbeans project, it contains all the libraries you want to make a java mp3 player.
0 comments:
Post a Comment