Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Sound Code Examples

Demonstrating the Java Media Player

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
Demonstrating the Java Media Player import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.media.*; public class MediaPlayerDemo extends JFrame { private Player player; private File file; public MediaPlayerDemo() { super( "Demonstrating the Java Media Player" ); JButton openFile = new JButton( "Open file to play" ); openFile.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { openFile(); createPlayer(); } }); getContentPane().add( openFile, BorderLayout.NORTH ); setSize( 300, 300 ); show(); } private void openFile() { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); int result = fileChooser.showOpenDialog( this ); // user clicked Cancel button on dialog if ( result == JFileChooser.CANCEL_OPTION ) file = null; else file = fileChooser.getSelectedFile(); } private void createPlayer() { if ( file == null ) return; removePreviousPlayer(); try { // create a new player and add listener player = Manager.createPlayer( file.toURL() ); player.addControllerListener( new EventHandler() ); player.start(); // start player } catch ( Exception e ) { JOptionPane.showMessageDialog( this, "Invalid file or location", "Error loading file", JOptionPane.ERROR_MESSAGE ); } } private void removePreviousPlayer() { if ( player == null ) return; player.close(); Component visual = player.getVisualComponent(); Component control = player.getControlPanelComponent(); Container c = getContentPane(); if ( visual != null ) c.remove( visual ); if ( control != null ) c.remove( control ); } public static void main(String args[]) { MediaPlayerDemo app = new MediaPlayerDemo(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit(0); } }); } // inner class to handler events from media player private class EventHandler implements ControllerListener { public void controllerUpdate( ControllerEvent e ) { if ( e instanceof RealizeCompleteEvent ) { Container c = getContentPane(); // load Visual and Control components if they exist Component visualComponent = player.getVisualComponent(); if ( visualComponent != null ) c.add( visualComponent, BorderLayout.CENTER ); Component controlsComponent = player.getControlPanelComponent(); if ( controlsComponent != null ) c.add( controlsComponent, BorderLayout.SOUTH ); c.doLayout(); } } } }