×
Namespaces

Variants
Actions
Revision as of 03:44, 13 July 2012 by hamishwillee (Talk | contribs)

How to set video playback rate in Java ME for Series 40

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Code ExampleTested with
SDK: Nokia SDK 1.0 for Java
Devices(s): Nokia 300, Nokia 3610 fold, Nokia 3710 fold, Nokia C3-00, Nokia C3-01, Nokia X2-00, Nokia X2-01, Nokia X3-02
Compatibility
Platform(s): Series 40 5th Edition, Series 40 6th Edition, Series 40 6th Edition, Feature Pack 1, Java Runtime 1.0.0 for Series 40
Series 40
Series 40 6th Edition FP1
Series 40 6th Edition (initial release)
Series 40 5th Edition FP1
Series 40 5th Edition (initial release)
Device(s): Series 40 devices with MMAPI (JSR-135)
Article
Keywords: MMAPI (JSR-135), RateControl.getMaxRate(), RateControl.getMinRate(), RateControl.setRate()
Created: r2j7 (25 Nov 2011)
Updated: r2j7 (25 Nov 2011)
Reviewed: r2j7 (25 Nov 2011)
Last edited: hamishwillee (13 Jul 2012)

Contents

Overview

This article shows how to set video playback rate in Java ME for Series 40 devices.

In order to show the effect of choosing video playback rate as easily as possible, the test case uses only the maximum and minimum rates available for video playback.

After the application startup, end-user selects the preferred video format for playback. In this example, 3GPP and MP4 formats are used. After the selection the application checks whether the device supports RateControl: if yes, the application adds two Command options under the Options list menu for setting the preffered rate for video playback, Max Rate and Min Rate (both depend on the device specific values which are acquired by using RateControl.getMaxRate() and RateControl.getMinRate() methods). Finally, user starts the video playback by pressing Start.

Device Requirements

This example can be tested on Series 40 devices which support Mobile Media API (JSR-135) with VideoControl and RateControl.

For more information about Java ME support on Nokia device models, please visit the device specifications page on Nokia Developer web site here: Device specifications on Nokia Developer web site . The Java ME support including the supported Java APIs can be checked for each device by clicking the 'APIs' tab when viewing a device specification.

Source file: MMAPI_RateControlTest.java

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.*;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.*;
import javax.microedition.midlet.*;
 
public class RateControlTest extends MIDlet implements CommandListener, PlayerListener, Runnable{
 
private Display display;
private Form myForm;
 
private Thread streamingThread;
private Player myPlayer;
private VideoControl videoControl;
private RateControl rateControl;
private Command cmdExit, cmd3GPPvideo, cmdMP4video, cmdStart, cmdSetMaxRate, cmdSetMinRate, cmdStopVideo;
private String video;
private String codec;
int rateValue;
InputStream is;
 
public RateControlTest() {
 
display = Display.getDisplay(this);
myForm=new Form ("Video playback test");
cmdExit = new Command("Exit", Command.EXIT,0);
cmd3GPPvideo = new Command("3GPP video", Command.OK,0);
cmdMP4video = new Command("MP4 video", Command.OK,0);
cmdStart = new Command ("Start", Command.OK, 0);
cmdStopVideo = new Command("Stop video", Command.STOP, 0);
is = null;
addVideoCommand(cmdExit);
addVideoCommand(cmd3GPPvideo);
addVideoCommand(cmdMP4video);
setStartText();
myForm.setCommandListener(this);
 
}
 
public void setStartText(){
 
myForm.append("Start the test by selecting video format.\n");
 
}
 
public void startApp() {
 
display.setCurrent(myForm);
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
 
stopVideo();
}
 
public void stopVideo(){
 
if (is != null) {
 
try {
is = null;
// is.close();
 
} catch (Exception ex) {
myForm.append(ex.toString());
 
}
 
}
 
if(myPlayer != null) {
 
try {
myPlayer.stop();
myPlayer.close();
myPlayer = null;
}
catch( Exception e ) {
}
}
 
}
 
public void commandAction(Command cmd, Displayable display) {
 
if(cmd == cmdExit) {
destroyApp(true);
notifyDestroyed();
}
// Set the 3GPP video and
else if(cmd == cmd3GPPvideo) {
video = "/12072011028.3gp";
codec = "video/3gpp";
myForm.append("3GPP video set. \n");
loadVideo();
}
else if(cmd == cmdMP4video) {
video = "/c10b317409190aa74e1fd3e34a348060.mp4";
codec = "video/mp4";
myForm.append("MP4 video set.\n");
loadVideo();
}
else if (cmd == cmdStart) {
myForm.deleteAll();
removeCommand(cmdSetMaxRate);
removeCommand(cmdSetMinRate);
myForm.addCommand(cmdStopVideo);
myForm.append((Item)videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null));
 
streamingThread = new Thread(this);
streamingThread.start();
}
else if (cmd == cmdSetMaxRate){
rateValue = rateControl.getMaxRate();
rateControl.setRate(rateValue);
}
else if (cmd == cmdSetMinRate){
rateValue = rateControl.getMinRate();
rateControl.setRate(rateValue);
}
else if (cmd == cmdStopVideo) {
 
stopVideo();
 
}
}
 
