`
dato0123
  • 浏览: 913938 次
文章分类
社区版块
存档分类
最新评论

JMF示例(一)

 
阅读更多
<!--<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);
}
}

}


分享到:
评论

相关推荐

    java的JMF播放音乐示例

    可以搜索E盘里所有的MP3文件,将其播放,只要在控制台输入数字,就可以播放音乐,是学习java的JMF的简单示例。代码可运行。

    JMF摄像头视频采集示例

    给学员写的一个JMF的例子,能捕获本机摄像头的视频并在窗体口中显示。 严格来说还不完整,因为原本打算实现网络传输的,后来改成保存到磁盘文件了。呵呵。

    Jmf捕获摄像头的示例

    Jmf捕获摄像头的示例

    利用JMF进行多媒体编程

    Java多媒体框架(JMF)中包含了许多用于...它是一个相当复杂的系统,完全了解这个系统可能需要花上几周的时间,但是这篇文章将主要介绍JMF的几个核心接口和类,然后通过一个简单的例子向你展示如何利用该接口进行编程。

    Java编写多个爬虫实例

    VApplet和vid2jpg JMF示例 pageSim 计算网页相似度 SST 计算网站风格树 cobra 基于视觉的网页分块算法 HTIS HTIS算法 PageRank PageRank算法 Link 链接 WebGraph Web图建模 WebGraphMemory 内存Web图 Synonym 同义词...

    JMF_Fina_2020

    这是基于R Markdown和bookdown ( )的书籍的最小示例。 请参阅上的“”页面,以了解如何将此示例编译为HTML。 您可以通过调用bookdown::render_book('index.Rmd', 'bookdown::pdf_book')生成bookdown::pdf_book格式...

    Java开发技术大全 电子版

    1.3一个简单的Java应用程序14 1.4一个简单的Java小程序16 1.5本章小结18 第2章Java语言基础19 2.1Java语言的特点19 2.2Java程序的构成21 2.3数据类 型23 2.3.1基本数据类型23 2.3.2常量25 2.3.3变量26 ...

    北大MOOC《Java程序设计》源码

    这是我在学习中国大学MOOC北京大学唐大仕老师的《Java程序设计》的过程中写的代码,其中,LearnJava里的代码是我个人写的,PKU-MOOC-JavaExample里有课程PDF和示例代码。 其中用到的Java库有:httpcomponents-...

    JAVA基础课程讲义

    第一章 JAVA入门 10 计算机语言发展史 10 机器语言 10 汇编语言 10 高级语言 10 其他高级语言 11 JAVA发展简史 12 JAVA为什么能够流行? 13 JAVA各版本的含义 13 JAVA技术体系架构 14 JAVA的特性和优势 14 JAVA应用...

    JimToMov(图片转视频)

    压缩包中包括使用示例,图片资源,JimToMov.jar,jmf-2_1_1e-alljava.zip框架jar包

    Java拍照功能

     保存图像的就不多说了,以下为示例代码 bufferedimage bi = (bufferedimage) createimage(imgwidth, imgheight); graphics2d g2 = bi.creategraphics(); g2.drawimage(img, null, null); fileoutputstream out = ...

    jsr80 java 访问 usb

    它是一个展示 jUSB API 应用的很好的示例程序。 尽管 usb.core.Host 对象的实现对于不同的操作系统是不同的,但是 Java 程序员只需要理解 usb.core 包就可以用 jUSB API 开始应用程序的开发。表 1 列出了 usb.core ...

    NyARToolkit:从 NyARToolkit-4.1.1 (http

    将网络摄像头捕获库( )与 NyARToolkit java3d utils(而不是 JMF)集成。 使用 webcam-capture 库的类包含在以下项目中。 NyARToolkit.utils.java3d.webcam-capture NyARToolkit.utils.webcam-capture ...

    Panic Player-开源

    使用 JMF 的操作系统独立音频播放器。 Panic Player 应该支持多种输入和输出格式。 可以使用其他示例文件来操作输入流。

    DistributedOptimizationWithLocalDomains:在纸上复制实验的代码

    JFC Mota,JMF Xavier,PMQ Aguiar,M.Püschel。 IEEE Transactions on Automatic Control,第1卷。 60,No.7,pp.2004-2009,2015。 , 主要算法是DADMM_Partial.m 。 所有代码都在Matlab中实现。 尽管所有算法都...

Global site tag (gtag.js) - Google Analytics