我正在运行一个c#程序,我需要在另一个进程上在vlc上播放视频并用命令提供它。
我不是在找axVLCPlugin21
我只需要基本的播放/暂停/音量命令。 实现这一目标的最简单方法是什么?
我试过这个,但是stdin写错了
Process p = Process.Start(@"C:\...\a.mp4");
p.StandardInput.Write("comand");
答案 0 :(得分:2)
我还发现控制台Std In重定向不适用于VLC进程。我可以让VLC控制台界面工作的唯一方法是使用SendKeys,这不是一个非常好的方法。
VLC也支持相同接口的套接字连接,这看起来效果很好。以下是如何连接和发送/接收命令和响应的示例。
static void Main()
{
IPEndPoint socketAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 54165);
var vlcServerProcess = Process.Start(@"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe", "-I rc --rc-host " + socketAddress.ToString());
try
{
Socket vlcRcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
vlcRcSocket.Connect(socketAddress);
// start another thread to look for responses and display them
Task listener = Task.Factory.StartNew(() => Receive(vlcRcSocket));
Console.WriteLine("Connected. Enter VLC commands.");
while(true)
{
string command = Console.ReadLine();
if (command.Equals("quit")) break;
Send(vlcRcSocket, command);
}
Send(vlcRcSocket, "quit"); // close vlc rc interface and disconnect
vlcRcSocket.Disconnect(false);
}
finally
{
vlcServerProcess.Kill();
}
}
private static void Send(Socket socket, string command)
{
// send command to vlc socket, note \n is important
byte[] commandData = UTF8Encoding.UTF8.GetBytes(String.Format("{0}\n", command));
int sent = socket.Send(commandData);
}
private static void Receive(Socket socket)
{
do
{
if (socket.Connected == false)
break;
// check if there is any data
bool haveData = socket.Poll(1000000, SelectMode.SelectRead);
if (haveData == false) continue;
byte[] buffer = new byte[socket.ReceiveBufferSize];
using (MemoryStream mem = new MemoryStream())
{
while (haveData)
{
int received = socket.Receive(buffer);
mem.Write(buffer, 0, received);
haveData = socket.Poll(1000000, SelectMode.SelectRead);
}
Console.WriteLine(Encoding.UTF8.GetString(mem.ToArray()));
}
} while (true);
}
答案 1 :(得分:0)
您必须重定向正在创建的流程的标准输入。有关详细信息,请参阅this页面上的示例。