Hi Friends,
I want to show one of the easy and interesting application using JMF.
Here is the tutorial to build an audio player using this Java Media FrameWork.
First of all, set up the environment by downloading and configuring the JMF in your system from here.
Try this simple example :
Here, you are done... :) You can able to listen to song playing.... :-)
Lets try some thing little more:
Then a Simple Basic Audio Player is done.......... :D
The following is my code for your reference:
(Don't be scared with the code. Very few lines code is written by me....
Total code is auto-generated.... :-)
)
There are much more interesting things that you can do with JMF framework.....
Here is the API for JMF...
Explore more and enjoy java.................
I want to show one of the easy and interesting application using JMF.
Here is the tutorial to build an audio player using this Java Media FrameWork.
First of all, set up the environment by downloading and configuring the JMF in your system from here.
Try this simple example :
package jmf_test1; import java.io.File; import javax.media.Manager; import javax.media.Player; /** * @author naveen.k */ public class Main { static Player audioPlayer = null; public static void main(String[] args) { try { Manager.createRealizedPlayer(new File("D:\\Sample.mp3").toURL()).start(); } catch (Exception ex) { ex.printStackTrace(); } } }
Here, you are done... :) You can able to listen to song playing.... :-)
Lets try some thing little more:
- Take an JFrame Java Application.
- Create a Panel on existing JFrame.
- Create three buttons for Play, Pause and Stop.
- (see into image for your better understanding)
- Add Listeners for three of the buttons:
- Let you add the following code for the respective listeners
- Play: audioPlayer.start();
- Pause: audioPlayer.stop();
- Stop: audioPlayer.stop(); //stop and reset to 1st position audioPlayer.setMediaTime(new Time(0.0));
- Run the application.
Then a Simple Basic Audio Player is done.......... :D
The following is my code for your reference:
(Don't be scared with the code. Very few lines code is written by me....
Total code is auto-generated.... :-)
)
package jmf_test1; import java.io.File; import java.io.IOException; import java.net.URL; import javax.media.CannotRealizeException; import javax.media.Manager; import javax.media.NoPlayerException; import javax.media.Player; import javax.media.Time; /** * @author naveen.k */ public class AudioPlayerTest extends javax.swing.JFrame { Player audioPlayer = null; String audioPath = ""; /** Creates new form AudioPlayerTest */ public AudioPlayerTest() { initComponents(); audioPath = "D:\\sample.mp3"; initAudioPlayer(audioPath); } private void initAudioPlayer(String pathname) { try { URL url = new File(pathname).toURL(); audioPlayer = Manager.createRealizedPlayer(url); } catch (IOException ex) { ex.printStackTrace(); } catch (NoPlayerException ex) { ex.printStackTrace(); } catch (CannotRealizeException ex) { ex.printStackTrace(); } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") //private void initComponents() { mainPanel = new javax.swing.JPanel(); PlayButton = new javax.swing.JButton(); PauseButton = new javax.swing.JButton(); StopButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); PlayButton.setText("Play"); PlayButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { PlayButtonActionPerformed(evt); } }); PauseButton.setText("Pause"); PauseButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { PauseButtonActionPerformed(evt); } }); StopButton.setText("Stop"); StopButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { StopButtonActionPerformed(evt); } }); javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel); mainPanel.setLayout(mainPanelLayout); mainPanelLayout.setHorizontalGroup( mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(mainPanelLayout.createSequentialGroup() .addContainerGap() .addComponent(PlayButton) .addGap(27, 27, 27) .addComponent(PauseButton) .addGap(18, 18, 18) .addComponent(StopButton) .addContainerGap(36, Short.MAX_VALUE)) ); mainPanelLayout.setVerticalGroup( mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(mainPanelLayout.createSequentialGroup() .addContainerGap() .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(PlayButton) .addComponent(PauseButton) .addComponent(StopButton)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(69, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// private void PlayButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: audioPlayer.start(); } private void PauseButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: audioPlayer.stop(); } private void StopButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: audioPlayer.stop(); audioPlayer.setMediaTime(new Time(0.0)); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new AudioPlayerTest().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton PauseButton; private javax.swing.JButton PlayButton; private javax.swing.JButton StopButton; private javax.swing.JPanel mainPanel; // End of variables declaration }
There are much more interesting things that you can do with JMF framework.....
Here is the API for JMF...
Explore more and enjoy java.................
Good Post ---> +1
ReplyDeletenice post naveen this is really helpful... i learned many new things by reading this post... keep continue updating the thins
ReplyDeletecan you publish source code of project
ReplyDeleteyour code is very helpful....Is it possible to shuffle the songs using JMF???
ReplyDeleteI want to create a next button....hepl me!!!!!!
ReplyDeletethank u so much naveen k
ReplyDeleteplease share some ideas with me
anilds04@gmail.com
sure Anil...
DeleteThank you.. :)
i want to paly mp3 file not wav.
ReplyDeleteI am unable to hear sound.plz help me
ReplyDeleteeverything is fine,unable to hear sound.please help me.
ReplyDeletethanks in advance.
That's good.
ReplyDeletenot good.. too many exception
ReplyDeletei have come error like
ReplyDeleteFailed to configure: com.sun.media.PlaybackEngine@7c53a9eb
Bad header in the media: Couldn't detect stream type
Error: Unable to realize com.sun.media.PlaybackEngine@7c53a9eb
your code is really helpful and motivating for beginners.
ReplyDeletethanks a lot. :) :)
Thank you manoj :) :)
Deletetx
ReplyDeleteThanks for the Java tutorial, it is really a good programming language...... I have built ePrice on Java and PHP.
ReplyDelete
ReplyDeleteThanks for sharing this good blog.
Core Java Online Training
This comment has been removed by the author.
ReplyDeletehii I like your project your project is amazing and I have also written content on Computer Scienece with Java https://tutorial8java.blogspot.com/2021/06/computer-science-with-java-what-is.html
ReplyDelete