hector
YaBB Newbies
Offline
Posts: 25
Movie Player
May 5th , 2009, 7:57pm
I am trying to create a class that can play the image and audio tracks of movies in various formats. This is my first try. There are several issues. First of all, I rely on awt, which could cause problems. Secondly, I am not sure whether the image and audio tracks will remain in sync. Thirdly the program does not exit gracefully. Fourthly, you name it... It is all very rough. But I would welcome any thoughts on this approach, or perhaps suggestions for alternative directions. I will post a sample usage in a separate message, since this one is about to exceed the character limit... Thanks! <code follows> import quicktime.*; import quicktime.app.time.*; import quicktime.app.view.*; import quicktime.io.*; import quicktime.std.*; import java.awt.*; import quicktime.std.movies.*; import quicktime.std.clocks.*; class Channel { Movie movie; MoviePlayer Channel; MovieController mc; QTImageProducer iP; QTFile f; OpenMovieFile omf; Track audioTrack; int trackCount; Movie copiedMovie; //virtual clipboard Channel( String file){ try { QTSessionCheck.check( ); f = new QTFile( file ); omf = OpenMovieFile.asRead( f ); movie = Movie.fromFile( omf ); trackCount = movie.getTrackCount( ); Channel = new MoviePlayer( movie ); mc = new MovieController( movie ); audioTrack = movie.getIndTrackType( 1, StdQTConstants.audioMediaCharacteristic, StdQTConstants.movieTrackCharacteristic ); }catch (Exception e) { System.out.println( e ); } } //constructor void start( ){ try { TaskAllMovies.addMovieAndStart( ); movie.start( ); } catch (StdQTException e ) { System.out.println( e ) ; } } void pause( ) { try { movie.stop( ); } catch (Exception e ) { } } void stop( ){ try { movie.stop( ); movie.setTime( new TimeRecord( movie.getTimeScale( ), 0 ) ); } catch (Exception e ) { } } void prepareToEdit(boolean edit ) { try { mc.enableEditing( edit ); } catch (StdQTException e ) { System.out.println( e ) ; } } //prepareToEdit( ) Image grabImage(int w, int h ) { Image image = null; try { iP = new QTImageProducer( Channel, new Dimension( w, h ) ); image = Toolkit.getDefaultToolkit().createImage( iP ); }catch (Exception e ) { return null; } return image; } void copy( int sourceIn, int sourceDuration ){ sourceIn = adjustTimeScale( sourceIn ); sourceDuration = adjustTimeScale( sourceDuration ); try { copiedMovie = new Movie( ); movie.insertSegment( copiedMovie, sourceIn, sourceDuration, 0 ); } catch (Exception e ) { System.out.println( e ) ; } } boolean cut( int sourceIn, int sourceDuration ) { sourceIn = adjustTimeScale( sourceIn ); sourceDuration = adjustTimeScale( sourceDuration ); try { copiedMovie = new Movie( ); movie.insertSegment( copiedMovie, sourceIn, sourceDuration, 0 ); movie.deleteSegment( sourceIn, sourceDuration ); mc.movieChanged( ); } catch (Exception e ) { System.out.println( e ) ; return false;} return true; } boolean paste( int targetIn, int duration ) { targetIn = adjustTimeScale( targetIn ); duration = adjustTimeScale( duration ); try { if (copiedMovie == null) return false; copiedMovie.insertSegment( movie, 0, duration, targetIn ); } catch (Exception e ) { System.out.println( e ) ; return false; } return true; } void delete( int start, int duration) { start = adjustTimeScale( start ); duration = adjustTimeScale( duration ); try { movie.deleteSegment( start, duration ); } catch (Exception e ) { System.out.println( e ) ; } } void undo( ){ try { mc.undo( ); } catch (QTException e ) { System.out.println( e ) ; } } void scaleSegment( int start, int oldDuration, int newDuration ){ start = adjustTimeScale( start ); oldDuration = adjustTimeScale( oldDuration); newDuration = adjustTimeScale( newDuration ); try { movie.scaleSegment( start, oldDuration, newDuration ); } catch (QTException e ) { System.out.println( e ) ; } } void jump( int whereToJump){ try { movie.setTimeValue( adjustTimeScale( whereToJump) ); movie.update( ); mc.movieChanged( ); } catch (Exception e ) { } } int adjustTimeScale( int seconds ) { try { return seconds * movie.getTimeScale( ); } catch( Exception e ) { return -1; } } boolean adjustSound( boolean whatToDo ) { try { if (audioTrack == null) return false; audioTrack.setEnabled( whatToDo ); return true; } catch (Exception e ) { return false; } } //if the audioTrack is disabled, enable it //otherwise, disable it. boolean flipSound( ){ try { return adjustSound( !audioTrack.getEnabled( ) ); } catch (Exception e ) { return false; } } public static class QTSessionCheck { private Thread shutdownHook; static QTSessionCheck instance; private QTSessionCheck( ) throws QTException { super( ); // init QTSession.open( ); // create shutdown handler shutdownHook = new Thread( ) { public void run( ) { QTSession.close( ); } }; Runtime.getRuntime( ).addShutdownHook(shutdownHook); } static QTSessionCheck getInstance( ) throws QTException { if (instance == null) instance = new QTSessionCheck( ); return instance; } static void check( ) throws QTException { getInstance( ); } }