الأربعاء، 11 مارس 2009

Create or Embed media player in Java program under windows

I wanted to create my own media player using java which has the codec for many media types.

NOTE: that works for windows OS.

Tools:
I used netbeans 6.5 but any IDE can do the job.
JDK (1.6.0_07) installed.
I use JMF as a library for the program but it's not required to install it.

Required Files:
jmf.properties
libfobs4jmf.a
fobs4jmf.ja
jmf.jar
fobs4jmf.dll


Steps:
1. Create a new project.
2. Add Libraries (fobs4jmf.jar, jmf.jar) to the project.
3. Create new JFrame class. make it like the picture.

NOTE: the panel in the middle is JPanle and name mediaPanel in code

4. Add class mediaPanel to your source package.
5. For The JMenuItem action (or the button that will open the file chooser) write this piece of code:

JFileChooser fc = new JFileChooser();
URL mediaURL;
if (mediaPanel instanceof MediaPanel)
((MediaPanel)mediaPanel).stopPlayer();

if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
try {
mediaURL = fc.getSelectedFile().toURI().toURL();
if ( mediaURL != null ) // Only display if there is a valid URL
{
mediaPanel = new MediaPanel(mediaURL);
showMedia();
}
} catch (MalformedURLException ex) {
Logger.getLogger(MediaPlayer.class.getName()).log(Level.SEVERE, null, ex);
}
}


6. Add a private method showMedia() and implement it as following:
THE TARGET IS TO ADD THE PANEL TO THE FRAME AGAIN SO WE CAN VIEW THE CHANGES
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(mediaPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(mediaPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);


7. Add the following files to your dist folder (beside the jar file)
"fobs4jmf.dll, jmf.properties, libfobs4jmf.a"

8. Run the program and browse for a file to play :)
NOTE: JMF doesn't support long named files.




References for more information:
Geertjan's Blog


Website counter

ليست هناك تعليقات: