HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Unlocking the Secrets of the Java Media Framework"
Volume: 5 Issue: 2, p.74

	

Listing 1: 

import java.applet.AudioClip; 

public class LessPrimitive extends java.applet.Applet 
{ 
   AudioClip audioClip1; 
   AudioClip audioClip2; 

   // Applet role 
   public void init() 
   { 
  
      // create two audio objects 

      audioClip1 =  getAudioClip( get- 
      CodeBase(), "clock1.au" ); 
      audioClip2 =  getAudioClip( get- 
      CodeBase(), "clock2.au" ); 

      // play two audio files in loop mode 
  
      audioClip1.loop(); 
      audioClip2.loop(); 

      try 
      { 
        // we still have NO clue how 
        // long these files will take if 
        // we want to play them one time 
        // however, we put them in loop 
        // mode, so they will play forever 
        Thread.sleep( (long) 2000 ); 
      } 
      catch (InterruptedException e) 
      { 
        System.out.println("Someone 
        woke us up unexpectedly..."); 

      } 

        // stop the first clip after 2 
        // seconds 

        audioClip1.stop(); 

        try 
      { 
        // we still have NO clue how 
        // long these files will take if 
        // we want to play them one time 
        // however, we put them in loop 
        // mode, so they will play forever 
        Thread.sleep( (long) 7000 ); 
      } 
      catch (InterruptedException e) 
      { 
        System.out.println("Someone 
        woke us up unexpectedly..."); 

      } 
  
      // stop the 2nd after another 7 
      // seconds 

      audioClip2.stop(); 

   } 

} 

Listing 2: 

public synchronized void controllerUpdate(ControllerEvent event) 
{ 
 // this event is received when the 
 // Controller enters Realized state 
 if (event instanceof RealizeCom- 
 pleteEvent) 
 { 

        // event handling code goes 
        // here..... 
  } 
 // this event is received when the 
 // Controller finishes prefetching.... 
    else if (event instanceof Prefetch- 
    CompleteEvent) 
    { 
        // event handling code goes 
        // here..... 
  
    } 

} 

Listing 3: 

MediaLocator mrl = null; 
URL url = null; 

// The applet tag should contain the 
// path to the source media file, rela- 
// tive to the html page. 

if ((mediaFile = getParameter("FILE")) == null) 
{ 
    System.out.println("Invalid 
    media....."); 
} 
try 
{ 
    // create a MediaLocator based on 
    // the filename 
    url = new URL(getDocumentBase(), 
    mediaFile); 
    mediaFile = url.toExternalForm(); 
} 
catch (MalformedURLException mue) 
{ 
    System.out.println("Bad URL......"); 
} 
try 
{ 
  // Create a media locator from URL 
  if ((mrl = new MediaLocator(media- 
  File)) == null) 
  { 
   System.out.println("Can't build URL 
   for " + mediaFile); 
  } 

catch (MalformedURLException mue) 
{ 
    System.out.println("Invalid media 
    file URL!"); 
    System.exit(0); 
} 

Listing 4: 

try 
{ 
    // create a Player based on the 
    // medialocator 
    player = Manager.createPlayer(mrl); 
} 
catch (NoPlayerException e) 
{ 
    // exceptions will be caught 
    // here... 
    System.out.println("Can't find a 
    player for " + mrl); 

} 

// if an exception occurred in Manag- 
// er.createPlayer(mrl) 
// then the player reference will be 
// NULL and the line below will gener- 
// ate another exception 
player.addControllerListener(this); 

Listing 5: 

 // this applet will load the audio 
 // file specified by the HTML page and 
 // play it forever until stop or pause 
 // is pressed 

import java.applet.Applet; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import java.net.URL; 
import java.io.IOException; 
import java.util.Properties; 
import javax.media.*; 
import java.lang.String; 
import java.net.MalformedURLException; 

public class AlohaJMF extends Applet implements ControllerListener 
{ 
  
    // this is the object that will 
    // play the media files.. 
    Player player = null; 

    // displays progress during download 
    Component progressBar = null; 
  
  
    // video window 
    Component visualComponent = null; 

 
    // GUI controls for position, 
    // start, stop etc. 
    Component controlComponent = null; 
  
  
    int controlPanelHeight = 0; 
    Panel panel = null; 

    // attributes of the video window.... 
    int videoHeight = 0; 
    int videoWidth = 0; 

    // init reads the HTML params and 
    // creates a Player based on those 
    // parameters...... 
    public void init() 
    { 
     setLayout(null); 
  
     // create the panel where we'll 
     // show our Player 
     panel = new Panel(); 
     panel.setLayout( null ); 
     add(panel); 
     panel.setBounds(0, 0, 320, 240); 

     // HTML page will tell us what to 
     // play... 
     String mediaFile = null; 
  
     MediaLocator mrl = null; 
     URL url = null; 

     // Get the file to play from HTML... 
  
     if ((mediaFile = 
     getParameter("FILE")) == null) 
         System.out.println("HTML page 
         contains bogus file."); 

     try 
     { 
         // create a MediaLocator based 
         // on the filename 
         url = new URL(getDocument- 
         Base(), mediaFile); 
         mediaFile = url.toExternal- 
         Form(); 
     } 
     catch (MalformedURLException mue) 
     { 
         System.out.println("Invalid URL"); 
     } 
  
     try 
     { 
         // Create a media locator from URL 
         if ((mrl = new MediaLocator- 
         (mediaFile)) == null) 
         { 
           System.out.println("URL er- 
           ror for:" + mediaFile); 
     } 
else 
     { 
       try 
       { 
         // setup Player for media 
             player = Manager.cre- 
            atePlayer(mrl); 
            // listen for events imme- 
            // diately.... 
            player.addController- 
            Listener(this); 
             } 
             catch (NoPlayerException e) 
             { 
                   System.out.println- 
                   ("Player creation 
                   failed for " + mrl); 
                System.exit(0); 
             } 
            } 
     } 
     catch (MalformedURLException mue) 
     { 
         System.out.println("Invalid 
         media file URL!"); 
         System.exit(0); 
     } 
     catch (IOException ioe) 
     { 
         System.out.println("IO excep- 
         tion creating player for " + mrl); 
         System.exit(0); 
     } 
  

    } 

    // this method is called on applet 
    // creation and also when the page 
    // is reloaded 

    public void start() 
    { 
        // automatically begin playback 
        // when the page is loaded.... 
     if (player != null) 
     { 
         player.start(); 
     } 
    } 

    // make sure to stop AND close all 
    // resources when stop is called.. 
    // otherwise, we'll have resource 
    // leaks and frustrated users... 
    public void stop() 
    { 
        if (player != null) 
        { 
            player.stop(); 
            player.deallocate(); 
        } 
    } 

    public void destroy() 
    { 
     player.close(); 
    } 
  

    // controllerUpdate is required to 
    // listen for JMF events 
  
    public synchronized void con- 
    trollerUpdate(ControllerEvent event) 
    { 
     // Has our Player dead? 
     // If so, nothing to do..event is 
     // bogus. 
     if (player == null) 
         return; 
  
     // When the player is Realized, 
     // get the visual and control com- 
     // ponents and add them to the 
     // Applet 
     if (event instanceof RealizeCom- 
     pleteEvent) 
     { 
         if (progressBar != null) 
         { 
          panel.remove(progressBar); 
          progressBar = null; 
         } 

         int width = 320; 
         int height = 0; 
         if (controlComponent == null) 
               if (( controlComponent = 
              player.getControl- 
                    PanelComponent()) 
                    != null) 
          { 
  
           controlPanelHeight = 
                 controlComponent.get- 
                   PreferredSize().height; 
           panel.add(controlCom- 
                 ponent); 
           height += control- 
                 PanelHeight; 
          } 
  
         if (visualComponent == null) 
          if (( visualComponent 
                   = player.getVisual- 
                   Component())!= null) 
          { 
              panel.add(visual- 
                    Component); 
              Dimension video 
                    Size = visual 
                    Component.getPre- 
                    ferredSize(); 
              videoWidth = 
                    videoSize.width; 
              videoHeight = 
                    videoSize.height; 
              width = video- 
                    Width; 
              height += video 
                    Height; 
              visualComponent.setBounds(0, 0, videoWidth, videoHeight); 
          } 

         panel.setBounds(0, 0, width, 
         height); 
         if (controlComponent != null) 
         { 
          controlComponent.set- 
                Bounds(0, videoHeight, 
                width, controlPanel 
                Height); 
                 controlCompo- 
                nent.invalidate(); 
         } 
  
      } 
  
      // the EndOfMediaEvent == no 
        // more data 
     else if (event instanceof EndOfMe- 
     diaEvent) 
     { 
         // jump to beginning of content 
         player.setMediaTime(new 
         Time(0)); 
         // immediately restart playback... 
         player.start(); 
     } 
     else if (event instanceof Con- 
     trollerClosedEvent) 
        { 
            // player is done, kill 
            // GUI elements 
         panel.removeAll(); 
     } 
    } 

} 
  



 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.