home *** CD-ROM | disk | FTP | other *** search
/ Oracle Video Server 3.0.3.1 / OracleVideoClient3.0.3.iso / webplugin / samples / liveconnect / java / full / sample1 / fulljava.java < prev   
Encoding:
Java Source  |  1998-01-30  |  15.6 KB  |  544 lines

  1. //******************************************************************************
  2. // Applet:
  3. //
  4. // fullJava.java    (compiles to fullJava.class)    
  5. //
  6. //******************************************************************************
  7. import java.applet.*;
  8. import java.awt.*;
  9. import netscape.javascript.JSObject;    //provides access to objects
  10. import netscape.javascript.JSException; //JavaScript Exception 
  11.  
  12. import OviObserver;                        //OVI Plug-in listener
  13. import OviPlayer;                        //OviPlayer interface
  14.  
  15. //==============================================================================
  16. // Main Class for applet fullJava
  17. //
  18. //==============================================================================
  19. public class fullJava extends Applet implements OviObserver
  20. {
  21.     private JSObject window, document;        //objects from Netscape env
  22.     private OviPlayer ovi = null;            //OviPlayer object
  23.     int sess  = 0;
  24.  
  25.     boolean started      = false;                //track state of applet
  26.     
  27.  
  28.     // PARAMETER SUPPORT:
  29.     //        Parameters allow an HTML author to pass information to the applet;
  30.     // the HTML author specifies them using the <PARAM> tag within the <APPLET>
  31.     // tag.  The following variables are used to store the values of the
  32.     // parameters.
  33.     //--------------------------------------------------------------------------
  34.  
  35.     // Members for applet parameters
  36.     // <type>       <MemberVar>    = <Default Value>
  37.     //--------------------------------------------------------------------------
  38.     private String m_plgname     = "";
  39.     private String m_movie1Title = "";
  40.     private String m_movie2Title = "";
  41.     private String m_movie3Title = "";
  42.     private String m_movie1         = "";
  43.     private String m_movie2         = "";
  44.     private String m_movie3         = "";
  45.     private String[][] m_movieInfo;
  46.  
  47.     // Parameter names.  To change a name of a parameter, you need only make
  48.     // a single change.  Simply modify the value of the parameter string below.
  49.     //--------------------------------------------------------------------------
  50.     private final String PARAM_plgname        = "plgname";
  51.     private final String PARAM_movie1Title  = "movie1Title";
  52.     private final String PARAM_movie1        = "movie1";
  53.     private final String PARAM_movie2Title  = "movie2Title";
  54.     private final String PARAM_movie2        = "movie2";
  55.     private final String PARAM_movie3Title  = "movie3Title";
  56.     private final String PARAM_movie3        = "movie3";
  57.     
  58.  
  59.     //GUI COMPONENTS
  60.     //---------------------------------------------------------------------------
  61.     Panel ctrlPanel        = null;
  62.     Panel ckPanel        = null;
  63.     Panel statusPanel    = null;
  64.  
  65.     Button btnPause        = null;
  66.     Button btnPlay        = null;
  67.     Button btnFwd        = null;
  68.     Button btnRwd        = null;
  69.     Button btnLoad        = null;
  70.     Button btnClose        = null;
  71.  
  72.     Checkbox ckLoop        = null;
  73.     Checkbox ckStart    = null;
  74.     Label  counter        = null;
  75.  
  76.  
  77.     
  78.  
  79.     // fullJava Class Constructor
  80.     //--------------------------------------------------------------------------
  81.     public fullJava()
  82.     {
  83.  
  84.     }
  85.  
  86.     // APPLET INFO SUPPORT:
  87.     // The getAppletInfo() method returns a string describing the applet's
  88.     // author, copyright date, or miscellaneous information.
  89.     //--------------------------------------------------------------------------
  90.     public String getAppletInfo()
  91.     {
  92.         return "Name: fullJava.class\r\n" +
  93.                "Author: Oracle Corp., All Rights Reserved\r\n" +
  94.                "Created with Microsoft Visual J++ Version 1.1";
  95.     }
  96.  
  97.     // PARAMETER SUPPORT
  98.     // The getParameterInfo() method returns an array of strings describing
  99.     // the parameters understood by this applet.
  100.     //
  101.     // fullJava Parameter Information:
  102.     //  { "Name", "Type", "Description" },
  103.     //--------------------------------------------------------------------------
  104.     public String[][] getParameterInfo()
  105.     {
  106.         String[][] info =
  107.         {
  108.             { PARAM_plgname, "String", "Name of Browser plug-in object" },
  109.             { PARAM_movie1Title, "String", "Title of first movie clip"    },
  110.             { PARAM_movie1, "String", "Path to first movie clip"        },
  111.             { PARAM_movie2Title, "String", "Title of second movie clip"    },
  112.             { PARAM_movie2, "String", "Path to first movie clip"        },
  113.             { PARAM_movie3Title, "String", "Title of third movie clip"    },
  114.             { PARAM_movie3, "String", "Path to first movie clip"        },
  115.         };
  116.         return info;        
  117.     }
  118.  
  119.     // The init() method is called by the AWT when an applet is first loaded or
  120.     // reloaded.  Override this method to perform whatever initialization your
  121.     // applet needs, such as initializing data structures, loading images or
  122.     // fonts, creating frame windows, setting the layout manager, or adding UI
  123.     // components.
  124.     //--------------------------------------------------------------------------
  125.     public void init()
  126.     {
  127.         boolean cond = false;
  128.         // PARAMETER SUPPORT
  129.         //        The following code retrieves the value of each parameter
  130.         // specified with the <PARAM> tag and stores it in a member
  131.         // variable.
  132.         //----------------------------------------------------------------------
  133.         String param;
  134.  
  135.         // param: Parameter description
  136.         //----------------------------------------------------------------------
  137.         param = getParameter(PARAM_plgname);
  138.         if (param != null)
  139.             m_plgname = param;
  140.         param = getParameter(PARAM_movie1Title);
  141.         if (param != null)
  142.             m_movie1Title = param;
  143.         param = getParameter(PARAM_movie1);
  144.         if (param != null)
  145.             m_movie1 = param;
  146.         param = getParameter(PARAM_movie2Title);
  147.         if (param != null)
  148.             m_movie2Title = param;
  149.         param = getParameter(PARAM_movie2);
  150.         if (param != null)
  151.             m_movie2 = param;
  152.         param = getParameter(PARAM_movie1Title);
  153.         if (param != null)
  154.             m_movie3Title = param;
  155.         param = getParameter(PARAM_movie3);
  156.         if (param != null)
  157.             m_movie3 = param;
  158.  
  159.         String [][]    movieInfo = 
  160.         {
  161.             { m_movie1Title, m_movie1},
  162.             { m_movie2Title, m_movie2},
  163.             { m_movie3Title, m_movie3}
  164.         };
  165.  
  166.         m_movieInfo = movieInfo;
  167.  
  168.         // If you use a ResourceWizard-generated "control creator" class to
  169.         // arrange controls in your applet, you may want to call its
  170.         // CreateControls() method from within this method. Remove the following
  171.         // call to resize() before adding the call to CreateControls();
  172.         // CreateControls() does its own resizing.
  173.         //----------------------------------------------------------------------
  174.         resize(320, 45);
  175.         setBackground(Color.white);
  176.         setLayout( new GridLayout(2,1,2,2));
  177.         resize(320, 45);
  178.  
  179.         ctrlPanel = new Panel ();
  180.         ctrlPanel.setLayout(new GridLayout(1,4,2,2));
  181.         ctrlPanel.resize (320, 24);
  182.  
  183.         ckPanel = new Panel ();
  184.         ckPanel.setLayout (new FlowLayout(FlowLayout.LEFT,4,0));
  185.  
  186.         statusPanel = new Panel ();
  187.         statusPanel.setLayout(new BorderLayout());
  188.         statusPanel.setBackground(Color.pink);
  189.         statusPanel.resize (318, 24);
  190.  
  191.         btnPause    = new Button ("Pause");
  192.         btnPlay        = new Button ("Play");
  193.         btnFwd        = new Button ("Seek >>");
  194.         btnRwd        = new Button ("<< Seek");
  195.         btnLoad        = new Button ("Load Next");
  196.         btnClose    = new Button ("Close");
  197.  
  198.         counter        = new Label ("   00:00:00");
  199.         counter.setBackground(Color.black);
  200.         counter.setForeground(Color.green);
  201.         counter.resize (50, 20);
  202.  
  203.         ckLoop        = new Checkbox ("Loop");
  204.         ckStart        = new Checkbox ("Auto Start");
  205.  
  206.         ctrlPanel.add (btnPlay);
  207.         ctrlPanel.add (btnRwd);
  208.         ctrlPanel.add (btnFwd);
  209.         ctrlPanel.add (btnLoad);
  210.         ctrlPanel.add (btnClose);
  211.  
  212.         ckPanel.add (ckStart);
  213.         ckPanel.add (ckLoop);
  214.  
  215.         statusPanel.add    ("Center", ckPanel);
  216.         statusPanel.add ("East", counter);        
  217.         
  218.         add (ctrlPanel);
  219.         add (statusPanel);    
  220.  
  221.         /* Give time for plug-in instance initialization */
  222.         try{
  223.             Thread.sleep(3000);
  224.         } catch (InterruptedException e){
  225.             System.out.println("Failed to sleep in start()...");
  226.         }
  227.     }
  228.  
  229.  
  230.     // fullJava Paint Handler
  231.     //--------------------------------------------------------------------------
  232.     public void paint(Graphics g)
  233.     {
  234.         //Place applet paint code here
  235.     }
  236.  
  237.     // The start() method is called when the page containing the applet
  238.     // first appears on the screen. The AppletWizard's initial implementation
  239.     // of this method starts execution of the applet's thread.
  240.     //--------------------------------------------------------------------------
  241.     public void start()
  242.     {
  243.         // GRAB PLUG-IN OBJECT 
  244.         // Retrieve the plug-in object handle from the browser's document context
  245.         //-------------------------------------------------------------------------
  246.         ovi = getPlugin();
  247.  
  248.         // REGISTER CLASS AS PLUG-IN EVENT LISTENER 
  249.         // Set up plug-in listener relationship 
  250.         //-------------------------------------------------------------------------
  251.         if (! started) {
  252.             getObserver();
  253.         }
  254.     }
  255.     
  256.     // The stop() method is called when the page containing the applet is
  257.     // no longer on the screen. The AppletWizard's initial implementation of
  258.     // this method stops execution of the applet's thread.
  259.     //--------------------------------------------------------------------------
  260.     public void stop()
  261.     {
  262.         started = false;
  263.     }
  264.  
  265.     // Applet clean up code.  destroy() is called when
  266.     // when you applet is terminating and being unloaded.
  267.     //-------------------------------------------------------------------------
  268.     public void destroy()
  269.     {
  270.         ovi            = null;
  271.         document    = null;
  272.         window        = null;
  273.         System.gc();
  274.     }
  275.   //IMPLEMENT OVIOBSERVER METHODS
  276.   //Because OviObserver class is only an interface, the methods of this class
  277.   //must be defined by any Java class implementing this interface
  278.   //-------------------------------------------------------------------------
  279.   
  280.  
  281.     // IMPLEMENT ONSTOP() OVIOBSERVER METHOD
  282.     // Catch and handle onStop() method of OviObserver interface
  283.     // In this context, onStop is thrown when the end of the current
  284.     // stream is reached, not when a pause is called.
  285.     //------------------------------------------------------------------------
  286.     public void onStop() 
  287.     {
  288.         
  289.       if(! ckLoop.getState()) {
  290.  
  291.         System.out.println("\nCurrent video has ended. Loading new video...");
  292.         if(sess == 2)
  293.             sess = 0;
  294.         else
  295.             sess++;
  296.         counter.setText ("   00:00:00");
  297.  
  298.         if (ckStart.getState()) {
  299.             ovi.setAutoStart (true);
  300.             btnPlay.setLabel ("Pause");
  301.         }
  302.         else {
  303.             ovi.setAutoStart (false);
  304.             btnPlay.setLabel ("Play");
  305.         }
  306.         ovi.load (m_movieInfo[sess][1]);
  307.         /* 
  308.          * wait for client initialization 
  309.          */
  310.         while (ovi.getState() < ovi.ST_INIT);
  311.  
  312.       } //end if
  313.       else
  314.           System.out.println("\nLooping current video...");
  315.     }
  316.  
  317.     // IMPLEMENT ONPOSITIONCHANGE() OVIOBSERVER METHOD
  318.     // Catch and handle onPositionChange() method of OviObserver interface
  319.     //--------------------------------------------------------------------------
  320.     public void onPositionChange(int newPos) 
  321.     {
  322.         started = true;
  323.  
  324.         long tmp;
  325.         long tmp2;
  326.         long hh;
  327.         long mm;
  328.         long ss;
  329.         String s = "";
  330.  
  331.         tmp = newPos;
  332.         tmp2 = tmp % 3600000;
  333.         hh = (tmp - tmp2) / 3600000;
  334.         if (tmp2 > 0)
  335.             tmp = tmp2;
  336.         tmp2 = tmp % 60000;
  337.         mm = (tmp -tmp2) / 60000;
  338.         if (tmp2 > 0)
  339.             tmp = tmp2;
  340.         tmp2 = tmp % 1000;
  341.         ss = (tmp - tmp2) /1000;
  342.         s = "   " + pad (hh) + ":" + pad (mm) + ":" + pad (ss);
  343.         counter.setText(s);
  344.     }
  345.  
  346.   //HANDLE MOUSE EVENTS
  347.   //Traps all action events to relevant components.
  348.   //Event handling code for any additional components may be added here.
  349.   //--------------------------------------------------------------------------  
  350.   public boolean mouseUp (Event evt, int x, int y)
  351.   {
  352.     boolean bCond = false;
  353.  
  354.     // GRAB PLUG-IN OBJECT 
  355.     // Refresh the plug-in object handle from the browser's document context.
  356.     //-------------------------------------------------------------------------
  357.     ovi = getPlugin();
  358.  
  359.     // REGISTER CLASS AS PLUG-IN EVENT LISTENER 
  360.     // Set up plug-in listener relationship 
  361.     //-------------------------------------------------------------------------
  362.     if (! started) {
  363.         getObserver();
  364.     }
  365.  
  366.     // PROCESS MOUSE-UP EVENTS
  367.     //-------------------------------------------------------------------------
  368.     if (evt.target == ckLoop) {
  369.         if(!ckLoop.getState()) {
  370.             ovi.setLoop(true);
  371.         }
  372.         else {
  373.             ovi.setLoop(false);
  374.         }
  375.         bCond = true;
  376.     }
  377.  
  378.     if (evt.target == ckStart) {
  379.  
  380.         if (!ckStart.getState()) {
  381.             ovi.setAutoStart(true);
  382.         }
  383.         else {
  384.             ovi.setAutoStart(false);
  385.         }
  386.         bCond = true;
  387.     }
  388.  
  389.     if (evt.target == btnPlay) {
  390.         if (ovi.getState() == ovi.ST_PLAYING) {
  391.           ovi.pause();
  392.           btnPlay.setLabel("Resume");
  393.         }
  394.         else if (ovi.getState() == ovi.ST_PAUSED) {
  395.             ovi.resume();
  396.             btnPlay.setLabel("Pause");
  397.         }
  398.         else if (ovi.getState() == ovi.ST_REALIZED) {
  399.             ovi.play();
  400.             btnPlay.setLabel("Pause");
  401.         }
  402.       bCond = true;
  403.      }
  404.  
  405.     if (evt.target == btnFwd) {
  406.         ovi.pause();
  407.  
  408.         long cur = ovi.getPos();
  409.         long len = ovi.getMaxPos();
  410.         long dest = (long)(cur + (len * .10));
  411.         if (dest > len)
  412.             dest = len;
  413.         
  414.         ovi.setPos(dest);
  415.         btnPlay.setLabel("Resume");
  416.         bCond = true;
  417.     }
  418.  
  419.     if (evt.target == btnRwd) {
  420.         ovi.pause();
  421.  
  422.         long cur = ovi.getPos();
  423.         long len = ovi.getMaxPos();
  424.         long dest = (long)(cur - (len * .10));
  425.         if (dest < 0)
  426.             dest = 0;
  427.  
  428.         ovi.setPos(dest);
  429.         btnPlay.setLabel("Resume");
  430.         bCond = true;
  431.     }
  432.  
  433.     if (evt.target == btnLoad) {
  434.         if(sess == 2) {
  435.             sess = 0;
  436.         }
  437.         else {
  438.             sess++;
  439.         }
  440.         counter.setText ("   00:00:00");
  441.         if (ckStart.getState()) {
  442.             ovi.setAutoStart (true);
  443.             btnPlay.setLabel("Pause");
  444.         }
  445.         else {
  446.             ovi.setAutoStart (false);
  447.             btnPlay.setLabel("Play");
  448.         }
  449.  
  450.         ovi.load (m_movieInfo[sess][1]);    /* load next video */
  451.  
  452.         /* 
  453.          * wait for client initialization 
  454.          */
  455.         while (ovi.getState() < ovi.ST_INIT);        
  456.  
  457.         bCond = true;
  458.     }
  459.  
  460.     if (evt.target == btnClose) {
  461.       ovi.unload ();
  462.       counter.setText("   00:00:00");
  463.       btnPlay.setLabel("Play");
  464.     }
  465.    
  466.     return bCond;
  467.   } //end mouseUp()    
  468.  
  469.     //UTILITY FUNCTION
  470.     //-------------------------------------------------------------------------
  471.     String pad (long l)
  472.     {
  473.         String s = Long.toString(l);
  474.         if (s.length () == 1)
  475.           s = "0" + s;
  476.          return s;
  477.     }
  478.  
  479.     // GET JS OBJECT HANDLES AND POINTER TO PLUGIN
  480.     // Retrieves JavaScript Object contexts from Browswer.
  481.     // This function depends upon the Netscape supplied Java package,
  482.     // netscape.javascript. This function will NOT work under Internet
  483.     // Explorer.
  484.     //-------------------------------------------------------------------
  485.     public OviPlayer getPlugin () {
  486.  
  487.         OviPlayer    plugin    = null;
  488.         ovi                    = null;
  489.         document            = null;
  490.         window                = null;
  491.         System.gc ();        
  492.  
  493.         try {
  494.             window = JSObject.getWindow(this); //grab window obj
  495.         }        
  496.         catch(NullPointerException e) {
  497.             System.out.println("getWindow() failed in getPlugin()...");
  498.         }
  499.     
  500.         if (window != null)
  501.             try {
  502.                 document = (JSObject) window.getMember("document"); //grab document obj
  503.             }        
  504.             catch(NullPointerException e) {
  505.                 System.out.println("getMember() failed in getPlugin()...");
  506.             }
  507.         if (document != null) {
  508.             try {
  509.                 plugin = (OviPlayer) document.getMember(m_plgname);
  510.             } 
  511.             catch(NullPointerException e) {
  512.                   System.out.println("unable to grab plugin");
  513.             }
  514.         }
  515.         else {
  516.             System.out.println("Document null in getPlugin()...\n");
  517.         }
  518.  
  519.         return plugin;
  520.  
  521.     } //end getPlugin
  522.  
  523.     // REGISTER CLASS AS PLUG-IN EVENT LISTENER
  524.     // Retrieves JavaScript Object contexts from Browswer.
  525.     // This function depends upon the Netscape supplied Java package,
  526.     // netscape.javascript. This function will NOT work under Internet
  527.     // Explorer.
  528.     //-------------------------------------------------------------------
  529.     private void getObserver () 
  530.     {
  531.         if (ovi != null) {
  532.             try {
  533.                 if (ovi.advise(this)) {}
  534.             }
  535.             catch(NullPointerException e) {
  536.                 System.out.println("Caught NullPointerException in getObserver()...\n");
  537.             }
  538.         }
  539.         else
  540.             System.out.println("'ovi' null in getObserver()...\n");
  541.     } // end getObserver
  542.  
  543. } // end fullJava
  544.