2010-02-18 5 views
5

Ho già visto questo thread, ma sto ancora avendo un problema: starting vlc player in java Sembra che i binding Java per VLC non siano più in sviluppo attivo e non supportano tutto possibile sulla riga di comando comunque.Avviare VLC in Java e connettersi ad esso tramite interfaccia rc

Dato il codice seguente, non è possibile avviare VLC da un'applicazione Java su Mac OS 10.5.8 (Java 1.6) e quindi connettersi ad esso tramite l'interfaccia rc tramite il Terminale o un'altra applicazione Java.

public class Main { 

public static void main(String[] args) { 
    String s = null; 


    try { 
     //Process p = Runtime.getRuntime().exec("/Applications/VLC.app/Contents/MacOS/VLC -I telnet --telnet-host=localhost:4442 -I rc --rc-host=localhost:4444"); 
     //Process p = Runtime.getRuntime().exec("/Applications/VLC.app/Contents/MacOS/VLC -I rc --rc-host=localhost:4444"); 

     //ProcessBuilder pb = new ProcessBuilder("/Applications/VLC.app/Contents/MacOS/VLC","-I rc","--rc-host=localhost:4444"); 
     ProcessBuilder pb = new ProcessBuilder("/Applications/VLC.app/Contents/MacOS/VLC","-IRC","--rc-host=localhost:4444"); 
     Process p = pb.start(); 

     StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), false); 
     StreamGobbler inputGobbler = new StreamGobbler(p.getInputStream(), false); 
     errorGobbler.start(); 
     inputGobbler.start(); 

     System.out.println("Waiting: \n"+p.waitFor());  
     System.out.println("All done here"); 
     //p.destroy(); 
     //System.exit(0); 

    } catch (IOException ioe) { 
    ioe.printStackTrace(); 
    } catch (Exception ie) { 
    ie.printStackTrace(); 
    } 
} 
} 

class StreamGobbler extends Thread { 
InputStream is; 
boolean discard; 
StreamGobbler(InputStream is, boolean discard) { 
    this.is = is; 
    this.discard = discard; 
} 
public void run() { 
try { 
    InputStreamReader isr = new InputStreamReader(is); 
    BufferedReader br = new BufferedReader(isr); 
    String line=null; 
    while ((line = br.readLine()) != null) 
    if(!discard) 
     System.out.println(line);  
    } 
catch (IOException ioe) { 
    ioe.printStackTrace(); 
} 

}}

Qui è l'applicazione Java utilizzando il pacchetto di rete Apache Commons che sto cercando di connettersi al app sopra in esecuzione sulla stessa macchina:

public class TelnetTest { 
public static void main(String args[]) { 


    TelnetClient tl = new TelnetClient(); 
    try { 
     tl.connect("localhost", 4444); 
     if(tl.isConnected()) { 
      System.out.println("Connected successfully!"); 

      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(tl.getOutputStream())); 
      bw.write("quit"); 
      bw.flush(); 

     } else { 
      System.err.println("Problem with connection"); 
     } 
    } catch(Exception e) { 
     System.err.println("Telnet connection threw an exception: "+e.getMessage()); 
    } 
} 
} 

Il Quest'ultima app funziona correttamente se avvio VLC utilizzando i comandi della prima app del Terminale. Allo stesso modo, non riesco a collegarmi alla prima app dal Terminale usando "telnet localhost 4444" nel Terminale.

L'unica differenza che posso trovare è nell'output da VLC. Quando si esegue nel terminale:

[0x2786e8] main interface error: no interface module matched "globalhotkeys,none" 
[0x2786e8] main interface error: no suitable interface module 
[0x201b28] main libvlc error: interface "globalhotkeys,none" initialization failed 
Remote control interface initialized. Type `help' for help. 

Quando si esegue tramite la parte superiore dell'applicazione Java:

[0x4009178] main interface error: no interface module matched "globalhotkeys,none" 
[0x4009178] main interface error: no suitable interface module 
[0x2017a8] main libvlc error: interface "globalhotkeys,none" initialization failed 
[0x4009178] main interface error: no suitable interface module 
[0x2017a8] main libvlc error: interface "default" initialization failed 

qualcuno mi può aiutare qui? Sono in perdita. Grazie mille.

risposta

5

È possibile eseguire VLC come sottoprocesso e alimentarlo tramite il flusso di output del processo. Devi lavare il flusso e dormire per un po 'di tempo dopo ogni comando. Il seguente codice non fa tutto, ma mi permette di riprodurre file diversi in VLC sotto il controllo di Java.

 String vlcParameters = String.format(
      "-I rc --rc-fake-tty --video-on-top --disable-screensaver --no-video-title-show " + 
      "--no-mouse-events --no-keyboard-events --no-fullscreen --no-video-deco " + 
      "--x11-display \"%s\" --video-x %d --video-y %d --width %d --height %d", 
      ":0.0", // X11 display 
      top,  // X 
      left,  //Y 
      width, //Width 
      height  //Height 
      ); 

    ProcessBuilder pb = new ProcessBuilder("vlc", vlcParameters); 

    pb.redirectErrorStream(true); 

    vlcProcess = pb.start(); 

// Later - clear current playlist 

     writer.write("clear\n".getBytes()); 
     writer.flush(); 
     Thread.sleep(10); 

     String playListCommand = String.format(
       "add file://%s\n", 
       filePath); 

     writer.write(playListCommand.getBytes()); 
     writer.flush(); 

     Thread.sleep(milliDuration - 10); 

Nota - è necessario un altro thread per leggere l'output di VLC in modo da non bloccare:

 Thread inputThread = new Thread(new Runnable() 
     { 

     @Override 
     public void run() 
      { 
      InputStream in = vlcProcess.getInputStream(); 

      BufferedReader bufin = new BufferedReader(new InputStreamReader(in)); 

      try 
       { 
       while (true) 
       { 
       String line = bufin.readLine(); 

       if (line == null) 
        { 
        System.out.writeln("End of data from VLC"); 
        } 

       System.out.writeln("VLC OUTPUT:" + line); 
       } 
       } 
      catch (IOException ex) 
       { 
       //... 
       } 
      } 
     }, 
     "VLC stdout reader"); 

    inputThread.start(); 
+0

Per chi vuole implementare ciò, notare che il "\ n" alla fine di ogni comando. Tenere anche presente che la chiusura del flusso di output causerà l'arresto dell'istanza di VLC. Queste erano due cose che mi hanno catturato. –

0

Dal VLC apre una nuova finestra DOS in modalità RC, durante writer.flush () il codice si lamenta che la pipa è stata chiusa. questo è stato verificato anche come inputThread stampa "VLC OUTPUT: nullEnd of data from VLC". C'è un modo per evitarlo, per collegarsi alla finestra vlc rc appena aperta?

saluti

Shahid

Problemi correlati