Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576

Buy the Book!
Amazon logo
InformIT logo

Java, through its class libraries, provides extensive multimedia facilities that enable you to develop powerful multimedia applications. In this tutorial, we show how to play video and other media with the Java Media Framework. This tutorial is intended for students and developers who are familiar with Java GUIs and event handling.

Download the code examples for this tutorial.

[Note: This tutorial is an excerpt (Section 21.6) of Chapter 21, Multimedia, from our textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.992-996. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

21.6 Playing Video and Other Media with the Java Media Framework (Continued)

The application in Fig. 21.7 displays a JFileChooser dialog for the user to choose a media file. It then creates a MediaPanel that plays the selected file and creates a JFrame to display the MediaPanel.s

Fig. 21.7 | Test application that creates a MediaPanel from a user-selected file.
1     // Fig. 21.7: MediaTest.java
2     // A simple media player
3     import java.io.File;
4     import java.net.MalformedURLException;
5     import java.net.URL;
6     import javax.swing.JFileChooser;
7     import javax.swing.JFrame;
8    
9     public class MediaTest
10    {
11       // launch the application
12       public static void main( String args[] )
13       {
14          // create a file chooser
15          JFileChooser fileChooser = new JFileChooser();
16   
17          // show open file dialog
18          int result = fileChooser.showOpenDialog( null );
19   
20          if ( result == JFileChooser.APPROVE_OPTION ) // user chose a file
21          {
22             URL mediaURL = null;
23   
24             try
25             {
26                // get the file as URL
27                mediaURL = fileChooser.getSelectedFile().toURL();
28             } // end try
29             catch ( MalformedURLException malformedURLException )
30             {
31                System.err.println( "Could not create URL for the file" );
32             } // end catch
33   
34             if ( mediaURL != null ) // only display if there is a valid URL
35             {
36                JFrame mediaTest = new JFrame( "Media Tester" );
37                mediaTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
38   
39                MediaPanel mediaPanel = new MediaPanel( mediaURL );
40                mediaTest.add( mediaPanel );
41   
42                mediaTest.setSize( 300, 300 );
43                mediaTest.setVisible( true );
44             } // end inner if
45          } // end outer if
46       } // end main
47    } // end class MediaTest

Method main (lines 12–46) assigns a new JFileChooser to local variable fileChooser (line 15), shows an open file dialog (line 18) and assigns the return value to result. Line 20 checks result to determine whether the user chose a file. To create a Player to play the selected media file, you must convert the File object returned by JFileChooser to a URL object. Method toURL of class File returns a URL that points to the File on the system, possibly throwing a MalformedURLException if it cannot create a URL object for the File. The try statement (lines 24–32) creates a URL for the selected file and assigns it to mediaURL. The if statement in lines 34–44 checks that mediaURL is not null and creates the GUI components to play the media.

Previous Page
Return to Tutorial Index