Java - Example Very Simple Player (JMF)


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package org.jmf.example;
  2.  
  3. import javax.swing.JDialog;
  4. import javax.swing.JFrame;
  5. import javax.swing.UIManager;
  6. import javax.swing.UnsupportedLookAndFeelException;
  7. import javax.swing.plaf.metal.MetalLookAndFeel;
  8.  
  9. public class ExampleJMF
  10. {
  11. public static void main(String[] args)
  12. {
  13. JFrame.setDefaultLookAndFeelDecorated(true);
  14. JDialog.setDefaultLookAndFeelDecorated(true);
  15.  
  16. try
  17. {
  18. UIManager.setLookAndFeel(new MetalLookAndFeel());
  19. }
  20. {
  21. e.printStackTrace();
  22. }
  23.  
  24. new exampleFrame();
  25. }
  26. }
  27.  
  28.  
  29. package org.jmf.example;
  30.  
  31. import java.awt.Toolkit;
  32. import java.awt.event.WindowAdapter;
  33. import java.awt.event.WindowEvent;
  34.  
  35. import javax.swing.JFrame;
  36.  
  37. public class exampleFrame extends JFrame
  38. {
  39. private static final long serialVersionUID = 1L;
  40.  
  41. public exampleFrame()
  42. {
  43. super("JMF - Example...");
  44.  
  45. setSize(400, 300);
  46. setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getWidth())/2, (Toolkit.getDefaultToolkit().getScreenSize().height - getHeight())/2);
  47.  
  48. addWindowListener(new WindowAdapter()
  49. {
  50. public void windowClosing(WindowEvent evt)
  51. {
  52. System.exit(0);
  53. }
  54. });
  55.  
  56. setContentPane(new examplePanel());
  57. setVisible(true);
  58. }
  59. }
  60.  
  61.  
  62. package org.jmf.example;
  63.  
  64. import java.awt.Component;
  65. import java.awt.Graphics;
  66. import java.awt.event.ActionEvent;
  67. import java.awt.event.ActionListener;
  68. import java.io.IOException;
  69. import java.net.MalformedURLException;
  70. import java.net.URL;
  71.  
  72. import javax.media.ControllerEvent;
  73. import javax.media.ControllerListener;
  74. import javax.media.Manager;
  75. import javax.media.NoPlayerException;
  76. import javax.media.Player;
  77. import javax.media.RealizeCompleteEvent;
  78. import javax.swing.JPanel;
  79.  
  80. public class examplePanel extends JPanel implements ActionListener, ControllerListener
  81. {
  82. private static final long serialVersionUID = 1L;
  83.  
  84. private Component visualComponent;
  85. private Player player;
  86.  
  87. public examplePanel()
  88. {
  89. try
  90. {
  91. player = Manager.createPlayer(new URL("file:///tmp/a.mpg"));
  92. player.addControllerListener(this);
  93.  
  94. player.start();
  95. }
  96. catch(NoPlayerException e)
  97. {
  98. e.printStackTrace();
  99. }
  100. {
  101. e.printStackTrace();
  102. }
  103. catch(IOException e)
  104. {
  105. e.printStackTrace();
  106. }
  107. }
  108.  
  109. public void paintComponent(Graphics g)
  110. {
  111. super.paintComponent(g);
  112. }
  113.  
  114. public void actionPerformed(ActionEvent e)
  115. {
  116.  
  117. }
  118.  
  119. public void controllerUpdate(ControllerEvent c)
  120. {
  121. if(player == null)
  122. return;
  123.  
  124. if(c instanceof RealizeCompleteEvent)
  125. {
  126. if((visualComponent = player.getVisualComponent()) != null)
  127. add(visualComponent);
  128. }
  129. }
  130. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.