求指教SWT.EMBEDDED 嵌入mplayer何解?

xiechunmei13 2011-12-12 04:25:41
我想使用swt来播放视频,其中mplayer是经过各种参考后最终决定使用的播放器。
之前有使用gstreamer来播放视频,但是在ubuntu下各种版本问题,各种依赖问题一大堆,总是不能完整的播放,并且性能低下。cpu占有率比mplayer高出至少2倍。
使用mplayer的缺点就是我之前的swing框架不能使用,得改为swt,因为mplayer想要无gui播放就得用slave模式,而slave模式必须有窗体句柄才能将mplayer嵌入到窗体中,而swt则刚好为我们提供了句柄这个属性。
各种前期工作完成后,swt在windows下做了个测试,可以将mplayer嵌入到java的窗体中但是窗体背景色必须为黑色。
可将相同代码移植到ubuntu下却无法实现相同效果。
ubuntu下设置为SWT.EMBEDDED后无法显示窗体的背景色,且只能听到Mplayer播放的声音不能看到画面。



import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Player2
{
//You'll have to edit these paths to something useful.
private static final String MPLAYER="/usr/bin/mplayer";

private static final String VIDEO_FILE ="/usr/A.rmvb";

private static Color wColor;
public static Process setFile(final File pFile, final int hWnd)
throws IOException
{
//final String[] lCmds = new String[] { MPLAYER, "-wid", String.valueOf(hWnd), "-nokeepaspect", "-x", "" + 352, "-y", "" + 240, "-geometry", "0:0", "-colorkey", "0x101010", "-vo", "directx", "-quiet", "-loop", "0", "-playlist", pFile.getAbsolutePath()};

final String[] lCmds = new String[] {
MPLAYER,
"-wid", String.valueOf(hWnd),
"-nokeepaspect",
"-geometry", "0:0",
"-colorkey", "0x101010",
"-vo", "directx",
pFile.getAbsolutePath()
};


/*
final String[] lCmds = new String[] {
MPLAYER,
"-wid", String.valueOf(hWnd),
pFile.getAbsolutePath()
};
***/

final Process lProcess = Runtime.getRuntime().exec(lCmds);
final InputStream stderr = lProcess.getErrorStream();
final InputStream stdin = lProcess.getInputStream();

new Thread(new Runnable() {
public void run() {
try {
final BufferedReader lReader = new BufferedReader(new InputStreamReader(stderr, "UTF-8"));
for (String l = lReader.readLine(); l != null; l = lReader.readLine()) {
System.out.println(l);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}).start();

new Thread(new Runnable() {
public void run() {
try {
final BufferedReader lReader = new BufferedReader(new InputStreamReader(stdin, "UTF-8"));
for (String l = lReader.readLine(); l != null; l = lReader.readLine()) {
System.out.println(l);
}
} catch (Throwable t) {
t.printStackTrace();
}
}

}).start();

return lProcess;
}

public static void main(String[] pArgs)
throws Exception
{
final Display wDisplay = new Display();
final Shell wShell = new Shell(wDisplay, SWT.SHELL_TRIM);
final Color bkColor = new Color(null,0x10,0x10,0x10);
final FillLayout wLayout = new FillLayout();
final Composite videoComposite;
videoComposite = new Composite(wShell, SWT.EMBEDDED);
videoComposite.setLayout(wLayout);
videoComposite.setBackground(bkColor);
videoComposite.setBounds(new Rectangle(0, 51, 720, 480));

wShell.setLayout(wLayout);
wShell.setSize(800, 600);
wShell.layout();
wShell.setVisible(true);

final Process lProcess = setFile(new File(VIDEO_FILE),
videoComposite.handle);

while ( !wShell.isDisposed() ) {
if ( !wDisplay.readAndDispatch() ) {
wDisplay.sleep();
}
}

lProcess.destroy();
}
}








求高人指教...
...全文
224 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyytttttt 2013-05-11
  • 打赏
  • 举报
回复
和楼上的问题一样啊!!!盼望lz解答!!!
long2684487 2012-09-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
该问题已解决,现在附上解决方案和代码。


Java code




import java.io.BufferedReader;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

……
[/Quote]


您修改前的代码我还能播放出声音,但是不能出视频,上面的这些代码连声音都没了,我是在windows环境下, han=videoComposite.embeddedHandle; 这行代码改成windows下的 han=videoComposite.handle;这是什么原因呢?能否赐教?谢谢!



xiechunmei13 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 chensuper 的回复:]

有我当年的研究精神,呵呵……当年我折腾mediaplayer的问题,可是折腾了好久,因为swt的一个bug而无法播放,后来还是自己改的swt的代码。
[/Quote]
前段时间研究gstreamer,cpu占有率太高了,播一段时间画面就会完全没动静..不知道是我们的机子配置太低还是我gstreamer安装有问题...最后还是决定用mplayer..毕竟mplayer是个不错的东西...虽然话称gstreamer在linux下就是windows下的mplayer..可还是觉得没有mplayer好使
xiechunmei13 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 chensuper 的回复:]

有我当年的研究精神,呵呵……当年我折腾mediaplayer的问题,可是折腾了好久,因为swt的一个bug而无法播放,后来还是自己改的swt的代码。
[/Quote]

可是为了人道我没给您给分....加个好友吧...以后有不会的问题可以请教您。。。我刚开始接触swt。。。之前是做web 开发的
chensuper 2011-12-15
  • 打赏
  • 举报
回复
有我当年的研究精神,呵呵……当年我折腾mediaplayer的问题,可是折腾了好久,因为swt的一个bug而无法播放,后来还是自己改的swt的代码。
xiechunmei13 2011-12-15
  • 打赏
  • 举报
回复
该问题已解决,现在附上解决方案和代码。




import java.io.BufferedReader;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;



import org.eclipse.swt.SWT;

import org.eclipse.swt.graphics.Color;

import org.eclipse.swt.graphics.Rectangle;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;



public class Player2

{

//You'll have to edit these paths to something useful.

private static final String MPLAYER="/usr/bin/mplayer";



private static final String VIDEO_FILE ="/usr/A.rmvb";



public static Process setFile(final File pFile, final int hWnd)

throws IOException

{

//调用mplayer命令行

String[] cmd = new String[] {

MPLAYER,//mplayer路径

"-vo","x11",//linux下只能用x11和xv还有一个神码的,windows下用directX

"-identify", //输出详情

"-slave", //slave模式播放

"-wid", String.valueOf(hWnd),//视频窗口的 handle

"-colorkey", "0x010101",//视频窗口的背景色

"-osdlevel", String.valueOf(1),//osd样式

VIDEO_FILE//播放文件路径

};



final Process lProcess = Runtime.getRuntime().exec(cmd);

final InputStream stderr = lProcess.getErrorStream();

final InputStream stdin = lProcess.getInputStream();



new Thread(new Runnable() {

public void run() {

try {

final BufferedReader lReader = new BufferedReader(new InputStreamReader(stderr, "UTF-8"));

for (String l = lReader.readLine(); l != null; l = lReader.readLine()) {

System.out.println(l);

}

} catch (Throwable t) {

t.printStackTrace();

}

}

}).start();



new Thread(new Runnable() {

public void run() {

try {

final BufferedReader lReader = new BufferedReader(new InputStreamReader(stdin, "UTF-8"));

for (String l = lReader.readLine(); l != null; l = lReader.readLine()) {

System.out.println(l);

}

} catch (Throwable t) {

t.printStackTrace();

}

}



}).start();



return lProcess;

}



public static void main(String[] pArgs)

throws Exception

{

final Display wDisplay = new Display();

final Shell wShell = new Shell(wDisplay, SWT.SHELL_TRIM);

//在mplayer中你设置背景色为多少,在这里你就应该设置播放窗体的背景色为多少。

final Color bkColor = new Color(null,0x01,0x01,0x01);

final FillLayout wLayout = new FillLayout();

Composite videoComposite;

videoComposite = new Composite(wShell, SWT.EMBEDDED);

videoComposite.setLayout(wLayout);

videoComposite.setBackground(bkColor);

videoComposite.setBounds(new Rectangle(0, 51, 720, 480));

wShell.setLayout(wLayout);

wShell.setSize(800, 600);

wShell.layout();

wShell.setVisible(true);

int han=0;

//handle ,windows下使用.handle来获取窗体句柄。但是linux下使用embeddedHandle来获取句柄,否则会出现

/**

* X11 error: BadDrawable (invalid Pixmap or Window parameter)

* X11 error: BadWindow (invalid Window parameter)

* 这样的错误

*/

han=videoComposite.embeddedHandle;

System.out.println(han);

final Process lProcess = setFile(new File(VIDEO_FILE),

han);



while ( !wShell.isDisposed() ) {

if ( !wDisplay.readAndDispatch() ) {

wDisplay.sleep();

}

}



lProcess.destroy();

}

}










xiechunmei13 2011-12-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 chensuper 的回复:]

http://mchristoff.com/2009/12/using-the-air-2-0-nativeprocess-api-to-control-mplayer/
[/Quote]

Thank you,我认真看下。我问下您那个例子是不是用Ole实现的?
chensuper 2011-12-12
  • 打赏
  • 举报
回复
帮你找了个教程……虽然是flash实现的,不过大同小异。
xiechunmei13 2011-12-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 chensuper 的回复:]

Linux下的没研究过,Windows下的我倒是会。
[/Quote]

能传受下经验嘛?或许windows和Linux是同等道理。?如果可以的话先教我下windows下如何嵌入?
chensuper 2011-12-12
  • 打赏
  • 举报
回复
Linux下的没研究过,Windows下的我倒是会。


xiechunmei13 2011-12-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chensuper 的回复:]

这种实现方法太弱了吧,命令行……你完全无法获得各种事件。应该当做控件来进行处理。
[/Quote]

那要如何实现播放各种类型的视频呢?mplayer只有这一种方式可以嵌入控件播放,而且我不许要获取事件,我只要它能显示出来播放即可。
chensuper 2011-12-12
  • 打赏
  • 举报
回复
这种实现方法太弱了吧,命令行……你完全无法获得各种事件。应该当做控件来进行处理。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