2015-01-11 2 views
1

저는 C# 프로그램을 실행 중이므로 다른 프로세스에서 vlc에 대한 비디오를 재생하고 명령을 제공해야합니다. 나는 같은 것을 찾고 있지 않다. axVLCPlugin21C에서 제어 vlc 프로세스 #

기본 재생/일시 정지/볼륨 명령 만 필요하다. 가장 쉬운 방법은 무엇입니까?

나는이 시도하지만, 표준 입력 쓰는 당신은 작성중인 프로세스의 표준 입력을 리디렉션해야 Process p = Process.Start(@"C:\...\a.mp4"); p.StandardInput.Write("comand");

답변

1

리디렉션의 콘솔 표준이 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);   
} 
+0

팁 주셔서 감사합니다. 소켓은 놀라 울 정도로 쉽게 작동했습니다!. SendKeys는 개념의 단순한 증명을 위해 내가 찾고있는 종류의 Aproach입니다. 여전히 기대했던 것보다 훨씬 더 추악합니다. – Troveldom

0

에 실패했습니다. 자세한 내용은 this 페이지의 샘플을 참조하십시오.

+0

감사합니다! 그러나 stdin을 올바르게 설정하면 어떤 명령에도 응답하지 않습니다. S – Troveldom