Professional Documents
Culture Documents
Introduction
In this post Im going to show you how to play an audio and a video file using a java application. At first I have to mention that Im using Java Media Framework (JMF) for this purpose. So you have to download and install JMF from here. According to the web page you can, Playback audios and videos Capture from devices Transmitt over an internet connection JMF is providing you necessary classes and you have to do less. But in here Ive to mention that JMF is a bit old too. In many cases you wont be able to play every media file you want. Because there are some restrictions. Through following examples Ill show you how to get the use of JMF.
Manager: Manager is responsible for creating appropriate player object using an URL or a Media Locator. Manager is also use to create a processor will be explained later. SimpleAudioPlayer.java
1 2 3 4 5 6 7 8 9 10
import java.io.File; import java.net.MalformedURLException; import java.net.URL; import javax.media.*; // import JMF classes
import javax.swing.JFileChooser;
11 12 13 14 15 16 17 18 19
//MediaLocator ml=new MediaLocator(url); try { public SimpleAudioPlayer(URL url) { private Player audioPlayer = null; public class SimpleAudioPlayer {
20 21
audioPlayer = Manager.createPlayer(url);
22 23
} catch (Exception ex) {
24 25
System.out.println(ex);
26 27
}
28
29 30 31 32 33 34 35 36
this(file.toURI().toURL());
37 38
audioPlayer.start(); // start playing
39 40
}
41 42
public void stop() {
43 44
audioPlayer.stop(); //stop playing
45 46
audioPlayer.close();
47 48 49 50 51 52 53 54 55
JFileChooser fc = new JFileChooser(); // TODO code application logic here try { public static void main(String[] args) { }
56 57
fc.showOpenDialog(null);
58 59
File file = fc.getSelectedFile();
60 61
SimpleAduioPlayer sap = new SimpleAduioPlayer(file);
62 63
sap.play();
64
65 66 67 68 69 70 71 72
//sap.stop();
System.out.println(ex);
73 74
}
75 76 77 78
With this example you can play MP3, WAV, and AU files.
1 2 3 4 5
import java.awt.BorderLayout; import java.awt.Component; import java.net.MalformedURLException; import java.net.URL; import javax.media.Manager; import javax.media.MediaLocator;
6
import javax.media.Player;
7
import javax.swing.JFileChooser;
8
import javax.swing.JFrame;
9 10
/**
11 12 13 14 15 16 17 18
* * @author BUDDHIMA */
19 20 21 22 23 24 25 26
initComponents();
setLayout(new BorderLayout());
try {
Component video=mediaPlayer.getVisualComponent();
27 28
Component control=mediaPlayer.getControlPanelComponent();
29 30
if (video!=null) {
31 32
add(video, BorderLayout.CENTER);
33 34
}
35 36
add(control, BorderLayout.SOUTH); // place the control in panel
37 38 39 40 41 42 43 44 45
public static void main(String[] args) { } } } catch (Exception e) { mediaPlayer.start();
46 47
JFileChooser fileChooser = new JFileChooser();
48 49
fileChooser.showOpenDialog(null);
50 51
URL mediaUrl=null;
52 53
try {
54
55 56 57 58 59 60 61 62
mediaUrl = fileChooser.getSelectedFile().toURI().toURL();
System.out.println(ex);
63 64
mediaTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
65 66
MediaPlayer mediaPanel = new MediaPlayer( mediaUrl );
67 68
mediaTest.add( mediaPanel );
69 70
mediaTest.setSize( 800, 700 ); // set the size of the player
71 72
mediaTest.setLocationRelativeTo(null);
73 74 75 76 77 78 79 80 81 82
} } mediaTest.setVisible( true );
When you run this code and select a file, you would get something like this. But you can only play limited number of video fromats only (.mpg, some .avi files). Give a try !
Resources:
1. JMF official site : http://www.oracle.com/technetwork/java/javase/tech/index-jsp-140239.html 2. JMF examples : http://blogs.sun.com/geertjan/date/20070121
About these ads