我正在 BlackBerry 中开发一个应用程序,其中视频将从 SDCard 播放。但我越来越Media Exception
像Media cannot start while another media is active
。
当我调试应用程序时发现player.start()
在线抛出异常。即使是第一次播放器也不会开始播放视频。
我写了如下简单的代码:
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());
}
}
}
请让我知道我缺少的地方。videoName
来自上一个具有扩展名的类.mp4
。