<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->importjava.applet.Applet;
importjava.awt.*;
importjava.awt.event.*;
importjava.lang.String;
importjava.net.URL;
importjava.net.MalformedURLException;
importjava.io.File;
importjava.io.IOException;
importjava.util.Properties;
importjavax.media.*;
importcom.sun.media.*;
/**
*ThisisaJavaAppletthatdemonstrateshowtocreateasimple
*mediaplayerwithamediaeventlistener.Itwillplaythe
*mediacliprightawayandcontinuouslyloop.
*
*<!--SampleHTML
*<appletcode=TVAppletwidth=587height=510>
*<paramname=filevalue="sun.avi">
*</applet>
*-->
*/
publicclassTVAppletextendsAppletimplementsControllerListener
{
privateStringfileToPlay="";//要播放的文件(完整的路径名称)
//mediaPlayer
Playerplayer=null;//播放器
//componentinwhichvideoisplaying
ComponentvisualComponent=null;//可视组件
//controlsgain,position,start,stop
ComponentcontrolComponent=null;//控制组件
//displaysprogressduringdownload
ComponentprogressBar=null;//进度条
longCachingSize=0L;
Panelpanel=null;
PanelvPanel=null;
intcontrolPanelHeight=0;
Image[]showmeImage=null;
Image[]zoomImageUp=null;
Image[]zoomImageDn=null;
CPanelcPanel=null;
ZoomButtonzoomButton;
finalint[]VLEFT={59,120};
finalint[]VTOP={33,67};
finalint[]VRIGHT={59+175,120+351};
finalint[]VBOTTOM={173,67+287+11};
finalint[]WIDTH={176,352};
finalint[]HEIGHT={138,288};
finalintHALF=0;//小屏
finalintFULL=1;//全屏
inttSize=0;
/**
*Readtheappletfileparameterandcreatethemedia
*player.
*/
publicvoidinit()
{
setLayout(null);
setBackground(Color.white);
cPanel=newCPanel();
add(cPanel);
//Figureoutwhatsizetouse
//Theapplettagtakesanoptionalparameter"SIZE"whosevalue
//canbe"half"or"full"
//Eg.<paramname=sizevalue=half>
StringszSize="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;
elseif(szSize.toLowerCase().equals("half"))
tSize=0;
}
cPanel.setBounds(VLEFT[tSize],VTOP[tSize],WIDTH[tSize],HEIGHT[tSize]);//设置播放器上部分的窗口矩阵的位置
//Gettheimages
showmeImage=newImage[FULL+1];
zoomImageUp=newImage[FULL+1];
zoomImageDn=newImage[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();
//URLforourmediafile
URLurl=null;
this.fileToPlay="d://rr.mp3";
FilefileToPlay=newFile(this.fileToPlay);
try
{
//Createanurlfromthefilenameandtheurltothe
//documentcontainingthisapplet.
if((url=fileToPlay.toURL())==null)
Fatal("Can'tbuildURLfor"+this.fileToPlay);
//Createaninstanceofaplayerforthismedia
try
{
player=Manager.createPlayer(url);//创建播放器
}
catch(NoPlayerExceptione)
{
System.out.println(e);
Fatal("Couldnotcreateplayerfor"+url);
}
//Addourselvesasalistenerforaplayer'sevents
player.addControllerListener(this);//为播放器事件增加监听者
}
catch(MalformedURLExceptione)
{
Fatal("InvalidmediafileURL!");
}
catch(IOExceptione)
{
Fatal("IOexceptioncreatingplayerfor"+url);
}
}
/**
*Startmediafileplayback.Thisfunctioniscalledthe
*firsttimethattheAppletrunsandevery
*timetheuserre-entersthepage.
*/
publicvoidstart()
{
if(player!=null)
player.realize();
}
/**
*Stopmediafileplaybackandreleaseresourcebefore
*leavingthepage.
*/
publicvoidstop()
{
if(player!=null)
{
player.stop();
player.deallocate();
}
}
publicvoiddestroy()
{
player.close();
}
publicvoidpaint(Graphicsg)
{
if(showmeImage[tSize]!=null)
g.drawImage(showmeImage[tSize],0,0,this);
super.paint(g);
}
publicsynchronizedvoidreSize()
{
cPanel.setBounds(VLEFT[tSize],VTOP[tSize],WIDTH[tSize],HEIGHT[tSize]);
if(visualComponent!=null){
Dimensionsize=visualComponent.getPreferredSize();
intwidth=size.width;
intheight=size.height;
while(true){
//Scaletofit
if(width>WIDTH[tSize]||height>HEIGHT[tSize]){
width/=2;
height/=2;
}elseif(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();
}
publicvoidaddZoomButton()
{
zoomButton=newZoomButton(zoomImageUp[tSize],zoomImageDn[tSize],1-tSize);
add(zoomButton);
zoomButton.setBounds(showmeImage[tSize].getWidth(this)-24,showmeImage[tSize].getHeight(this)-24,24,24);
}
/**
*ThiscontrollerUpdatefunctionmustbedefinedinorderto
*implementaControllerListenerinterface.This
*functionwillbecalledwheneverthereisamediaevent
*/
publicsynchronizedvoidcontrollerUpdate(ControllerEventevent)
{
//Ifwe'regettingmessagesfromadeadplayer,
//justleave
if(player==null)
return;
//WhentheplayerisRealized,getthevisual
//andcontrolcomponentsandaddthemtotheApplet
if(eventinstanceofRealizeCompleteEvent)
{
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();
}elseif(eventinstanceofEndOfMediaEvent)
{
//We'vereachedtheendofthemedia;rewindand
//startover
player.setMediaTime(newTime(0));
player.prefetch();
}elseif(eventinstanceofControllerErrorEvent){
player=null;
Fatal(((ControllerErrorEvent)event).getMessage());
}elseif(eventinstanceofPrefetchCompleteEvent){
if(visualComponent!=null){
reSize();
}
player.start();
}elseif(eventinstanceofSizeChangeEvent){
reSize();
}
}
voidFatal(Strings)
{
//Applicationswillmakevariouschoicesaboutwhat
//todohere.Weprintamessageandthenexit
System.err.println("FATALERROR:"+s);
thrownewError(s);//Invoketheuncaughtexception
//handlerSystem.exit()isanother
//choice.
}
/*************************************************************************
*INNERCLASSES
*************************************************************************/
publicclassCPanelextendsPanel
{
publicCPanel()
{
super();
setBackground(Color.black);
setLayout(null);
}
}
publicclassZoomButtonextendsComponent
{
Imageup,down;
intnewSize;
booleanmouseUp=true;
publicZoomButton(Imageup,Imagedown,intnewSize)
{
this.up=up;
this.down=down;
this.newSize=newSize;
setSize(24,24);
addMouseListener(newMouseAdapter()
{
publicvoidmousePressed(MouseEventme)
{
mouseUp=false;
repaint();
}
publicsynchronizedvoidmouseReleased(MouseEventme)
{
mouseUp=true;
TVApplet.this.tSize=ZoomButton.this.newSize;
reSize();
}
});
}
publicvoidpaint(Graphicsg)
{
if(mouseUp)
g.drawImage(up,0,0,this);
else
g.drawImage(down,0,0,this);
}
}
}
importjava.awt.*;
importjava.awt.event.*;
importjava.lang.String;
importjava.net.URL;
importjava.net.MalformedURLException;
importjava.io.File;
importjava.io.IOException;
importjava.util.Properties;
importjavax.media.*;
importcom.sun.media.*;
/**
*ThisisaJavaAppletthatdemonstrateshowtocreateasimple
*mediaplayerwithamediaeventlistener.Itwillplaythe
*mediacliprightawayandcontinuouslyloop.
*
*<!--SampleHTML
*<appletcode=TVAppletwidth=587height=510>
*<paramname=filevalue="sun.avi">
*</applet>
*-->
*/
publicclassTVAppletextendsAppletimplementsControllerListener
{
privateStringfileToPlay="";//要播放的文件(完整的路径名称)
//mediaPlayer
Playerplayer=null;//播放器
//componentinwhichvideoisplaying
ComponentvisualComponent=null;//可视组件
//controlsgain,position,start,stop
ComponentcontrolComponent=null;//控制组件
//displaysprogressduringdownload
ComponentprogressBar=null;//进度条
longCachingSize=0L;
Panelpanel=null;
PanelvPanel=null;
intcontrolPanelHeight=0;
Image[]showmeImage=null;
Image[]zoomImageUp=null;
Image[]zoomImageDn=null;
CPanelcPanel=null;
ZoomButtonzoomButton;
finalint[]VLEFT={59,120};
finalint[]VTOP={33,67};
finalint[]VRIGHT={59+175,120+351};
finalint[]VBOTTOM={173,67+287+11};
finalint[]WIDTH={176,352};
finalint[]HEIGHT={138,288};
finalintHALF=0;//小屏
finalintFULL=1;//全屏
inttSize=0;
/**
*Readtheappletfileparameterandcreatethemedia
*player.
*/
publicvoidinit()
{
setLayout(null);
setBackground(Color.white);
cPanel=newCPanel();
add(cPanel);
//Figureoutwhatsizetouse
//Theapplettagtakesanoptionalparameter"SIZE"whosevalue
//canbe"half"or"full"
//Eg.<paramname=sizevalue=half>
StringszSize="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;
elseif(szSize.toLowerCase().equals("half"))
tSize=0;
}
cPanel.setBounds(VLEFT[tSize],VTOP[tSize],WIDTH[tSize],HEIGHT[tSize]);//设置播放器上部分的窗口矩阵的位置
//Gettheimages
showmeImage=newImage[FULL+1];
zoomImageUp=newImage[FULL+1];
zoomImageDn=newImage[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();
//URLforourmediafile
URLurl=null;
this.fileToPlay="d://rr.mp3";
FilefileToPlay=newFile(this.fileToPlay);
try
{
//Createanurlfromthefilenameandtheurltothe
//documentcontainingthisapplet.
if((url=fileToPlay.toURL())==null)
Fatal("Can'tbuildURLfor"+this.fileToPlay);
//Createaninstanceofaplayerforthismedia
try
{
player=Manager.createPlayer(url);//创建播放器
}
catch(NoPlayerExceptione)
{
System.out.println(e);
Fatal("Couldnotcreateplayerfor"+url);
}
//Addourselvesasalistenerforaplayer'sevents
player.addControllerListener(this);//为播放器事件增加监听者
}
catch(MalformedURLExceptione)
{
Fatal("InvalidmediafileURL!");
}
catch(IOExceptione)
{
Fatal("IOexceptioncreatingplayerfor"+url);
}
}
/**
*Startmediafileplayback.Thisfunctioniscalledthe
*firsttimethattheAppletrunsandevery
*timetheuserre-entersthepage.
*/
publicvoidstart()
{
if(player!=null)
player.realize();
}
/**
*Stopmediafileplaybackandreleaseresourcebefore
*leavingthepage.
*/
publicvoidstop()
{
if(player!=null)
{
player.stop();
player.deallocate();
}
}
publicvoiddestroy()
{
player.close();
}
publicvoidpaint(Graphicsg)
{
if(showmeImage[tSize]!=null)
g.drawImage(showmeImage[tSize],0,0,this);
super.paint(g);
}
publicsynchronizedvoidreSize()
{
cPanel.setBounds(VLEFT[tSize],VTOP[tSize],WIDTH[tSize],HEIGHT[tSize]);
if(visualComponent!=null){
Dimensionsize=visualComponent.getPreferredSize();
intwidth=size.width;
intheight=size.height;
while(true){
//Scaletofit
if(width>WIDTH[tSize]||height>HEIGHT[tSize]){
width/=2;
height/=2;
}elseif(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();
}
publicvoidaddZoomButton()
{
zoomButton=newZoomButton(zoomImageUp[tSize],zoomImageDn[tSize],1-tSize);
add(zoomButton);
zoomButton.setBounds(showmeImage[tSize].getWidth(this)-24,showmeImage[tSize].getHeight(this)-24,24,24);
}
/**
*ThiscontrollerUpdatefunctionmustbedefinedinorderto
*implementaControllerListenerinterface.This
*functionwillbecalledwheneverthereisamediaevent
*/
publicsynchronizedvoidcontrollerUpdate(ControllerEventevent)
{
//Ifwe'regettingmessagesfromadeadplayer,
//justleave
if(player==null)
return;
//WhentheplayerisRealized,getthevisual
//andcontrolcomponentsandaddthemtotheApplet
if(eventinstanceofRealizeCompleteEvent)
{
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();
}elseif(eventinstanceofEndOfMediaEvent)
{
//We'vereachedtheendofthemedia;rewindand
//startover
player.setMediaTime(newTime(0));
player.prefetch();
}elseif(eventinstanceofControllerErrorEvent){
player=null;
Fatal(((ControllerErrorEvent)event).getMessage());
}elseif(eventinstanceofPrefetchCompleteEvent){
if(visualComponent!=null){
reSize();
}
player.start();
}elseif(eventinstanceofSizeChangeEvent){
reSize();
}
}
voidFatal(Strings)
{
//Applicationswillmakevariouschoicesaboutwhat
//todohere.Weprintamessageandthenexit
System.err.println("FATALERROR:"+s);
thrownewError(s);//Invoketheuncaughtexception
//handlerSystem.exit()isanother
//choice.
}
/*************************************************************************
*INNERCLASSES
*************************************************************************/
publicclassCPanelextendsPanel
{
publicCPanel()
{
super();
setBackground(Color.black);
setLayout(null);
}
}
publicclassZoomButtonextendsComponent
{
Imageup,down;
intnewSize;
booleanmouseUp=true;
publicZoomButton(Imageup,Imagedown,intnewSize)
{
this.up=up;
this.down=down;
this.newSize=newSize;
setSize(24,24);
addMouseListener(newMouseAdapter()
{
publicvoidmousePressed(MouseEventme)
{
mouseUp=false;
repaint();
}
publicsynchronizedvoidmouseReleased(MouseEventme)
{
mouseUp=true;
TVApplet.this.tSize=ZoomButton.this.newSize;
reSize();
}
});
}
publicvoidpaint(Graphicsg)
{
if(mouseUp)
g.drawImage(up,0,0,this);
else
g.drawImage(down,0,0,this);
}
}
}