题
我需要一个视频屏幕,在视频播放下,我想显示两行文本,如下所示。
为此,我正在使用以下代码。
public final class PlayVideoScreen extends MainScreen {
private Player player;
private VideoControl videoControl;
public PlayVideoScreen() {
// ms.setTitle(new LabelField("Let's play some video..."));
LabelField lf = new LabelField("Video Play");
try {
// Create a new Player pointing to the video file.
// This can use any valid URL.
player = javax.microedition.media.Manager
.createPlayer("file:///SDCard/BlackBerry/videos/sample.avi");
player.realize();
// Create a new VideoControl.
videoControl = (VideoControl) player.getControl("VideoControl");
// Initialize the video mode using a Field.
Field videoField = (Field) videoControl.initDisplayMode(
VideoControl.USE_GUI_PRIMITIVE,
"net.rim.device.api.ui.Field");
add(videoField);
VolumeControl volume = (VolumeControl) player
.getControl("VolumeControl");
volume.setLevel(30);
player.start();
// Set the video control to be visible.
// videoControl.setVisible(true);
} catch (MediaException me) {
Dialog.alert(me.toString());
} catch (IOException ioe) {
Dialog.alert(ioe.toString());
}
add(lf);
LabelField lf2 = new LabelField("How r you?");
add(lf2);
}
public boolean onClose() {
// Clean up the player resources.
player.close();
videoControl.setVisible(false);
close();
return true;
}
}
现在可能是视频的高度,视图正在滚动,文本仅在滚动后可见。我使用的设备屏幕尺寸为 320X240px。我什至用 320X150px 的视频进行了测试。但是,尽管视频的上方和底部有大量可用空间,但如果不滚动,文本是不可见的。我的代码有什么问题?怎么解决呢?
解决方案
有多种方法可以解决您的问题。在你的情况下最简单的是使用 主屏幕#setStatus 它设置此屏幕状态部分的内容。
而不是添加 LabelField
直接添加,如下:
VerticalFieldManager vfm = new VerticalFieldManager();
vfm.add(lf);
vfm.add(lf2);
setStatus(vfm);
编辑: 另一种解决方案是自行布局和定位子字段。为此,您可以覆盖 sublayout()
PlayVideoScreen 的或引入另一个管理器(VerticalFieldManager
),向其中添加所有字段(视频和标签)并覆盖其 sublayout()
方法。
以下是经过上述修改的代码
public PlayVideoScreen() {
super(NO_VERTICAL_SCROLL);
// ms.setTitle(new LabelField("Let's play some video..."));
final LabelField lf = new LabelField("Video Play");
try {
// Create a new Player pointing to the video file.
// This can use any valid URL.
player = javax.microedition.media.Manager
.createPlayer("file:///SDCard/BlackBerry/videos/sample.avi");
player.realize();
// Create a new VideoControl.
videoControl = (VideoControl) player.getControl("VideoControl");
// Initialize the video mode using a Field.
final Field videoField = (Field) videoControl.initDisplayMode(
VideoControl.USE_GUI_PRIMITIVE,
"net.rim.device.api.ui.Field");
VolumeControl volume = (VolumeControl) player
.getControl("VolumeControl");
volume.setLevel(30);
player.start();
// Set the video control to be visible.
// videoControl.setVisible(true);
final LabelField lf2 = new LabelField("How r you?");
VerticalFieldManager vfm = new VerticalFieldManager(NO_VERTICAL_SCROLL) {
protected void sublayout(int maxWidth, int maxHeight) {
int heightLeft = maxHeight;
// layout the children fields
layoutChild(lf, maxWidth, heightLeft);
heightLeft -= lf.getHeight();
layoutChild(lf2, maxWidth, heightLeft);
heightLeft -= lf2.getHeight();
layoutChild(videoField, maxWidth, heightLeft);
// position the children fields
int yPos = 0;
setPositionChild(videoField, 0, yPos);
yPos += videoField.getHeight();
setPositionChild(lf, 0, yPos);
yPos += lf.getHeight();
setPositionChild(lf2, 0, yPos);
setExtent(maxWidth, maxHeight);
};
};
vfm.add(videoField);
vfm.add(lf);
vfm.add(lf2);
add(vfm);
} catch (MediaException me) {
Dialog.alert(me.toString());
} catch (IOException ioe) {
Dialog.alert(ioe.toString());
}
}
不隶属于 StackOverflow