0

I am creating an android application which is running a mediaplayer on a textureview, and streaming video from the internet. Now, I want to record the same streaming video to a .mp4 file(or in any format) to SD card. how can I do it?

I cannot use a surfaceview instead of textureview. please help me.

Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
Vanilla Boy
  • 258
  • 2
  • 12
  • SurfaceView and TextureView are where the output goes. Capturing video *from* those is taking the long way around. You want to grab the video stream as it's arriving. – fadden Oct 28 '15 at 17:59
  • @fadden How can i do that?Could you please explain? – Vanilla Boy Oct 29 '15 at 09:09

1 Answers1

0

I got a solution. If the server supports downloading use the following code.

                private final int TIMEOUT_CONNECTION = 5000; //5sec
                private final int TIMEOUT_SOCKET = 30000; //30sec
                private final int BUFFER_SIZE = 1024 * 5; // 5MB


                try {
                    URL url = new URL("http://....");

                    //Open a connection to that URL.
                    URLConnection ucon = url.openConnection();
                    ucon.setReadTimeout(TIMEOUT_CONNECTION);
                    ucon.setConnectTimeout(TIMEOUT_SOCKET);

                    // Define InputStreams to read from the URLConnection.
                    // uses 5KB download buffer
                    InputStream is = ucon.getInputStream();
                    BufferedInputStream in = new BufferedInputStream(is, BUFFER_SIZE);
                    FileOutputStream out = new FileOutputStream(file);
                    byte[] buff = new byte[BUFFER_SIZE];

                    int len = 0;
                    while ((len = in.read(buff)) != -1)
                    {
                        out.write(buff,0,len);
                    }
                } catch (IOException ioe) {
                    // Handle the error
                } finally {
                    if(in != null) {
                        try {
                            in.close();
                        } catch (Exception e) {
                            // Nothing you can do
                        }
                    }
                    if(out != null) {
                        try {
                            out.flush();
                            out.close();
                        } catch (Exception e) {
                            // Nothing you can do
                        }
                    }
                }

it will help.thanks

Vanilla Boy
  • 258
  • 2
  • 12