Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 239
Registered: ‎07-26-2010
My Device: Z10
My Carrier: Telkomsel

How to stop video playing properly

I use this code to display video in full screen:

import java.io.IOException;

import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import javax.microedition.media.control.VolumeControl;

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

public class VideoPlayerScreen extends MainScreen {

	private Player player;
	private boolean playing = false;
	
	public VideoPlayerScreen(){
		try {
			player = Manager.createPlayer("file:///SDCard/BlackBerry/videos/Sweet Child O' Mine.mp4");
			player.realize();
			VideoControl videoControl = (VideoControl) player.getControl("VideoControl");
			Field videoField = (Field) videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,
					"net.rim.device.api.ui.Field");
			
			add(videoField);
			
			VolumeControl volumeControl = (VolumeControl) player.getControl("VolumeControl");
			volumeControl.setLevel(30);
			
			player.start();
			playing = true;
		}
		catch (MediaException me){
			Dialog.alert(me.toString());
		}
		catch (IOException ioe){
			Dialog.alert(ioe.toString());
		}
	}
	
	protected boolean keyChar(char c, int status, int time) {
		if (c == Characters.ESCAPE){
			if (playing){
				try {
					player.close();
					player.deallocate();
					player.stop();
				}
				catch (MediaException me){
					Dialog.alert(me.toString());
				}
				catch (IllegalStateException ise){
					Dialog.alert(ise.toString());
				}
			}
		}
		return super.keyChar(c, status, time);
	}
}

 

Of course my intention is if the back button is pressed, then the video player will be stopped. In reality, an IllegalStateException is triggered.

ise.jpg

 

Is there a better method to stop the video, so instead of triggering an exception and turning the screen into black, the last frame of the video is still shown on the screen?