JMF这个多媒体开发框架太牛了,简单的几句代码就能实现一个视频播放器的开发,厉害,就是支持的格式少了一些,没关系,这个视频播放器可以播放mpg,avi,fvl等等,想播放其他的请开发自己的插件,下面将代码贴上去
java" id="highlighter_86481">
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
|
package com.bird.jmf; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.Frame; import java.awt.Panel; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import javax.media.CannotRealizeException; import javax.media.ControllerEvent; import javax.media.ControllerListener; import javax.media.EndOfMediaEvent; import javax.media.Manager; import javax.media.MediaLocator; import javax.media.NoPlayerException; import javax.media.Player; import javax.media.PrefetchCompleteEvent; import javax.media.RealizeCompleteEvent; import javax.media.Time; @SuppressWarnings ({ "restriction" , "unused" }) public class JMFSample implements ControllerListener { public static void main(String[] args) { JMFSample sp = new JMFSample(); sp.play(); } private Player mediaPlayer; private Frame f; private Player player; private Panel panel; private Component visual; private Component control = null ; public void play(){ f = new Frame( "JMF Sample1" ); f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent we) { if (player != null ) { player.close(); } System.exit( 0 ); } }); f.setSize( 500 , 400 ); f.setVisible( true ); URL url = null ; try { //准备一个要播放的视频文件的URL url = new URL( "file:/d:/2.mpg" ); } catch (MalformedURLException e) { e.printStackTrace(); } try { //通过调用Manager的createPlayer方法来创建一个Player的对象 //这个对象是媒体播放的核心控制对象 player = Manager.createPlayer(url); } catch (NoPlayerException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } //对player对象注册监听器,能噶偶在相关事件发生的时候执行相关的动作 player.addControllerListener( this ); //让player对象进行相关的资源分配 player.realize(); } private int videoWidth = 0 ; private int videoHeight = 0 ; private int controlHeight = 30 ; private int insetWidth = 10 ; private int insetHeight = 30 ; //监听player的相关事件 public void controllerUpdate(ControllerEvent ce) { if (ce instanceof RealizeCompleteEvent) { //player实例化完成后进行player播放前预处理 player.prefetch(); } else if (ce instanceof PrefetchCompleteEvent) { if (visual != null ) return ; //取得player中的播放视频的组件,并得到视频窗口的大小 //然后把视频窗口的组件添加到Frame窗口中, if ((visual = player.getVisualComponent()) != null ) { Dimension size = visual.getPreferredSize(); videoWidth = size.width; videoHeight = size.height; f.add(visual); } else { videoWidth = 320 ; } //取得player中的视频播放控制条组件,并把该组件添加到Frame窗口中 if ((control = player.getControlPanelComponent()) != null ) { controlHeight = control.getPreferredSize().height; f.add(control, BorderLayout.SOUTH); } //设定Frame窗口的大小,使得满足视频文件的默认大小 f.setSize(videoWidth + insetWidth, videoHeight + controlHeight + insetHeight); f.validate(); //启动视频播放组件开始播放 player.start(); mediaPlayer.start(); } else if (ce instanceof EndOfMediaEvent) { //当播放视频完成后,把时间进度条恢复到开始,并再次重新开始播放 player.setMediaTime( new Time( 0 )); player.start(); } } } |
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。