// Windows Media Player sample. // // To use this sample, use EZ JCom to build a media player api, using the package name "wmp". // // You can use a local file path, or enter a http:// URL containing a media item. // import wmp.IWMPMedia; import wmp.IWMPPlayer4; import wmp.WindowsMediaPlayer; import ezjcom.JComObject; import ezjcom.JComActiveXContainer; import ezjcom.JComException; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JFileChooser; import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class JavaWMP extends JFrame { IWMPPlayer4 player; JButton browseButton = new JButton(); JTextField mediaPath = new JTextField( 24 ); JButton playButton = new JButton(); JPanel buttonPanel = new JPanel(); // The "browse" button lets user select a media file on the local hard disk. void onBrowseButton() { JFileChooser fc = new JFileChooser(); if ( fc.showOpenDialog( this ) == JFileChooser.APPROVE_OPTION ) { mediaPath.setText( fc.getSelectedFile().getAbsolutePath()); } } // The play button plays the media file named in the text field. void onPlayButton() { try { IWMPMedia media = player.newMedia( mediaPath.getText()); player.getCurrentPlaylist().appendItem( media ); player.getControls().play(); } catch (Exception ex) { ex.printStackTrace(); } } void showPlayer() { try { setTitle( "Windows Media Player: Java API generated by EZ JCom" ); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); getContentPane().setLayout( new BorderLayout()); // Create a container instance and add it to the frame. ezjcom.JComActiveXContainer activexContainer = new ezjcom.JComActiveXContainer(); getContentPane().add( activexContainer, BorderLayout.CENTER ); browseButton.setText( "Local File..." ); buttonPanel.add( browseButton ); buttonPanel.add( mediaPath ); playButton.setText( "Play" ); buttonPanel.add( playButton ); getContentPane().add( buttonPanel, BorderLayout.SOUTH ); browseButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { onBrowseButton(); } }); playButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { onPlayButton(); } }); // Create the ActiveX object and attach it to the container. WindowsMediaPlayer wmp = new WindowsMediaPlayer(); player = wmp.getIWMPPlayer4(); activexContainer.setActiveX(wmp); // Show the frame. setSize( 600, 400 ); setVisible( true ); } catch (Exception ex) { ex.printStackTrace(); } } public static void main( String[] args ) { new JavaWMP().showPlayer(); } }