Software School Projects | Academic Students Projects | Source Codes | Tablets header
Please use our contact us form or send email to Support@srishtis.com.

Java Media Framework
The Java Media Framework (JMF) is a recent API for Java dealing with real-time multimedia presentation and effects processing.  JMF handles time-based media, media which changes with respect to time.  Examples of this are video from a television source, audio from a raw-audio format file and animations.  The beta JMF 2.0 specification will be used for this report, as they currently reflect the features that will appear in the final version.
Using the Player
The Player class is an easy way to embed multimedia in an application.  It handles the setup of the file handler, video and audio decoders, and media renderers automatically.  It is possibly to embed the Player in a Swing application, but care must be taken as it is a heavy-weight component (it won?t clip if another component is placed in front of it).

import java.applet.*;
import java.awt.*;
import java.net.*;
import javax.media.*;

public class PlayerApplet extends Applet {

Player player = null;

public void init() {

setLayout( new BorderLayout() );

String mediaFile = getParameter( "FILE" );

try {

URL mediaURL = new URL( getDocumentBase(), mediaFile );

player = Manager.createRealizedPlayer( mediaURL );

if (player.getVisualComponent() != null)

add("Center", player.getVisualComponent());

if (player.getControlPanelComponent() != null)

add("South", player.getControlPanelComponent());

}

catch (Exception e) {

System.err.println( "Got exception " + e );

}

}

 

public void start() {

player.start();

}

 

public void stop() {

player.stop();

player.deallocate();

}

public void destroy() {

player.close();

}

}

In this case, we are using the static createRealizedPlayer() function of the Manager class to create the Player object.  This ensures that the visual and control panel components are created before it gets added to the window by blocking until then.  It is also possible to create an unrealized player and implement the ControllerEventHandler interface.  The window then waits for the controllerUpdate event to fire and adds the components to the layout as they are realized:

 

public synchronized void controllerUpdate( ControllerEvent event ) {

       if ( event instanceof RealizeCompleteEvent ) {

              Component comp;

              if ( (comp = player.getVisualComponent()) != null )

                     add ( "Center", comp );

              if ( (comp = player.getControlPanelComponent()) != null )

                     add ( "South", comp );

              validate();

       }

}

 

Using a simple applet tag, a multimedia stream can easily be embedded in a webpage:

<APPLET CODE=PlayerApplet WIDTH=320 HEIGHT=300>

<PARAM NAME=FILE VALUE="sparkle2.mpeg">

</APPLET>

This will create an applet with an embedded MPEG video stream:


Using the Player with Swing

The Player can be easily used in a Swing application as well.  The following code creates a Swing-based TV capture program with the video output displayed in the entire window:

 

import javax.media.*;

import javax.swing.*;

import java.awt.*;

import java.net.*;

import java.awt.event.*;

import javax.swing.event.*;

 

public class JMFTest extends JFrame {

    Player _player;

    JMFTest() {

        addWindowListener( new WindowAdapter() {

            public void windowClosing( WindowEvent e ) {

                _player.stop();

                _player.deallocate();

                _player.close();

                System.exit( 0 );

            }

        });

          setExtent( 0, 0, 320, 260 );

        JPanel panel = (JPanel)getContentPane();

        panel.setLayout( new BorderLayout() );

        String mediaFile = "vfw://1";

        try {

            MediaLocator mlr = new MediaLocator( mediaFile );

            _player = Manager.createRealizedPlayer( mlr );

            if (_player.getVisualComponent() != null)

            panel.add("Center", _player.getVisualComponent());

            if (_player.getControlPanelComponent() != null)

            panel.add("South", _player.getControlPanelComponent());

        }

        catch (Exception e) {

            System.err.println( "Got exception " + e );

        }

    }

 

    public static void main(String[] args) {

        JMFTest jmfTest = new JMFTest();

        jmfTest.show();

    }

}


Capturing Audio

To capture audio, the specified sampling frequency, sample size and number of channels must be specified.  JMF will attempt to locate any devices which will support this format and return a list of all that match.

 

CaptureDeviceInfo di = null;

Vector deviceList = CaptureDeviceManager.getDeviceList(

new AudioFormat( "linear", 44100, 16, 2 ) );

if ( deviceList.size() > 0 )

       di = (CaptureDeviceInfo)deviceList.firstElement();

Processor p = Manager.createRealizedProcessor(di.getLocator());

DataSource source = p.getDataOutput();

The source object returned from the Processor can then be turned into a Player object by calling Manager.createPlayer().  To capture it to an audio file instead, a DataSink can take the data instead:

DataSink sink;

MediaLocator dest = new MediaLocator("file://output.wav");

try {

       sink = Manager.createDataSink(source, dest);

       sink.open();

       sink.start();

} catch (Exception e) { }

 

The combined source above will take input from the first matching audio device (usually a microphone) and stream it to a wave file on the local filesystem.

 


Capturing Video

Capturing video is identical to capturing audio.  Most video sources have an accompanying audio track that must be encoded as well, so we must create a compound destination file.

 

Format formats[] = new Format[2];

formats[0] = new AudioFormat( "linear", 44100, 16, 2 );

formats[1] = new VideoFormat( "cvid "); // Cinepak video compressor

Processor p;

try {

    p = Manager.createRealizedProcessor( new ProcessorModel( formats, null ) );

} catch ( Exception e ) { }

 

DataSource source = p.getDataOutput();

MediaLocator dest = new MediaLocator( "file://output.mov" );

DataSink filewriter = null;

try {

    filewriter = Manager.createDataSink( source, dest );

    filewriter.open();

    filewriter.start();

} catch ( Exception e ) { }

p.start();

 

This source will create a Quicktime-format file called "output.mov" with an audio track encoded raw and a video track encoded with the Cinepak compressor.

 

 
Job or extra money for students

Search Engine Rank of your blog or websites