lecture12
Download
Skip this Video
Download Presentation
Lecture12

Loading in 2 Seconds...

play fullscreen
1 / 22

Lecture12 - PowerPoint PPT Presentation


  • 169 Views
  • Uploaded on

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 identify it as media with a meaningful time dimension. Audio with Core Java. import java.applet.Applet; import java.applet.AudioClip;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Lecture12' - freya


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
lecture12

Lecture12

Java Media Framework I

streaming media
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.
audio with core java
Audio with Core Java

import java.applet.Applet;

import java.applet.AudioClip;

public class OldAudioClip extends java.applet.Applet {

AudioClip aClip;

// init()

// start()

// stop()

}

audio with core java1
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();

}

oldaudioclip html
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>

player vs manager in jmf
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
creating a player
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");

}

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

}

looping play in jmf
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.
player with controllerlistener for looping
Player with ControllerListenerfor 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");

}

controllerlistener for looping
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();

}

}

}

interacting with the player
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.
the player states
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.
player events
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.
jmf player applet with control panel
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

}

init for jmf player applet with control panel
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);

}

}

start for jmf player applet with control panel
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");

}

}

controllerlistener with controlpanelcomponent
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

}

controlpanelcomponent
ControlPanelComponent

class AddComponentsThread implements Runnable {

public void run() {

Component cpt = player.getControlPanelComponent();

if (cpt != null)

panel.add(cpt, BorderLayout.CENTER);

panel.validate();

}

}

playing video
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.
playing video1
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();

}

}

algorithm to play a media object in an applet
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:

  • Obtain the Player object’s visual component (if it exists) and place it on the applet.
  • Obtain the Player objects’s control panel if required and place it on the applet.