Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture12 Java Media Framework I. Streaming Media Steaming media simply means we have a stream of media coming through some kind of a media channel. Some.

Similar presentations


Presentation on theme: "Lecture12 Java Media Framework I. Streaming Media Steaming media simply means we have a stream of media coming through some kind of a media channel. Some."— Presentation transcript:

1 Lecture12 Java Media Framework I

2 Streaming Media Steaming media simply means we have a stream of media coming through some kind of a media channel. Some identify it as media with a meaningful time dimension.

3 Audio with Core Java import java.applet.Applet; import java.applet.AudioClip; public class OldAudioClip extends java.applet.Applet { AudioClip aClip; // init() // start() // stop() }

4 Audio with Core Java public void init() { // TODO start asynchronous download of heavy resources String audioFile; if ((audioFile = getParameter("AUDIOFILE")) == null) { System.err.println("Invalid AUDIOFILE parameter"); System.exit(1); } aClip = getAudioClip(getCodeBase(),audioFile); } public void start() { aClip.loop(); // or aClip.play() } public void stop() { aClip.stop(); }

5 OLdAudioClip.html Applet HTML Page Applet HTML Page

6 Player vs. Manager in JMF The main purpose of using JMF is very much to create a player which is capable of playing media such as audio and video. For this purpose JMF has the interface Player. JMF has a class called Manager which has all sort of static create methods, which in particular can create a Player object. There are usually two minimal interactions you do with JMF: Create a Player Start the Player

7 Creating a Player   In an applet: try { URL url = new URL(getCodeBase(),audioFile); player = Manager.createPlayer(url); } catch (NoPlayerException npe) { System.out.println("Could not create player"); } catch (MalformedURLException mue) { System.out.println("Bad URL"); } catch (IOException ioe) { System.out.println("IO error creating player"); }

8 Starting/Stopping/Closing a Player public void start() { player.start(); } public void stop() { player.stop(); } Public void distroy() {player.close();}

9 Looping play in JMF JMF Player object doesn’t have a loop method to loop the play! Looping the play is to do with player controls: for this we have a seperate interface Controller which maintain state information and fires various event objects of type ControllerEvent regarding the play. Just like the event handing architecture of java, the delegation event model, JMF needs objects which can listen to the event objects. These listeners are handled by Controllerlistner interfaces. We can design any class to implement these Controllerlistner interfaces. One of the event object which relate to looping play is EndOfMediaEvent event object. When the listener receives this object, all it has to do to loop the play is to rewind and start again. To start the play, the listener can acquire the source, i.e. the player, via getSourceController method.

10 Player with ControllerListener for Looping   In an Applet try { URL url = new URL(getCodeBase(),audioFile); player = Manager.createPlayer(url); player.addControllerListener(new LoopListener()); player.start(); } catch (NoPlayerException npe) { System.out.println("Could not create player"); } catch (MalformedURLException mue) { System.out.println("Bad URL"); } catch (IOException ioe) { System.out.println("IO error creating player"); }

11 ControllerListener for Looping public class LoopListener implements ControllerListener { private Player player; public void controllerUpdate(ControllerEvent event) { player = (Player) event.getSourceController(); if (event instanceof EndOfMediaEvent) { player.setMediaTime(new Time(0)); player.start(); }

12 Interacting with the Player Just like with a DVD player we would like to interact with the player to do things like pause, volume control, start etc. For this we need to have a control panel which has all the widgets that can perform the required tasks. The Player has getControlPanelComponent method which returns a Component which could be placed into the, say, swing’s JPanel.

13 The Player States Remember we had the time line. Along this time line, various events occurs, and as these events occurs, Player goes through different states as given in the following order: unrealized : Initial state. Player object instantiated. realizing : Determining required resources and information about media. Acquiring non-exclusive user resources. realized : All non-exclusive use resources have been acquired. prefetched : All resources have been acquired. started : Player is running. Clock has started.

14 Player Events The story is that the player can give the control panel component only after the realized state. The controller fires the beginning of realized state via RealizeCompleteEvent object. It is the ControllerListener object which capture RealizeCompleteEvent event object and hence it must implement the code to initiate processing the control panel component.

15 JMF Player Applet with Control Panel public class JMFAudioWithControlPanel extends javax.swing.JApplet { private Player player; private JPanel panel; String audioFile; // public void init() // public void start() // public void distroy() public void distroy(){ player.stop(); player.close(); } // public class AudioControlListener implements //ControllerListener }

16 init() for JMF Player Applet with Control Panel public void init() { panel = new JPanel(); panel.setLayout(new BorderLayout()); getContentPane().add(panel); if ((audioFile = getParameter("AUDIOFILE")) == null) { System.err.println("Invalid AUDIOFILE parameter"); System.exit(1); }

17 start() for JMF Player Applet with Control Panel public void start() { try { URL url = new URL(getCodeBase(),audioFile); player = Manager.createPlayer(url); player.addControllerListener(new AudioControlListener()); player.start(); } catch (NoPlayerException npe) { System.out.println("Could not create player"); } catch (MalformedURLException mue) { System.out.println("Bad URL"); } catch (IOException ioe) { System.out.println("IO error creating player"); }

18 ControllerListener with ControlPanelComponent public class AudioControlListener implements ControllerListener { private Player player; public void controllerUpdate(ControllerEvent event) { player = (Player) event.getSourceController(); if (event instanceof EndOfMediaEvent) { player.setMediaTime(new Time(0)); player.start(); } else if (event instanceof RealizeCompleteEvent){ SwingUtilities.invokeLater(new AddComponentsThread()); } // class AddComponentsThread implements Runnable }

19 ControlPanelComponent class AddComponentsThread implements Runnable { public void run() { Component cpt = player.getControlPanelComponent(); if (cpt != null) panel.add(cpt, BorderLayout.CENTER); panel.validate(); }

20 Playing Video We just need to get the visual component and put it at right place in the JPanel. All we need to change in the above program is change AddComponentsThread class which implements the Runnable interface for the GUI components.

21 Playing Video class AddComponentsThread implements Runnable { private Component controlPanel,visualComponent; public void run() { controlPanel = player.getControlPanelComponent(); if (controlPanel != null) panel.add(controlPanel, BorderLayout.SOUTH); visualComponent = player.getVisualComponent(); if (visualComponent != null) panel.add(visualComponent,BorderLayout.CENTER); panel.validate(); }

22 Algorithm to play a media object in an applet 1. Obtain the name of media file to play using the getParameter method of Applet. 2. Convert the URL object of the media file using getCodeBase method and the media file name. 3. Create the Player object using the JMF Manager class method createPlayer with the argument as the URL object. 4. Start the Player object. 5. Listen to the Player object events. 6. Wait for the Player object to become realized. 7. At the time of realization: a) a)Obtain the Player object’s visual component (if it exists) and place it on the applet. b) b)Obtain the Player objects’s control panel if required and place it on the applet.


Download ppt "Lecture12 Java Media Framework I. Streaming Media Steaming media simply means we have a stream of media coming through some kind of a media channel. Some."

Similar presentations


Ads by Google