public void addVideoCommand(Command cmd) {
 
myForm.addCommand(cmd);
}
 
public void removeCommand(Command cmd) {
 
myForm.removeCommand(cmd);
 
}
 
// Get video data and implement a Player for video
 
public void loadVideo(){
 
removeCommand(cmd3GPPvideo);
removeCommand(cmdMP4video);
 
try{
 
is = getClass().getResourceAsStream(video);
 
myPlayer = Manager.createPlayer(is, codec);
myPlayer.addPlayerListener(this);
myPlayer.realize();
// Get the video control and set it to the current display.
videoControl = (VideoControl)myPlayer.getControl("VideoControl");
if(videoControl == null)
{
myForm.append("VideoControl not supported. Cannot playback video.");
return;
}
 
rateControl = (RateControl)myPlayer.getControl("RateControl");
 
if(rateControl != null){
 
setRateControl();
}
 
else if(rateControl == null){
 
myForm.append("RateControl not available. Cannot set rate for video.");
}
myPlayer.prefetch();
 
addVideoCommand(cmdStart);
}
catch (IOException ex) {
myForm.append(ex.toString());
 
}
catch (MediaException ex) {
myForm.append(ex.toString());
 
}
}
 
// Setting RateControl commands
 
public void setRateControl() {
 
myForm.append("RateControl is available. Please set preferred rate from 'Options' and "
+ "then press 'Start'.");
cmdSetMaxRate = new Command("Max Rate", Command.OK, 1);
cmdSetMinRate = new Command("Min Rate", Command.OK, 1);
myForm.addCommand(cmdSetMaxRate);
myForm.addCommand(cmdSetMinRate);
}
 
/* Once video is stopped, remove rate, start and stop commands from the Form,
add exit and video commands to the Form.
*/

 
public void videoStopped(){
 
myForm.deleteAll();
removeCommand(cmdStopVideo);
addVideoCommand(cmdExit);
addVideoCommand(cmd3GPPvideo);
addVideoCommand(cmdMP4video);
setStartText();
 
}
 
public void playerUpdate(Player player, String event, Object eventData) {
 
if(event.equals(PlayerListener.END_OF_MEDIA))
{
stopVideo();
videoStopped();
}
 
if(event.equals(PlayerListener.STARTED))
{
removeCommand(cmdStart);
}
 
if(event.equals(PlayerListener.STOPPED))
{
videoStopped();
}
}
 
public void run() {
try {
myPlayer.start();
} catch (MediaException ex) {
myForm.append(ex.toString());
}
}
 
}

Supplementary material

Executables and source files are available in File:MMAPI RateControlTest.zip.

98 page views in the last 30 days.
×