我正在尝试编写一个Java程序,允许一个用户充当服务器并流式传输他们的桌面(视频和音频),然后其他用户充当客户端并观看其桌面的实时流(类似于Twitch ,Webex,Skype屏幕共享等)。我正在使用VLCJ,虽然我没有承诺使用它,所以如果有更好的解决方案,我会全力以赴。这是代码,它是从我在下面提供的链接中复制的:
var product = db.Products.Where(t => t.name.Contains(productname)).FirstOrDefault();
我将“screen://”作为参数传递给该程序。当我运行代码时,我收到此错误消息:
package test.java;
import uk.co.caprica.vlcj.discovery.NativeDiscovery;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.headless.HeadlessMediaPlayer;
import test.java.VlcjTest;
/**
* An example of how to stream a media file over HTTP.
* <p>
* The client specifies an MRL of <code>http://127.0.0.1:5555</code>
*/
public class StreamHttp extends VlcjTest {
//when running this it requires an MRL (Media Resource Locator)
//fancy term for saying the file you want to stream. This could be a url to another
//location that streams media or a filepath to a media file you want to stream
//on the system you are running this code on.
public static void main(String[] args) throws Exception {
new NativeDiscovery().discover();
if(args.length != 1) {
System.out.println("Specify a single MRL to stream");
System.exit(1);
}
//the media you are wanting to stream
String media = args[0];
//this is the IP address and port you are wanting to stream at
//this means clients will connect to http://127.0.0.1:5555
//to watch the stream
String options = formatHttpStream("127.0.0.1", 5555);
System.out.println("Streaming '" + media + "' to '" + options + "'");
//this creates a the actual media player that will make calls into the native
//vlc libraries to actually play the media you supplied. It does it in
//a headless fashion, as you are going to stream it over http to be watched
//instead of playing it locally to be watched.
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(args);
HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
//this simply starts the player playing the media you gave it
mediaPlayer.playMedia(media, options);
// Don't exit
//basically you don't want the thread to end and kill the player,
//so it just hangs around and waits for it to end.
Thread.currentThread().join();
}
private static String formatHttpStream(String serverAddress, int serverPort) {
StringBuilder sb = new StringBuilder(60);
sb.append(":sout=#duplicate{dst=std{access=http,mux=ts,");
sb.append("dst=");
sb.append(serverAddress);
sb.append(':');
sb.append(serverPort);
sb.append("}}");
return sb.toString();
}
}
我尝试寻找解决方案,但我能找到的就是: Video Streaming in vlcj 虽然这个用户有相同的错误,但我无法从这个链接解决我的问题,虽然我确实使用了它的StreamHttp代码示例。我是一个相对缺乏经验的程序员,所以如果我错过了一个明显的解决方案,那么我道歉。我使用的是Java 1.8,Windows 7 64位。
答案 0 :(得分:1)
你需要这样的东西:
String media = "screen://";
String[] options = {
":sout=#transcode{vcodec=FLV1,vb=4096,scale=0.500000}:http{mux=ffmpeg{mux=flv},dst=:5000/"
};
这里显示的关键事项是一个“sout”字符串来转码视频,然后另一个附加的“sout”字符串来流(在这种情况下通过http)。
在此示例字符串中,对于http流,仅指定端口(5000,任意选择)。没有指定主机,因此它表示localhost。你可以拥有类似“dst = 127.0.0.1:8080 /”的东西或者你需要的东西。
您必须选择/试验所需的特定转码/流媒体选项。这些选项没有一种尺寸适合所有。
足注意:
您实际上可以使用VLC本身为您生成此字符串。
启动VLC,然后选择要播放的媒体。
而不是按“播放”,而是使用小部件来选择“流”。这将打开Streaming向导,您可以在其中选择所有选项。
在向导结束时,在开始播放之前,它会显示您需要的字符串。