6

I want to download and play video files during downloading. Since VideoView is not helping with this matter I decided to work with nanoHTTPd to create a pseudo HTTP server and inside my own server try to download video file and play it afterward but my problem is :

1-How can I flush downloaded part to videoview and download the remaining parts?

Following is my source :

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);
        }
    }
Vahid Hashemi
  • 5,182
  • 10
  • 58
  • 88
  • look here , is a answer in the middle of page : http://stackoverflow.com/questions/9987042/videoview-onresume-loses-buffered-portion-of-the-video – CristiC777 Sep 15 '14 at 10:09

1 Answers1

-1

Found a way, you can read more about it here : http://www.vahidhashemi.com/?p=120

Vahid Hashemi
  • 5,182
  • 10
  • 58
  • 88
  • 4
    I don't like that it's a link-only answer, but it is helpful (and not dead yet). It would be better if you could explain the general idea, so it stays here when the link dies (it *will* die eventually). Then I would upvote it. – Matthieu Sep 06 '16 at 17:34