modifications to handle url in place of file and to be used with SmartQT4JApplet

This commit is contained in:
michel_jacobson 2002-11-28 16:16:27 +00:00
parent 178ffcb800
commit 3d215caf53

View file

@ -1,17 +1,17 @@
/* /*
The contents of this file are subject to the THDL Open Community License The contents of this file are subject to the THDL Open Community License
Version 1.0 (the "License"); you may not use this file except in compliance Version 1.0 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License on the THDL web site with the License. You may obtain a copy of the License on the THDL web site
(http://www.thdl.org/). (http://www.thdl.org/).
Software distributed under the License is distributed on an "AS IS" basis, Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific terms governing rights and limitations under the License for the specific terms governing rights and limitations under the
License. License.
The Initial Developer of this software is the Tibetan and Himalayan Digital The Initial Developer of this software is the Tibetan and Himalayan Digital
Library (THDL). Portions created by the THDL are Copyright 2001 THDL. Library (THDL). Portions created by the THDL are Copyright 2001 THDL.
All Rights Reserved. All Rights Reserved.
Contributor(s): ______________________________________. Contributor(s): ______________________________________.
*/ */
@ -31,29 +31,39 @@ import quicktime.std.movies.media.DataRef;
import quicktime.io.*; import quicktime.io.*;
import quicktime.app.time.*; import quicktime.app.time.*;
import quicktime.std.clocks.*; import quicktime.std.clocks.*;
import quicktime.app.QTFactory;
public class SmartQT4JPlayer extends SmartMoviePanel public class SmartQT4JPlayer extends SmartMoviePanel {
{
//attributes
private QTCanvas canvas; //this hands over screen real estate to
//native graphics that quicktime can control
private QTPlayer player; //this is the the client for the canvas
//In my app it will get set to a
// quicktime.app.player object
private Movie movie; //this is the content used in
//QTDrawable player's quicktime.app.player constructor
private MovieController controller; private myJumpCallBack theJumpper = null;
private myRateCallBack theRater = null;
private RateCallBack rateCallBack = null; private URL mediaUrl = null;
private TimeJumpCallBack jumpCallBack = null; private TimeBase myMoviesTimeBase;
private QTCanvas canvas;
private QTPlayer player;
private TimeBaseTimeCallBack theMoviesTimeCallback; //constructor
private TimeBaseTimeCallBackStopper theStopper; public SmartQT4JPlayer(Container cont, URL mediaURL) {
super( new GridLayout() );
try {
loadMovie(mediaURL);
} catch (SmartMoviePanelException smpe) {
smpe.printStackTrace();
}
}
//destructor
public void destroy() {
if (theJumpper != null)
theJumpper.cancelAndCleanup();
if(theRater != null)
theRater.cancelAndCleanup();
private URL mediaUrl = null; removeAllAnnotationPlayers();
QTSession.close();
removeAll();
mediaUrl = null;
System.out.println("Clean up performed.");
}
//accessors //accessors
public String getIdentifyingName() { public String getIdentifyingName() {
@ -64,354 +74,211 @@ public class SmartQT4JPlayer extends SmartMoviePanel
} }
public void setParentContainer(Container c) { public void setParentContainer(Container c) {
} }
public void setPlayer(QTPlayer player) public void setPlayer(QTDrawable player) {
{ this.player = (QTPlayer)player;
this.player = player;
} }
public QTPlayer getPlayer() public QTPlayer getPlayer() {
{
return player; return player;
} }
public void setCanvas(QTCanvas canvas) {
public void setMovie(Movie movie)
{
this.movie = movie;
}
public void setMovie(URL movieURL)
{
try
{
DataRef movieDataRef = new DataRef( "file://" + movieURL.getFile() );
Movie m = new Movie();
m = m.fromDataRef( movieDataRef, StdQTConstants.newMovieActive );
setMovie(m);
getMovie().setTimeScale(1000);
mediaUrl = movieURL;
}
catch(QTException qte)
{
qte.printStackTrace();
System.out.println( movieURL.toString() );
}
}
public Movie getMovie()
{
return movie;
}
public void setController(MovieController controller)
{
this.controller = controller;
}
public MovieController getController()
{
return controller;
}
public void setCanvas(QTCanvas canvas)
{
this.canvas = canvas; this.canvas = canvas;
} }
public QTCanvas getCanvas() public QTCanvas getCanvas() {
{
return canvas; return canvas;
} }
//contract methods - initialize //contract methods - initialize
public void displayBorders(boolean borders) throws SmartMoviePanelException public void displayBorders(boolean borders) throws SmartMoviePanelException {
{
} }
public void displayController(boolean controller) throws SmartMoviePanelException public void displayController(boolean controller) throws SmartMoviePanelException {
{
} }
public void loadMovie(URL mediaURL) throws SmartMoviePanelException
{ public void loadMovie(URL mediaURL) throws SmartMoviePanelException {
//Initialize a QT session and add a test image //Initialize a QT session and add a test image
//These three try/catch blocks come from PlayMovie.java copyright //These three try/catch blocks come from PlayMovie.java copyright
// Apple Co. I'm using it to test that QT and QT4Java exist // Apple Co. I'm using it to test that QT and QT4Java exist
try try {
{
if (QTSession.isInitialized() == false) { if (QTSession.isInitialized() == false) {
QTSession.open(); QTSession.open();
try try {
{
setCanvas( new QTCanvas(QTCanvas.kInitialSize, 0.5F, 0.5F) ); setCanvas( new QTCanvas(QTCanvas.kInitialSize, 0.5F, 0.5F) );
this.add( getCanvas() ); this.add( getCanvas() );
getCanvas().setClient(ImageDrawer.getQTLogo(), true); getCanvas().setClient(ImageDrawer.getQTLogo(), true);
} } catch(QTException qte) {
catch(QTException qte)
{
qte.printStackTrace(); qte.printStackTrace();
} }
} }
} } catch (NoClassDefFoundError er) {
catch (NoClassDefFoundError er)
{
add (new Label ("Can't Find QTJava classes"), "North"); add (new Label ("Can't Find QTJava classes"), "North");
add (new Label ("Check install and try again"), "South"); add (new Label ("Check install and try again"), "South");
} } catch (SecurityException se) {
catch (SecurityException se)
{
// this is thrown by MRJ trying to find QTSession class // this is thrown by MRJ trying to find QTSession class
add (new Label ("Can't Find QTJava classes"), "North"); add (new Label ("Can't Find QTJava classes"), "North");
add (new Label ("Check install and try again"), "South"); add (new Label ("Check install and try again"), "South");
} } catch (Exception e) {
catch (Exception e) // do a dynamic test for QTException
{
// do a dynamic test for QTException
//so the QTException class is not loaded unless //so the QTException class is not loaded unless
// an unknown exception is thrown by the runtime // an unknown exception is thrown by the runtime
if (e instanceof ClassNotFoundException || e instanceof java.io.FileNotFoundException) if (e instanceof ClassNotFoundException || e instanceof java.io.FileNotFoundException) {
{
add (new Label ("Can't Find QTJava classes"), "North"); add (new Label ("Can't Find QTJava classes"), "North");
add (new Label ("Check install and try again"), "South"); add (new Label ("Check install and try again"), "South");
} } else if (e instanceof QTException) {
else if (e instanceof QTException)
{
add (new Label ("Problem with QuickTime install"), "North"); add (new Label ("Problem with QuickTime install"), "North");
if (((QTException)e).errorCode() == -2093) if (((QTException)e).errorCode() == -2093)
add (new Label ("QuickTime must be installed"), "South"); add (new Label ("QuickTime must be installed"), "South");
else else
add (new Label (e.getMessage()), "South"); add (new Label (e.getMessage()), "South");
} }
} }
setMovie(mediaURL); try {
try this.remove( getCanvas() );
{ setPlayer(QTFactory.makeDrawable(mediaURL.toString()));
getCanvas().removeClient();
setController( new MovieController( getMovie() ));
getController().setKeysEnabled(true);
setPlayer(new QTPlayer( getController() ));
getCanvas().setClient( getPlayer(), true ); getCanvas().setClient( getPlayer(), true );
this.add( getCanvas() ); this.add( getCanvas() );
TimeBase theMoviesTimeBase = getMovie().getTimeBase(); System.out.println("loadMovie:"+mediaURL.toString());
// this callback is triggered when the rate of the movie changes (0 is stopped, >0 is playing)
rateCallBack = new RateCallBack(theMoviesTimeBase, 0, StdQTConstants.triggerRateChange) {
public void execute() {
System.out.println("Rate changed to: " + String.valueOf(rateWhenCalled));
if (rateWhenCalled > 0)
launchAnnotationTimer();
else
cancelAnnotationTimer();
schedule(this);
}
};
schedule(rateCallBack);
// this callback is triggered when there is a jump in the timebase, ie when the slider control is adjusted myMoviesTimeBase = getPlayer().getTimeBase();
jumpCallBack = new TimeJumpCallBack(theMoviesTimeBase) { theJumpper = new myJumpCallBack(myMoviesTimeBase);
public void execute() { theJumpper.callMeWhen();
System.out.println("Time Jump. New rate: " + String.valueOf(rateWhenCalled)); theRater = new myRateCallBack(myMoviesTimeBase, 0, StdQTConstants.triggerRateChange);
if (rateWhenCalled > 0) theRater.callMeWhen();
launchAnnotationTimer(); Timer timer = new Timer(10,1,new Tickler(), getPlayer().getMovieController().getMovie());
schedule(this); timer.setActive(true);
} } catch(QTException qte) {
}; System.out.println("loadMovie failed");
schedule(jumpCallBack);
}
catch(QTException qte)
{
qte.printStackTrace(); qte.printStackTrace();
} }
} }
private void schedule(QTCallBack callBack) {
try {
callBack.callMeWhen();
} catch (StdQTException stdqte) {
stdqte.printStackTrace();
}
}
//contract methods - control media //contract methods - control media
public void cmd_playOn() throws SmartMoviePanelException public void cmd_playOn() throws SmartMoviePanelException {
{ try {
try
{
getPlayer().setRate(1.0F); getPlayer().setRate(1.0F);
} } catch(QTException qte) {
catch(QTException qte)
{
qte.printStackTrace(); qte.printStackTrace();
} }
} }
public void cmd_playSegment(Integer startTime, Integer stopTime) throws SmartMoviePanelException public void cmd_playSegment(Integer startTime, Integer stopTime) throws SmartMoviePanelException {
{ try {
try int myScale = getPlayer().getScale();
{ cmd_stop();
getPlayer().setRate(0); getPlayer().setTime( (startTime.intValue() * myScale) / 1000 );
getPlayer().setTime( startTime.intValue() );
int value; if (stopTime == null) {
if (stopTime == null) myMoviesTimeBase.setStopTime(new TimeRecord(myScale, getEndTime()));
value=getEndTime(); //System.out.println("startTime:"+(startTime.intValue()*myScale)/1000+" stopTime: to the End" );
else } else {
value=stopTime.intValue(); myMoviesTimeBase.setStopTime(new TimeRecord(myScale, (stopTime.intValue()*myScale)/1000));
//System.out.println("startTime:"+(startTime.intValue()*myScale)/1000+" stopTime:"+(stopTime.intValue()*myScale)/1000 );
TimeBase theMoviesTimeBase = getPlayer().getTimeBase(); }
theStopper = new TimeBaseTimeCallBackStopper(theMoviesTimeBase, 1000, value, StdQTConstants.triggerTimeEither);
theStopper.callMeWhen();
cmd_playOn(); cmd_playOn();
/*
System.out.println("Set start time to " +startTime.intValue() );
if (stopTime != null)
System.out.println("Set stop time " +stopTime.intValue() );
System.out.println("Current time " +getPlayer().getTime() );
if (stopTime != null)
System.out.println("Time Stopper's stop trigger " +theStopper.getCallTime() );
System.out.println("Player Scale: " +getPlayer().getScale() );
System.out.println("Movie Scale: " +getMovie().getTimeScale() );
*/
} }
catch (SmartMoviePanelException smpe) {} catch (SmartMoviePanelException smpe) {
catch (StdQTException sqte) {} }
catch (QTException qte) {} catch (StdQTException sqte) {
}
catch (QTException qte) {
}
} }
public void cmd_stop() throws SmartMoviePanelException public void cmd_stop() throws SmartMoviePanelException {
{ try {
try
{
getPlayer().setRate(0.0F); getPlayer().setRate(0.0F);
} } catch(QTException qte) {
catch(QTException qte)
{
qte.printStackTrace(); qte.printStackTrace();
} }
} }
//contract methods - media status //contract methods - media status
public boolean isInitialized() { public boolean isInitialized() {
return true; //FIXME what should this do? return true; //FIXME what should this do?
} }
public boolean isPlaying() { public boolean isPlaying() {
try { try {
if (getMovie().getRate() > 0) if (player.getRate() > 0)
return true; return true;
} catch (StdQTException stdqte) { } catch (StdQTException stdqte) {
stdqte.printStackTrace(); stdqte.printStackTrace();
} }
return false; return false;
} }
public int getCurrentTime() //doit envoyer le temps en sec fois 1000
{ public int getCurrentTime() {
try { try {
return getMovie().getTime(); int myScale = getPlayer().getScale();
//System.out.println("getCurrentTime():"+(player.getTime()*1000)/myScale);
return (player.getTime()*1000)/myScale;
} catch (StdQTException stqte) { } catch (StdQTException stqte) {
stqte.printStackTrace(); stqte.printStackTrace();
return 0; return 0;
} } catch (QTException qte) {
} System.out.println("getCurrentTimeErr");
public int getEndTime() return 0;
{ }
try { }
return getMovie().getDuration(); public int getEndTime() {
} catch (StdQTException stqte) { try {
stqte.printStackTrace(); int myScale = getPlayer().getScale();
return (player.getDuration()*1000)/myScale;
} catch (StdQTException stqte) {
stqte.printStackTrace();
return 0;
} catch (QTException qte) {
System.out.println("getCurrentTimeErr");
return 0; return 0;
} }
} }
//helper methods - QT4J
public void destroy() public SmartQT4JPlayer() {
{
if (rateCallBack != null)
rateCallBack.cancelAndCleanup();
if (jumpCallBack != null)
jumpCallBack.cancelAndCleanup();
removeAllAnnotationPlayers();
QTSession.close();
removeAll();
mediaUrl = null;
System.out.println("Clean up performed.");
}
//constructor
public SmartQT4JPlayer(Container cont, URL mediaURL)
{
super( new GridLayout() ); super( new GridLayout() );
try {
loadMovie(mediaURL);
} catch (SmartMoviePanelException smpe) {
smpe.printStackTrace();
}
} }
public SmartQT4JPlayer()
{
super( new GridLayout() );
}
// inner classes // inner classes
class TimeBaseTimeCallBack extends quicktime.std.clocks.TimeCallBack class myRateCallBack extends quicktime.std.clocks.RateCallBack {
{ public myRateCallBack(TimeBase tb, int scale, int flag) throws QTException {
private int callTime; super(tb, scale, flag);
private TimeBase theTimeBase;
public void setCallTime(int time)
{
callTime=time;
value=callTime;
} }
public int getCallTime() public void execute() {
{
return callTime;
}
public void execute()
{
try
{
// System.out.println("--- TimeCallBack@triggerTimeEither--- called at:" + timeWhenCalledMsecs + "msecs");
cancel();
//reschedule
callMeWhen();
}
catch (StdQTException e)
{}
}
public TimeBaseTimeCallBack(TimeBase tb, int scale, int value, int flags) throws QTException
{
super(tb, scale, value, flags);
// setCallTime(value/1000);
setCallTime(value);
theTimeBase = tb;
}
}
class TimeBaseTimeCallBackStopper extends TimeBaseTimeCallBack
{
public void execute()
{
try { try {
/* //System.out.println("myRateCallBack: " + String.valueOf(rateWhenCalled));
System.out.println("---TimeCallBackStopper--- called at:" + timeWhenCalledMsecs + "msecs"); if (rateWhenCalled > 0)
System.out.println("---TimeCallBackStopper--- callTime is: " + getCallTime() + " msecs"); launchAnnotationTimer();
*/ else
cmd_stop(); cancelAnnotationTimer();
cancel();
/* callMeWhen();
System.out.println("---TimeCallBackStopper--- Player time is: " + getPlayer().getTime() + " msecs"); } catch (Exception e) {
System.out.println("---TimeCallBackStopper--- Movie time is: " + getMovie().getTime() + " msecs"); System.out.println("myRateCallBack err: "+e.getMessage());
*/ }
cancelAndCleanup();
//reschedule
//callMeWhen();
}
catch (SmartMoviePanelException smpe) {}
} }
public TimeBaseTimeCallBackStopper(TimeBase tb, int scale, int value, int flags) throws QTException
{
super(tb, scale, value, flags);
}
} }
} class myJumpCallBack extends quicktime.std.clocks.TimeJumpCallBack {
public myJumpCallBack(TimeBase tb) throws QTException {
super(tb);
}
public void execute() {
try {
//System.out.println("myJumpCallBack: " + String.valueOf(rateWhenCalled));
if (rateWhenCalled > 0)
launchAnnotationTimer();
cancel();
callMeWhen();
} catch (Exception e) {
System.out.println("myJumpCallBack err: "+e.getMessage());
}
}
}
public class Tickler implements Ticklish {
public void timeChanged(int newTime) throws QTException {
//System.out.println("*** TimerClass *** timeChanged at:"+newTime);
}
public boolean tickle(float er,int time) throws QTException {
//System.out.println("*** TimerClass *** tickle at:"+time);
return true;
}
}
}