Java示例非常简单的播放器(JMF)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java示例非常简单的播放器(JMF)相关的知识,希望对你有一定的参考价值。

  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. }

以上是关于Java示例非常简单的播放器(JMF)的主要内容,如果未能解决你的问题,请参考以下文章

如何播放wav文件

Java 视频/音乐播放器

用于媒体的框架

使用 JMF 将多路复用的音频/视频录制到文件中

Java音乐播放:Java Media Framework API(已停止更新)

java如何读取MP3格式文件的内容然后对其播放实例代码