import java.awt.*;
import java.awt.event.*;
import java.lang.String;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import javax.media.*;
import com.sun.media.*;
/**
* This is a Java Applet that demonstrates how to create a simple
* media player with a media event listener. It will play the
* media clip right away and continuously loop.
*
* <!-- Sample HTML
* <applet code=TVApplet width=587 height=510>
* <param name=file value="sun.avi">
* </applet>
* -->
*/
public class TVApplet extends Applet implements ControllerListener
{
private String fileToPlay = "";//要播放的文件(完整的路径名称)
// media Player
Player player = null;//播放器
// component in which video is playing
Component visualComponent = null;//可视组件
// controls gain, position, start, stop
Component controlComponent = null;//控制组件
// displays progress during download
Component progressBar = null;//进度条
long CachingSize = 0L;
Panel panel = null;
Panel vPanel = null;
int controlPanelHeight = 0;
Image [] showmeImage = null;
Image [] zoomImageUp = null;
Image [] zoomImageDn = null;
CPanel cPanel = null;
ZoomButton zoomButton;
final int [] VLEFT = {59, 120};
final int [] VTOP = {33, 67};
final int [] VRIGHT = {59 + 175, 120 + 351};
final int [] VBOTTOM = {173, 67 + 287 + 11};
final int [] WIDTH = {176, 352};
final int [] HEIGHT = {138, 288};
final int HALF = 0;//小屏
final int FULL = 1;//全屏
int tSize = 0;
/**
* Read the applet file parameter and create the media
* player.
*/
public void init()
{
setLayout(null);
setBackground(Color.white);
cPanel = new CPanel( );
add(cPanel);
// Figure out what size to use
// The applet tag takes an optional parameter "SIZE" whose value
// can be "half" or "full"
// Eg. <param name=size value=half>
String szSize = "full" ;//getParameter("SIZE");
//屏幕一般是1024*768
if (Toolkit.getDefaultToolkit().getScreenSize().getWidth() > 800)
tSize = 1;
else
tSize = 0;
if (szSize != null)
{
if (szSize.toLowerCase().equals("full"))
tSize = 1;
else if (szSize.toLowerCase().equals("half"))
tSize = 0;
}
cPanel.setBounds(VLEFT[tSize], VTOP[tSize], WIDTH[tSize], HEIGHT[tSize]);//设置播放器上部分的窗口矩阵的位置
// Get the images
showmeImage = new Image[FULL + 1];
zoomImageUp = new Image[FULL + 1];
zoomImageDn = new Image[FULL + 1];
showmeImage[HALF] = getImage(getDocumentBase(), "ShowMeS2.jpg");
showmeImage[FULL] = getImage(getDocumentBase(), "ShowMeS.jpg");
zoomImageUp[HALF] = getImage(getDocumentBase(), "InUp.gif");
zoomImageDn[HALF] = getImage(getDocumentBase(), "InDn.gif");
zoomImageUp[FULL] = getImage(getDocumentBase(), "OutUp.gif");
zoomImageDn[FULL] = getImage(getDocumentBase(), "OutDn.gif");
addZoomButton();
// URL for our media file
URL url = null;
this.fileToPlay = "d://rr.mp3";
File fileToPlay = new File(this.fileToPlay);
try
{
// Create an url from the file name and the url to the
// document containing this applet.
if ((url = fileToPlay.toURL()) == null)
Fatal("Can't build URL for " + this.fileToPlay);
// Create an instance of a player for this media
try
{
player = Manager.createPlayer(url);//创建播放器
}
catch (NoPlayerException e)
{
System.out.println(e);
Fatal("Could not create player for " + url);
}
// Add ourselves as a listener for a player's events
player.addControllerListener(this);//为播放器事件增加监听者
}
catch (MalformedURLException e)
{
Fatal("Invalid media file URL!");
}
catch (IOException e)
{
Fatal("IO exception creating player for " + url);
}
}
/**
* Start media file playback. This function is called the
* first time that the Applet runs and every
* time the user re-enters the page.
*/
public void start()
{
if (player != null)
player.realize();
}
/**
* Stop media file playback and release resource before
* leaving the page.
*/
public void stop()
{
if (player != null)
{
player.stop();
player.deallocate();
}
}
public void destroy()
{
player.close();
}
public void paint(Graphics g)
{
if (showmeImage[tSize] != null)
g.drawImage(showmeImage[tSize], 0, 0, this);
super.paint(g);
}
public synchronized void reSize()
{
cPanel.setBounds(VLEFT[tSize], VTOP[tSize], WIDTH[tSize], HEIGHT[tSize]);
if (visualComponent != null) {
Dimension size = visualComponent.getPreferredSize();
int width = size.width;
int height = size.height;
while (true) {
// Scale to fit
if (width > WIDTH[tSize] || height > HEIGHT[tSize]) {
width /= 2;
height /= 2;
} else if (width < WIDTH[tSize] && height < HEIGHT[tSize]) {
if (width * 2 <= WIDTH[tSize] && height*2 <= HEIGHT[tSize]) {
width *= 2;
height *= 2;
} else
break;
} else
break;
}
visualComponent.setBounds((WIDTH[tSize] - width) / 2,
(HEIGHT[tSize] - height) / 2,
width, height);
}
if (controlComponent != null) {
controlComponent.setBounds(VLEFT[tSize], VBOTTOM[tSize],
WIDTH[tSize], 24);
controlComponent.invalidate();
}
remove(zoomButton);
addZoomButton();
repaint();
}
public void addZoomButton()
{
zoomButton = new ZoomButton(zoomImageUp[tSize], zoomImageDn[tSize], 1 - tSize);
add(zoomButton);
zoomButton.setBounds(showmeImage[tSize].getWidth(this) - 24, showmeImage[tSize].getHeight(this) - 24, 24, 24);
}
/**
* This controllerUpdate function must be defined in order to
* implement a ControllerListener interface. This
* function will be called whenever there is a media event
*/
public synchronized void controllerUpdate(ControllerEvent event)
{
// If we're getting messages from a dead player,
// just leave
if (player == null)
return;
// When the player is Realized, get the visual
// and control components and add them to the Applet
if (event instanceof RealizeCompleteEvent)
{
if (( visualComponent =
player.getVisualComponent())!= null) {
cPanel.add(visualComponent);
}
if (( controlComponent =
player.getControlPanelComponent()) != null) {
add(controlComponent);
controlComponent.setBounds(VLEFT[tSize], VBOTTOM[tSize],
WIDTH[tSize], 24);
controlComponent.invalidate();
controlComponent.repaint();
}
reSize();
player.prefetch();
} else if (event instanceof EndOfMediaEvent)
{
// We've reached the end of the media; rewind and
// start over
player.setMediaTime(new Time(0));
player.prefetch();
} else if (event instanceof ControllerErrorEvent) {
player = null;
Fatal(((ControllerErrorEvent)event).getMessage());
} else if (event instanceof PrefetchCompleteEvent) {
if (visualComponent != null) {
reSize();
}
player.start();
} else if (event instanceof SizeChangeEvent) {
reSize();
}
}
void Fatal (String s)
{
// Applications will make various choices about what
// to do here. We print a message and then exit
System.err.println("FATAL ERROR: " + s);
throw new Error(s); // Invoke the uncaught exception
// handler System.exit() is another
// choice.
}
/*************************************************************************
* INNER CLASSES
*************************************************************************/
public class CPanel extends Panel
{
public CPanel()
{
super();
setBackground(Color.black);
setLayout( null );
}
}
public class ZoomButton extends Component
{
Image up, down;
int newSize;
boolean mouseUp = true;
public ZoomButton(Image up, Image down, int newSize)
{
this.up = up;
this.down = down;
this.newSize = newSize;
setSize(24, 24);
addMouseListener( new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
mouseUp = false;
repaint();
}
public synchronized void mouseReleased(MouseEvent me)
{
mouseUp = true;
TVApplet.this.tSize = ZoomButton.this.newSize;
reSize();
}
} );
}
public void paint(Graphics g)
{
if (mouseUp)
g.drawImage(up, 0, 0, this);
else
g.drawImage(down, 0, 0, this);
}
}
}