Java2实用教程(第二版)程序代码——第二十四章 Java与多媒体
1
//例子1
2
import java.applet.*;import java.awt.*;
3
import java.awt.event.*;
4
public class Example24_1 extends Applet implements ActionListener
5
{ AudioClip clip;//声明一个音频对象
6
Button button_play,button_loop,button_stop;
7
public void init()
8
{ clip=getAudioClip(getCodeBase(),"1.au");
9
//根据程序所在的地址处的声音文件1.au创建音频对象,Applet类的
10
// getCodeBase() 方法可以获得小程序所在的html页面的URL地址。
11
button_play=new Button("开始播放");button_loop=new Button("循环播放");
12
button_stop=new Button("停止播放");button_play.addActionListener(this);
13
button_stop.addActionListener(this);button_loop.addActionListener(this);
14
add(button_play);add(button_loop);add(button_stop);
15
}
16
public void stop()
17
{ clip.stop();//当离开此页面时停止播放。
18
}
19
public void actionPerformed(ActionEvent e)
20
{ if(e.getSource()==button_play)
21
{ clip.play();}
22
else if(e.getSource()==button_loop)
23
{ clip.loop();}
24
if(e.getSource()==button_stop)
25
{ clip.stop();}
26
}
27
}
28![]()
29
//例子2
30
import java.applet.*;import java.awt.*;import java.awt.event.*;
31
public class Example24_2 extends Applet implements ActionListener,Runnable,ItemListener
32
{ AudioClip clip;//声明一个音频对象。
33
Choice choice;
34
TextField text;
35
Thread thread;
36
String item=null;
37
Button button_play,button_loop,button_stop;
38
public void init()
39
{ choice=new Choice();
40
thread=new Thread(this);
41
int N=Integer.parseInt(getParameter("总数"));
42
for(int i=1;i<=N;i++)
43
{ choice.add(getParameter(String.valueOf(i)));
44
}
45
button_play=new Button("开始播放");
46
button_loop=new Button("循环播放");
47
button_stop=new Button("停止播放");
48
text=new TextField(12);
49
button_play.addActionListener(this);
50
button_stop.addActionListener(this);
51
button_loop.addActionListener(this);
52
choice.addItemListener(this);
53
add(choice);
54
add(button_play);add(button_loop);add(button_stop); add(text);
55
button_play.setEnabled(false);
56
button_loop.setEnabled(false);
57
}
58
public void itemStateChanged(ItemEvent e)
59
{ item=choice.getSelectedItem();
60
int index=item.indexOf(":");
61
item=item.substring(index+1).trim();
62
if(!(thread.isAlive()))
63
{ thread=new Thread(this);
64
}
65
try {
66
thread.start();
67
}
68
catch(Exception exp)
69
{ text.setText("正在下载音频文件");
70
}
71
}
72
public void stop()
73
{ clip.stop();
74
}
75
public void actionPerformed(ActionEvent e)
76
{ if(e.getSource()==button_play)
77
{ clip.play();
78
}
79
else if(e.getSource()==button_loop)
80
{ clip.loop();
81
}
82
else if(e.getSource()==button_stop)
83
{ clip.stop();
84
button_play.setEnabled(false);
85
button_loop.setEnabled(false);
86
}
87
}
88
public void run()
89
{
90
clip=getAudioClip(getCodeBase(),item);
91
text.setText("请稍等
");
92
if(clip!=null)
93
{ button_play.setEnabled(true);
94
button_loop.setEnabled(true);
95
text.setText("您可以播放了");
96
}
97
}
98
}
99![]()
100
//例子3
101
import java.applet.*;import java.awt.*;
102
import java.net.*;import java.awt.event.*;
103
import java.io.*;import javax.media.*;
104
public class Example24_3 extends Applet
105
implements ControllerListener,Runnable,ItemListener
106
{ Player player;
107
String str;
108
Thread mythread;
109
Choice choice;
110
Component visualComponent,controlComponent,progressBar;
111
String mediaFile;
112
URL mediaURL,codeBase;
113
Frame frame;
114
public void init()
115
{ str="Music01.MPG";
116
mythread=new Thread(this);
117
choice=new Choice();
118
choice.add("Music01.MPG");
119
choice.add("Music02.avi");
120
choice.add("Music03.avi");
121
choice.addItemListener(this);
122
codeBase=getDocumentBase();
123
frame=new Frame("视频系统");
124
frame.setSize(660,580);
125
frame.addWindowListener(new WindowAdapter()
126
{ public void windowClosing(WindowEvent e)
127
{ if(player!=null)
128
{ player.stop();player.deallocate();
129
}
130
frame.setVisible(false);
131
System.exit(0);
132
}
133
});
134
add(choice);
135
}
136
public void stop()
137
{ if(player!=null)
138
{ player.stop();
139
}
140
}
141
public synchronized void controllerUpdate(ControllerEvent event)
142
{ player.getDuration();
143
if(event instanceof RealizeCompleteEvent)
144
{ if((visualComponent=player.getVisualComponent())!=null)
145
frame.add("Center",visualComponent);
146
if((controlComponent=player.getControlPanelComponent())!=null)
147
if(visualComponent!=null)
148
frame.add("South",controlComponent);
149
else
150
frame.add( "Center",controlComponent);
151
frame.validate();
152
frame.pack();
153
}
154
else if(event instanceof PrefetchCompleteEvent)
155
{ player.start();
156
}
157
}
158
public void itemStateChanged(ItemEvent e)
159
{
160
str=choice.getSelectedItem();
161
if(player==null)
162
{
163
}
164
else
165
{ player.stop();player.deallocate();
166
}
167
frame.removeAll();
168
frame.setVisible(true);
169
frame.setBounds(300,100,150,100);
170
frame.validate();
171
if(!(mythread.isAlive()))
172
{ mythread=new Thread(this);
173
}
174
try{
175
mythread.start();
176
}
177
catch(Exception ee)
178
{
179
}
180
}
181
public synchronized void run()
182
{ try{ mediaURL=new URL(codeBase,str);
183
player=Manager.createPlayer(mediaURL);player.getDuration();
184
if(player!=null)
185
{ player.addControllerListener(this);
186
}
187
else
188
System.out.println("failed to creat player for"+mediaURL);
189
}
190
catch(MalformedURLException e)
191
{ System.out.println("URL for"+mediaFile+"is invalid");
192
}
193
catch(IOException e)
194
{ System.out.println("URL for"+mediaFile+"is invalid");
195
}
196
catch(NoPlayerException e)
197
{ System.out.println("canot find a player for"+mediaURL);
198
}
199
if(player!=null)
200
{ player.prefetch();
201
}
202
}
203
}
204![]()

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

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204
