3

I am developing an application in BlackBerry in which video is to be played from the SDCard. But I am getting Media Exception such as Media cannot start while another media is active.

When I debugged the app found exception thrown on player.start() line. Player does not starts to play video even on the first time.

I have written simple code as below:

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VideoControl;

import org.jackpot.device.blackberry.streaming.BaseVideoPlaybackScreen;

import net.rim.device.api.system.Application;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.HorizontalFieldManager;

public class PlayerScreen extends BaseVideoPlaybackScreen {
    Bitmap imgTitle = Bitmap.getBitmapResource("ic_topbar_m.png");
    Bitmap bbb;
    Player player;
    private String VIDEO_PATH = "file:///SDCard/videos/";
    FileConnection fconn;
    String videoName = "";
    String languageString = "";

    public PlayerScreen(String videoName, final String languageString) {
        super();
        this.videoName = videoName;
        this.languageString = languageString;
        HorizontalFieldManager hmf = new HorizontalFieldManager(
                Field.USE_ALL_WIDTH) {
            protected void paint(Graphics graphics) {
                bbb = resizeBitmap(imgTitle, Display.getWidth(),
                        imgTitle.getHeight());
                graphics.drawBitmap(0, 0, Display.getWidth(),
                        imgTitle.getHeight(), bbb, 0, 0);

                super.paint(graphics);
            }
        };

        BitmapButtonField btn = new BitmapButtonField("back_black_btn_on.png",
                "back_black_btn.png", 0) {
            public void getKeyPressed() {
                back();

            }
        };

        hmf.add(btn);
        add(hmf);

        try {
            // InputStream is = getClass().getResourceAsStream("/Crab.mp4");
            // System.out.println("IS:::: "+is);
            fconn = (FileConnection) Connector.open(VIDEO_PATH,
                    Connector.READ_WRITE);
            if (fconn.exists()) {
                player = javax.microedition.media.Manager
                        .createPlayer(VIDEO_PATH +  this.videoName
                                +".mp4");
                player.realize();
                VideoControl videoControl = (VideoControl) player
                        .getControl("VideoControl");
                Field videoField = (Field) videoControl.initDisplayMode(
                        VideoControl.USE_GUI_PRIMITIVE,
                        "net.rim.device.api.ui.Field");
                videoControl.setDisplaySize(Display.getWidth(),
                        Display.getHeight());
                videoControl.setVisible(true);

                add(videoField);
                player.start();

                player.addPlayerListener(new PlayerListener() {

                    public void playerUpdate(Player player, String event,
                            Object eventData) {
                        if (event.equals(PlayerListener.STARTED)) {
                            System.out.println("Player Started");
                        } else if (event.equals(PlayerListener.STOPPED)) {
                            System.out.println("Player Stopped");
                            popScreen();
                        } else if (event.equals(PlayerListener.END_OF_MEDIA)) {
                            System.out.println("Player End of Media");
                            popScreen();
                            System.out.println("After POP");
                            System.out.println("OnClose");
                            stopPlayback();
                            synchronized (Application.getEventLock()) {
                                UiApplication.getUiApplication().pushScreen(
                                        new FirstMessagesScreeen(languageString));
                                System.out.println("After Call");
                            }
                        }
                    }

                });
            }
        } catch (MediaException me) {
            System.out.println("MediaException:: " + me);
        } catch (IOException ioe) {
            System.out.println("IOException:: " + ioe);
        }
    }

    protected void back() {
        onClose();
    }

    public boolean onClose() {
        try {
            player.stop();
        } catch (MediaException e) {
            e.printStackTrace();
        }
        return super.onClose();
    }

    public static Bitmap resizeBitmap(Bitmap image, int width, int height) {
        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();
        int rgb[] = new int[imageWidth * imageHeight];
        image.getARGB(rgb, 0, imageWidth, 0, 0, imageWidth, imageHeight);
        int rgb2[] = rescaleArray(rgb, imageWidth, imageHeight, width, height);
        Bitmap temp2 = new Bitmap(width, height);
        temp2.setARGB(rgb2, 0, width, 0, 0, width, height);
        return temp2;
    }

    private static int[] rescaleArray(int[] ini, int x, int y, int x2, int y2) {
        int out[] = new int[x2 * y2];
        for (int yy = 0; yy < y2; yy++) {
            int dy = yy * y / y2;
            for (int xx = 0; xx < x2; xx++) {
                int dx = xx * x / x2;
                out[(x2 * yy) + xx] = ini[(x * dy) + dx];
            }
        }
        return out;
    }

    public boolean keyDown(int keycode, int time) {
        int key = Keypad.key(keycode);
        if (key == Keypad.KEY_ESCAPE) {
            back();
            return true;
        }
        return false;
    }

    protected void stopPlayback() {
        try {
            if (player != null)
                player.stop();
        } catch (Exception ex) {
            System.out.println("Player Stopped::: " + ex.toString());
        }
    }
}

Please let me know where i am lacking. The videoName comes from the previous class having extension .mp4.

gnat
  • 6,213
  • 108
  • 53
  • 73
AkashG
  • 7,868
  • 3
  • 28
  • 43
  • 1
    **Where** are you getting the exception thrown (which line)? Do you see this exception only the *second* time you try to play the video? – Nate Jan 08 '13 at 06:23
  • @Nate when i debugged the app found exception thrown on player.start() line.player does not starts to play video even on the first time. – AkashG Jan 08 '13 at 06:28
  • Have you tried removing the `fconn = (FileConnection) Connector.open()` line, and the `fconn.exists()` line after it? I tried this with an audio file, and it didn't make a difference, but I'm wondering if keeping the file connection open is causing problems when the player tries to start. Just something to try ... also, can you tell us which device and OS version you're using? – Nate Jan 08 '13 at 08:21
  • i have BlackBerry Bold 9780 os 6.0, i haven't tried removing fconn line, will try this – AkashG Jan 08 '13 at 08:31
  • Actually, now that I read your code again, you are opening the FileConnection on the root **directory** for all your videos. I thought maybe you were testing whether the *individual* video file existed. So, I would think that's probably not a problem. Sorry. – Nate Jan 08 '13 at 08:43
  • its ok @Nate, but i am stuck in a huge problem of video not playing, please take me out of this.. – AkashG Jan 08 '13 at 09:41
  • If you completely reboot the device, do you still see the exception the **first** time you run? – Nate Jan 08 '13 at 09:43
  • yes, i un-installed the app several times restarted the device several times than also player does not starts playing video – AkashG Jan 08 '13 at 10:39
  • I don't know. The only similar discussion I can find is [this one on blackberry.com](http://supportforums.blackberry.com/t5/Java-Development/Getting-quot-Media-Exception-Media-cannot-start-while-another/td-p/1244839). In this situation, the other experts ask if there is another part of your code that's playing video, or audio. Is this code above the only `Player` instance in your app? – Nate Jan 09 '13 at 05:13
  • yeah there is camera opening in the previous screen.this might be the reason why i must be getting this exception.Thankx @Nate for the help – AkashG Jan 09 '13 at 09:51
  • That would almost certainly be the problem. – Nate Jan 09 '13 at 10:13

0 Answers0