2017-08-17 50 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

相关问题