Lecture12 Java Media Framework I. Streaming Media Steaming media simply means we have a stream of...

22
Lecture12 Lecture12 Java Media Framework I Java Media Framework I
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    219
  • download

    0

Transcript of Lecture12 Java Media Framework I. Streaming Media Steaming media simply means we have a stream of...

Page 1: 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.

Lecture12Lecture12

Java Media Framework IJava Media Framework I

Page 2: 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.

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.

Page 3: 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.

Audio with Core Java

import java.applet.Applet;import java.applet.AudioClip;public class OldAudioClip extends

java.applet.Applet {AudioClip aClip;// init()// init()// start()// start()// stop()// stop()

}}

Page 4: 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.

Audio with Core Javapublic void init() {

// TODO start asynchronous download of heavy resourcesString 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();}

Page 5: 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.

OLdAudioClip.html<HTML>

<HEAD><TITLE>Applet HTML Page</TITLE></HEAD><BODY><H3><HR WIDTH="100%">Applet HTML Page <HR WIDTH =

"100%"> </H3><P><APPLET code="OldAudioClip.class" width=350 height=200><param name = AUDIOFILE value = "spacemusic.au"></APPLET></P></BODY>

</HTML>

Page 6: 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.

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

Page 7: 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.

Creating a PlayerCreating 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");

}

Page 8: 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.

Starting/Stopping/Closing a Player Starting/Stopping/Closing a Player

public void start() {player.start();

}

public void stop() {player.stop();

}

Public void distroy() {player.close();player.close();

}}

Page 9: 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.

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.

Page 10: 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.

Player with ControllerListenerPlayer with ControllerListenerfor Loopingfor Looping

In an Applettry {

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");

}

Page 11: 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.

ControllerListener for Loopingpublic 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();

}}

}

Page 12: 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.

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.

Page 13: 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.

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.

Page 14: 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.

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.

Page 15: 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.

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

}

Page 16: 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.

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);}

}

Page 17: 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.

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");}

}

Page 18: 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.

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

}

Page 19: 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.

ControlPanelComponentControlPanelComponent

class AddComponentsThread implements Runnable {

public void run() {Component cpt =

player.getControlPanelComponent();if (cpt != null) panel.add(cpt, BorderLayout.CENTER);panel.validate();

}}

Page 20: 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.

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.

Page 21: 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.

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();

}}

Page 22: 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.

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) Obtain the Player object’s visual component (if it exists) and place it on the applet.

b) Obtain the Player objects’s control panel if required and place it on the applet.