/* * @(#)Seek.java 1.2 01/03/13 * * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */ import java.awt.*; import java.awt.event.*; import javax.media.*; import javax.media.control.FramePositioningControl; import javax.media.protocol.*; import javax.media.protocol.DataSource; import java.applet.*; import javax.swing.*; import javax.swing.event.*; /** * Sample program to demonstrate FramePositioningControl. */ public class Seek extends Frame implements ControllerListener, ActionListener { Player p; FramePositioningControl fpc; Object waitSync = new Object(); Timer timer; int delay; boolean stateTransitionOK = true; int totalFrames = FramePositioningControl.FRAME_UNKNOWN; Panel cntlPanel; Button fwdButton; Button bwdButton; Button rndButton; Button ffwdButton; Button fbwdButton; Button pauseButton; TextField framesTextfield; JSlider framesSlider; JLabel framesLable; boolean ffdead = false; boolean bfdead = false; boolean frozen = false; boolean preThreadFlg4FF = false; boolean preThreadFlg4BF = false; JLabel jLabel1 = new JLabel(); public Seek() { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } /** * Given a DataSource, create a player and use that player * as a player to playback the media. */ public boolean open(DataSource ds) { System.err.println("create player for: " + ds.getContentType()); try { p = Manager.createPlayer(ds); } catch (Exception e) { System.err.println( "Failed to create a player from the given DataSource: " + e); return false; } p.addControllerListener(this); p.realize(); if (!waitForState(p.Realized)) { System.err.println("Failed to realize the player."); return false; } // Try to retrieve a FramePositioningControl from the player. fpc = (FramePositioningControl) p.getControl( "javax.media.control.FramePositioningControl"); if (fpc == null) { System.err.println( "The player does not support FramePositioningControl."); System.err.println( "There's no reason to go on for the purpose of this demo."); return false; } Time duration = p.getDuration(); if (duration != Duration.DURATION_UNKNOWN) { // System.err.println("Movie duration: " + duration.getSeconds()); System.err.println("Movie duration: " + duration.getNanoseconds()); totalFrames = fpc.mapTimeToFrame(duration); if (totalFrames != FramePositioningControl.FRAME_UNKNOWN) System.err.println("Total # of video frames in the movies: " + totalFrames); else System.err.println( "The FramePositiongControl does not support mapTimeToFrame."); } else { System.err.println("Movie duration: unknown"); } // Prefetch the player. p.prefetch(); if (!waitForState(p.Prefetched)) { System.err.println("Failed to prefetch the player."); return false; } // Display the visual & control component if there's one. cntlPanel = new Panel(); cntlPanel.setLayout(new GridLayout(3, 3)); fwdButton = new Button("Forward"); bwdButton = new Button("Backward"); rndButton = new Button("Random"); ffwdButton = new Button("Fast Forward"); fbwdButton = new Button("Fast Backward"); pauseButton = new Button("Pause"); framesTextfield = new TextField(); framesTextfield.setText("1"); framesSlider = new JSlider(JSlider.HORIZONTAL, 0, totalFrames, 10); framesLable = new JLabel(" Current frame:"); framesLable.setForeground(Color.blue); framesLable.setBackground(Color.lightGray); fwdButton.addActionListener(this); bwdButton.addActionListener(this); rndButton.addActionListener(this); ffwdButton.addActionListener(this); fbwdButton.addActionListener(this); pauseButton.addActionListener(this); // framesTextfield.addActionListener(new textfieldListner()); framesSlider.addChangeListener(new SliderListener()); cntlPanel.add(fwdButton); cntlPanel.add(bwdButton); cntlPanel.add(rndButton); cntlPanel.add(ffwdButton); cntlPanel.add(fbwdButton); cntlPanel.add(pauseButton); cntlPanel.add(framesLable); cntlPanel.add(framesTextfield); cntlPanel.add(framesSlider); Component vc; if ( (vc = p.getVisualComponent()) != null) { add("Center", vc); } add("South", cntlPanel); setVisible(true); return true; } public void addNotify() { super.addNotify(); pack(); } /** * Block until the player has transitioned to the given state. * Return false if the transition failed. */ boolean waitForState(int state) { synchronized (waitSync) { try { while (p.getState() < state && stateTransitionOK) waitSync.wait(); } catch (Exception e) {} } return stateTransitionOK; } /** Listens to the slider. */ class SliderListener implements ChangeListener { public void stateChanged(ChangeEvent e) { JSlider source = (JSlider) e.getSource(); if (!source.getValueIsAdjusting()) { framesTextfield.setText("" + (int) source.getValue()); } } } public void actionPerformed(ActionEvent ae) { String command = ae.getActionCommand(); if (command.equals("Forward")) { ffdead = true; bfdead = true; int dest = fpc.skip(1); System.err.println("Step forward " + dest + " frame."); } else if (command.equals("Backward")) { ffdead = true; bfdead = true; int dest = fpc.skip( -1); System.err.println("Step backward " + dest + " frame."); } else if (command.equals("Random")) { ffdead = true; bfdead = true; if (totalFrames == FramePositioningControl.FRAME_UNKNOWN) System.err.println("Cannot jump to a random frame."); else { int randomFrame = (int) (totalFrames * Math.random()); randomFrame = fpc.seek(randomFrame); System.err.println("Jump to a random frame: " + randomFrame); } } else if (command.equals("Fast Forward")) { // if (preThreadFlg4FF == true) return; ffdead = false; bfdead = true; frozen = false; new Thread(new Runnable() { public void run() { preThreadFlg4FF = true; int i = fpc.mapTimeToFrame(p.getMediaTime()); while ((i <= totalFrames)) { int dest = fpc.skip(1); System.err.println("Step forward " + dest + " frame."); System.err.println("Current frame: " + i); framesSlider.setValue(i); try { Thread.sleep(1); } catch (InterruptedException e) { System.err.print(e); } while(frozen == true) { try { Thread.sleep(100); }catch(InterruptedException e) { System.out.println(e.toString()); } if (ffdead == true) return; } i = i + 1; if (ffdead == true) return; } preThreadFlg4FF = false; } }).start(); } else if (command.equals("Fast Backward")) { bfdead = false; ffdead = true; frozen = false; new Thread(new Runnable() { public void run() { int i = fpc.mapTimeToFrame(p.getMediaTime()); while ( (i >= 0)) { int dest = fpc.skip( -1); System.err.println("Step backward " + dest + " frame."); System.err.println("Current frame: " + i); framesSlider.setValue(i); try { Thread.sleep(1); } catch (InterruptedException e) { System.err.print(e); } while (frozen == true) { try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println(e.toString()); } if (bfdead == true) return; } if (bfdead == true) return; i = i - 1; } } }).start(); } else if (command.equals("Pause")) { if (frozen == false) frozen = true; else frozen = false; // if (ffdead == false) { // if (frozen1 == false) // frozen1 = true; // else // frozen1 = false; // } // else if (bfdead == false) { // if (frozen2 == false) // frozen2 = true; // else // frozen2 = false; // } } int currentFrame = fpc.mapTimeToFrame(p.getMediaTime()); // if (currentFrame != FramePositioningControl.FRAME_UNKNOWN) // System.err.println("Current frame: " + currentFrame); if((command.equals("Forward"))||(command.equals("Backward"))||(command.equals("Random"))){ if (currentFrame != FramePositioningControl.FRAME_UNKNOWN) System.err.println("Current frame: " + currentFrame); } framesSlider.setValue(currentFrame); } /** * Controller Listener. */ public void controllerUpdate(ControllerEvent evt) { if (evt instanceof ConfigureCompleteEvent || evt instanceof RealizeCompleteEvent || evt instanceof PrefetchCompleteEvent) { synchronized (waitSync) { stateTransitionOK = true; waitSync.notifyAll(); } } else if (evt instanceof ResourceUnavailableEvent) { synchronized (waitSync) { stateTransitionOK = false; waitSync.notifyAll(); } } else if (evt instanceof EndOfMediaEvent) { p.setMediaTime(new Time(0)); //p.start(); //p.close(); //System.exit(0); } else if (evt instanceof SizeChangeEvent) { } } /** * Main program */ public static void main(String[] args) { if (args.length == 0) { prUsage(); System.exit(0); } MediaLocator ml; //boolean frozen = false; if ( (ml = new MediaLocator(args[0])) == null) { System.err.println("Cannot build media locator from: " + args[0]); prUsage(); System.exit(0); } DataSource ds = null; // Create a DataSource given the media locator. try { ds = Manager.createDataSource(ml); } catch (Exception e) { System.err.println("Cannot create DataSource from: " + ml); System.exit(0); } Seek seek = new Seek(); if (!seek.open(ds)) System.exit(0); } static void prUsage() { System.err.println("Usage: java Seek "); } }