1

I'm trying to play a video with media player while is downloading on sdcard. I give setdatasource from sdcard.. Media player play the video but after a few seconds stops the video, but media player still playing but content is on break. how can I play the video again where it left off?

    private void downloadVideo(String url) {
    try {
        URL url1 = new URL(url);
        long startTime = System.currentTimeMillis();
        Log.i("TAG", "image download beginning: " + url);

        URLConnection conn = url1.openConnection();

        conn.setReadTimeout(5000);
        conn.setConnectTimeout(30000);

        File path = android.os.Environment.getExternalStorageDirectory();
        InputStream is = conn.getInputStream();
        BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
        File directories = new File(path + "/sequence/");
        if (!directories.exists()) {
            directories.mkdirs();
        }
        FileOutputStream outStream = new FileOutputStream(new File(
                directories, "test2.mp4"));

        byte[] buff = new byte[5 * 1024];
        // byte[] buff = new byte[1024 * 24];

        int len;
        while ((len = inStream.read(buff)) != -1) {
            outStream.write(buff, 0, len);

        }

        outStream.flush();
        outStream.close();
        inStream.close();

        Log.i("TAG",
                "download completed in "
                        + ((System.currentTimeMillis() - startTime) / 1000)
                        + " sec");

    } catch (MalformedURLException e) {
        Log.d("Malformed", e.toString());
    } catch (IOException e) {
        Log.d("IOException", e.toString());
    }
}

code for playing video

        Runnable task = new Runnable() {
        public void run() {
            try {
                File path = android.os.Environment
                        .getExternalStorageDirectory();
                // http://www.pocketjourney.com/downloads/pj/video/famous.3gp
                // http://www.youtube.com/watch?v=Fvh38W4yKvc
                // http://mconnect123456.s3.amazonaws.com/4532happyfit2.mp4
                mp.reset();
                mp.setDataSource(path + "/sequence/test2.mp4");
                // mp.setDataSource("http://mconnect123456.s3.amazonaws.com/4532happyfit2.mp4");
                mp.prepare();

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mp.start();

            while (!mp.isPlaying()) {
                Log.d("Playing", "playing");

            }

        }
    };
    worker.schedule(task, 5, TimeUnit.SECONDS);
ovluca
  • 291
  • 1
  • 15

1 Answers1

1

I don't believe this is the best way to accomplish what you are trying to do. The MediaPlayer thinks it's playing a local file and doesn't take any time to buffer data properly as with network playback.

I would suggest creating a local proxy server on the device. You can then pass the MediaPlayer a localhost URI in setDataSource to your proxy and cache the data while it is being streamed. See my answer here for a little more info, and feel free to ask questions if you have trouble implementing it.

Community
  • 1
  • 1
Dave
  • 4,282
  • 2
  • 19
  • 24
  • I haven´t done this before. I need some more information. I will come back with questions.. thanks . – ovluca Nov 28 '13 at 09:53