当前位置: 首页 > 工具软件 > Java MPlayer > 使用案例 >

java 调用 mplayer

张兴旺
2023-12-01
java: [url]http://beradrian.wordpress.com/2008/01/30/jmplayer/[/url]

qt: [url]http://www.embedu.org/Column/Column140.htm[/url]
[url]http://tingxx.ycool.com/post.2028303.html[/url]


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;

class LineRedirecter extends Thread {
/** The input stream to read from. */
private InputStream in;
/** The output stream to write to. */
private OutputStream out;

/**
* @param in the input stream to read from.
* @param out the output stream to write to.
* @param prefix the prefix used to prefix the lines when outputting to the logger.
*/
LineRedirecter(InputStream in, OutputStream out) {
this.in = in;
this.out = out;
}

public void run()
{
try {
// creates the decorating reader and writer
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
PrintStream printStream = new PrintStream(out);
String line;

// read line by line
while ( (line = reader.readLine()) != null) {
printStream.println(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

}



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;


public class MyPlayer {

public static void main(String[] args) {
try{

// create the piped streams where to redirect the standard output and error of MPlayer
// specify a bigger pipesize than the default of 1024
PipedInputStream readFrom = new PipedInputStream(256*1024);
PipedOutputStream writeTo = new PipedOutputStream(readFrom);
BufferedReader mplayerOutErr = new BufferedReader(new InputStreamReader(readFrom));
Process mplayerProcess = Runtime.getRuntime().exec("e:/player.exe c:/media/video/1.mov");//-quiet -idle -slave
// create the threads to redirect the standard output and error of MPlayer
new LineRedirecter(mplayerProcess.getInputStream(), writeTo).start();
new LineRedirecter(mplayerProcess.getErrorStream(), writeTo).start();

// the standard input of MPlayer
PrintStream mplayerIn = new PrintStream(mplayerProcess.getOutputStream());

mplayerIn.print("set_property time_pos 300");
mplayerIn.print("\n");
mplayerIn.flush();


mplayerIn.print("loadfile \"c:/media/video/1.mov\" 0");
mplayerIn.print("\n");
mplayerIn.flush();


mplayerIn.print("pause");
mplayerIn.print("\n");
mplayerIn.flush();


mplayerIn.print("get_property length");
mplayerIn.print("\n");
mplayerIn.flush();


String answer;
int totalTime = -1;
try {
while ((answer = mplayerOutErr.readLine()) != null) {
if (answer.startsWith("ANS_length=")) {
totalTime = Integer.parseInt(answer.substring("ANS_length=".length()));
break;
}
}
}
catch (IOException e) {
}
System.out.println("========"+totalTime);



try {
mplayerProcess.waitFor();
}
catch (InterruptedException e) {}

}catch (Exception e) {
}
}
}



例子为 windows 版 mplayer ,此例用基本调用,如有需要请查看 mplayer 手册 [url]http://wuzijingaip.iteye.com/admin/blogs/545229[/url]



java开源万能播放器里的一段



/*
* 作者:lightp2p@gmail.com
* 网站:http://hi.baidu.com/mqlayer
*/

package player;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.peer.ComponentPeer;

import javax.swing.JPanel;


public class VideoPanel extends JPanel{
private static final long serialVersionUID = 4417214835406666167L;
Player player;
Canvas canvas;
VideoPanel(Player pl){
player=pl;
setBackground(new Color(3,3,3));
setLayout(new VideoLayout(player));
canvas=new Canvas();
add(canvas);
canvas.setName("canvas");
canvas.setBackground(new Color(3,3,3));
}

//获取组件的window handle
@SuppressWarnings("deprecation")
long getWid(){
long wid=-1;
try {
Class<?> cl = Class.forName("sun.awt.windows.WComponentPeer");
java.lang.reflect.Field f = cl.getDeclaredField("hwnd");
f.setAccessible(true);
ComponentPeer peer = canvas.getPeer();
wid = f.getLong(peer);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("wid:" + wid );
return wid;
}
}


 类似资料: