我想使用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();
}
}
求高人指教...
之前有使用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();
}
}
求高人指教...
那要如何实现播放各种类型的视频呢?mplayer只有这一种方式可以嵌入控件播放,而且我不许要获取事件,我只要它能显示出来播放即可。
能传受下经验嘛?或许windows和Linux是同等道理。?如果可以的话先教我下windows下如何嵌入?
Thank you,我认真看下。我问下您那个例子是不是用Ole实现的?
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(); }}
可是为了人道我没给您给分....加个好友吧...以后有不会的问题可以请教您我刚开始接触swt之前是做web 开发的