2017-08-17 47 views
-1

我正在試圖在android工作室上製作一個android應用程序,該應用程序ssh進入遠程服務器並運行命令。它還必須從通過vlc建立HTTP流的遠程服務器轉發端口8080。然後這將用於在應用程序中顯示視頻的videoview小部件。此應用程序在logcat中運行時沒有錯誤,併成功運行ssh命令,但視頻視圖只顯示一個黑盒子 - 我懷疑我的代碼有問題。任何幫助將是偉大的!在Android應用程序中傳輸http視頻

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final VideoView videoView = (VideoView) findViewById(R.id.videoView); 
    new AsyncTask<Integer, Void, Void>() { 
     @Override 
     protected Void doInBackground(Integer... params) { 
      try { 
       executeRemoteCommand0("xxx", "xxx", "xxx", 22, videoView); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
    }.execute(1); 

    try { 
     sleep(5); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 


videoView.setVideoPath(String.format("http://localhost:%d/video.mp4", 5000)); 

    videoView.start(); 

} 


public String executeRemoteCommand0(String username, String password, String hostname, int port, VideoView videoView) throws Exception { 

    JSch jsch = new JSch(); 
    Session session = jsch.getSession(username, hostname, port); 
    session.setPassword(password); 

    // Avoid asking for key confirmation 
    Properties prop = new Properties(); 
    prop.put("StrictHostKeyChecking", "no"); 
    session.setConfig(prop); 

    int camport = session.setPortForwardingL(5000, hostname,8080); 
    session.connect(); 


    // SSH Channel 
    ChannelExec channelssh = (ChannelExec) session.openChannel("exec"); 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    channelssh.setOutputStream(baos); 

    // Execute command 

    channelssh.setCommand("command"); 
    channelssh.setPty(true); 
    channelssh.connect(); 

    return baos.toString(); 
} 
} 

回答

0

我認爲你的問題就出在這行: videoView.setVideoPath(String.format("http://localhost:%d/video.mp4", 5000));

你正試圖從移動設備本身引用localhost。您應該將其替換爲用於流式傳輸視頻的服務器的真實IP地址或域名。

此外,睡在Android上的主線程是一種糟糕的做法。儘量避免這種情況。

+0

將遠程服務器的端口8080轉發到本地服務器的5000端口後,我認爲可以在本地訪問它?另外,我試圖將視頻視圖放入異步任務中,但它引發了'需要聲明looper.prepare(),並且當我做所有變量不再在外部庫中的函數中解析時。我知道它的超級笨重:/ – Liz

+0

引用'localhost'意味着你有一個移動設備上部署的服務器實例(顯然不是這種情況)。您需要在主線程上設置視頻播放器路徑。你可以實現'AsyncTask'的'onPostExecute()'方法。它將在後臺完成後在主線程上調用。 –

+0

是否有需要先轉發端口呢?或者它的功能是一個URL?感謝關於onPostExecute()的提示:) – Liz

相關問題