Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Video Player MIDlet Example 
 

This example is all about how to play the MPEG files on your mobile device. To play the video file we have to import the javax.microedition.media.control package and certain sub-package that are required for the application.

 

Video Player MIDlet Example

                         

This example is all about how to play the MPEG files on your mobile device. To play the video file we have to import the javax.microedition.media.control package and certain sub-package that are required for the application. 

Given are the classes and interfaces that are available in the above mentioned package.

 

 

 

 

 

javax.microedition.media

 Interfaces 

  • Control 
  • Controllable 
  • Player 
  • PlayerListener 
  • TimeBase 

Classes 

  • Manager 

Exceptions 

  • MediaException

 

javax.microedition.media.control

 Interfaces 

  • FramePositioningControl 
  • GUIControl 
  • MetaDataControl 
  • MIDIControl 
  • PitchControl 
  • RateControl 
  • RecordControl 
  • StopTimeControl 
  • TempoControl 
  • ToneControl 
  • VideoControl 
  • VolumeControl

The Application is as follows:

 

 

 

Source Code of VideoPlayer.java

 

import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

public class VideoPlayer extends MIDlet implements CommandListener, 
             PlayerListener 
{  
  private Display display;
  private List itemList;
  private Form form;
  private Command stop, pause, start;
  private Hashtable items, itemsInfo;
  private Player player;

  public VideoPlayer() {
    display = Display.getDisplay(this);
    itemList = new List("Select an item to play", List.IMPLICIT);
    stop = new Command("Stop", Command.STOP, 1);
    pause = new Command("Pause", Command.ITEM, 1);
    start = new Command("Start", Command.ITEM, 1);
    form = new Form("Playing video");
    form.addCommand(stop);
    form.addCommand(pause);
    form.setCommandListener(this);
    items = new Hashtable();
    itemsInfo = new Hashtable();

    items.put("SpringWaterFall...""file://SpringWaterFall.mpg");
    itemsInfo.put("SpringWaterFall...""video/mpeg");
    
    items.put("helloboy...""file://helloboy.mpg");
    itemsInfo.put("helloboy...""video/mpeg");

    items.put("pilgrim...""file://pilgrim.mpg");
    itemsInfo.put("pilgrim...""video/mpeg");

    items.put("pirates...""file://pirates.mpg");
    itemsInfo.put("pirates...""video/mpeg");

    items.put("pythag1...""file://pythag1.mpg");
    itemsInfo.put("pythag1...""video/mpeg");
    
    items.put("CarelessEnglish...""file://CarelessEnglish.mpg");
    itemsInfo.put("CarelessEnglish...""video/mpeg");
  }

  public void startApp() {
    for(Enumeration en = items.keys(); en.hasMoreElements();) {
      itemList.append((String)en.nextElement()null);
    }
    itemList.setCommandListener(this);
    display.setCurrent(itemList);
  }

  public void pauseApp() {
    try {
      if(player != nullplayer.stop();
    catch(Exception e) {}
  }

  public void destroyApp(boolean unconditional) {
    if(player != nullplayer.close();
  }

  public void commandAction(Command c, Displayable d){
    if(instanceof List) {
      List list = ((List)d);      
      String key = list.getString(list.getSelectedIndex());
      try {
        playAudio((String)items.get(key), key);
      catch (Exception e) {
        System.err.println("Unable to play: " + e);
        e.printStackTrace();
      }
    else if(instanceof Form){
      try {
        if(c == stop){
          player.close();
          display.setCurrent(itemList);
          form.removeCommand(start);
          form.addCommand(pause);
        else if(c == pause){
          player.stop();
          form.removeCommand(pause);
          form.addCommand(start);
        else if(c == start){
          player.start();
          form.removeCommand(start);
          form.addCommand(pause);
        }
      catch(Exception e) {
        System.err.println(e);
      }
    }
  }

  private void playAudio(String locator, String keythrows Exception {
    String file = locator.substring(locator.indexOf("file://"6
    locator.length
());
    player = Manager.createPlayer(getClass().getResourceAsStream(file)
             (
String)itemsInfo.get(key));
    player.addPlayerListener(this);
    player.setLoopCount(-1);
    player.prefetch();
    player.realize();
    player.start();
  }

  public void playerUpdate(Player player, String event, Object eventData) {
    if(event.equals(PlayerListener.STARTED&& new Long(0L).equals((Long)
       eventData
)) {
      VideoControl vc = null;
      if((vc = (VideoControl)player.getControl("VideoControl")) != null) {
        Item videoDisp = (Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null);
        form.append(videoDisp);
      }
      display.setCurrent(form);
    else if(event.equals(PlayerListener.CLOSED)) {
      form.deleteAll();
    }
  }
}

 

Download Source Code

                         

» View all related tutorials
Related Tags: c file list table time hash mobile object map screen hashtable maps display key value this id keys oo wav

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

perfect example but I don't undresting how i can feel the url of video in the variable string
how is it the organisation in eclips ?

Posted by mohamed on Saturday, 01.31.09 @ 20:04pm | #84364

can u please answer where to put video files in both mobile device and in which directory

Posted by gunjan on Tuesday, 12.9.08 @ 23:15pm | #82577

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.