如何通过nanoHTTPd将视频从互联网流式传输到VideoView

时间:2014-09-05 21:41:22

标签: android video video-streaming download nanohttpd

我想在下载过程中下载和播放视频文件。由于VideoView没有帮助解决这个问题,我决定使用nanoHTTPd创建一个伪HTTP服务器,并在我自己的服务器内尝试下载视频文件并在之后播放但我的问题是:

1 - 如何将下载的部分刷新到视频视图并下载其余部分?

以下是我的来源:

public class VideoStreamingServer extends NanoHTTPD {

        public VideoStreamingServer() {
            // by default listening on port 8080
            super(8080);
        }

        @Override
        public Response serve(String URI, Method method,
                              Map header, Map parameters, Map files) {

            FileInputStream fis = null;
            try {
//                fis = new FileInputStream("/mnt/sdcard/p/1.mp4");

                File bufferFile = File.createTempFile("test", "mp4");

                BufferedOutputStream bufferOS = new BufferedOutputStream(
                        new FileOutputStream(bufferFile));

                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet("http://www.example.net/dl/1.mp4");
                HttpResponse response = client.execute(request);
                Header[] headers = response.getAllHeaders();
                Log.e("Internet buffer", "connected to server");

                BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent(), 2048);


                byte[] buffer = new byte[16384];
                int numRead;
                boolean started = false;
                while ((numRead = bis.read(buffer)) != -1) {

                    bufferOS.write(buffer, 0, numRead);
                    bufferOS.flush();
                    totalRead += numRead;
                    if (totalRead > 120000 && !started) {
                             //problem starts here
                             //How can I flush the buffer to VideoView?


                    }

                }


            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            return new NanoHTTPD.Response(Response.Status.OK, "video/mp4", fis);
        }
    }

1 个答案:

答案 0 :(得分:-1)

找到方法,你可以在这里阅读更多相关信息:http://www.vahidhashemi.com/?p=120