JMF示例(一)

本文介绍了一个使用Java Applet实现的简单多媒体播放器,该播放器能够连续循环播放指定的媒体文件,并通过监听媒体事件来控制播放行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!--<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;

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
intHALF=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);
}
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值