3

I am trying to upload an mp4 file from sd card to remote server. The upload is getting sucessfully, but while i am trying to play that file by url using VideoView it is showing "can't play this video ". This issue is happening for only videos which is captured using phone, Suppose if i am uploading a video from watsapp folder everything works fine without any issues. Do i need to do any compression before uploading ? Here is the code which i am using for uploading video

 try {
                    FileInputStream fileInputStream = new FileInputStream(sourceFile);
                    URL url = new URL(my url for upload);
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setDoInput(true);
                    conn.setDoOutput(true);
                    conn.setUseCaches(false);
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                    conn.setRequestProperty("myFile", fileName);
                    video_name = fileName;
                    video_name = fileName.substring(fileName.lastIndexOf("/")+1);
                    dos = new DataOutputStream(conn.getOutputStream());

                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + fileName + "\"" + lineEnd);
                    dos.writeBytes(lineEnd);

                    bytesAvailable = fileInputStream.available();
                    Log.i("ava", "Initial .available : " + bytesAvailable);

                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    buffer = new byte[bufferSize];

                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                    while (bytesRead > 0) {
                        dos.write(buffer, 0, bufferSize);
                        bytesAvailable = fileInputStream.available();
                        bufferSize = Math.min(bytesAvailable, maxBufferSize);
                        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                    }

                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                    serverResponseCode = conn.getResponseCode();
                    Content = conn.getResponseMessage();

                    fileInputStream.close();
                    dos.flush();
                    dos.close();
                } catch (MalformedURLException ex) {
                    ex.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
Ramees
  • 68
  • 4
  • 16
  • have you verified that video uploads successfully into remote server ? – Heshan Sandeepa Oct 13 '16 at 09:57
  • @HeshanSandeepa yup it is able to play in browser as well. But feel like some issues are there, it is stucking sometimes. – Ramees Oct 13 '16 at 10:07
  • so whats the real issue here ? "can't play this video" or "stuck" ?? – Heshan Sandeepa Oct 13 '16 at 10:15
  • @HeshanSandeepa stuck is happening while playing the uploaded video in a web browser . If the try to load that url in VideoView in android app it will show can't play this video – Ramees Oct 13 '16 at 10:17
  • can you post the url please – Heshan Sandeepa Oct 13 '16 at 10:20
  • @HeshanSandeepa sorry yar.. url cannot be shared – Ramees Oct 13 '16 at 10:24
  • Since you are not able to share , ensure the relevant video format is supported by your device. – Heshan Sandeepa Oct 13 '16 at 10:26
  • @HeshanSandeepa it is .mp4 format , if i am playing the same video from sd card , it is working . Isssue is happening to the uploaded video only and as i mentioned in question if the video is uploaded from watsapp folder everything is working fine. My doubt is whether watsapp is having compressed video and do we need to do any compression before uploading. – Ramees Oct 13 '16 at 10:34

2 Answers2

0

Yes it may occurs because your URL contains white-spaces so you need to just remove space and set

%20

insted of white-space like :

URL url1 = new URL(your_url.trim().replace(" ", "%20"));

hope it works for you

Yash ajabiya
  • 134
  • 2
  • 8
0
try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(
                    PlayVideoActivity.this);
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            Uri video = Uri.parse(VideoURL);
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(video);

        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        videoview.requestFocus();
        videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                pgDialog.dismiss();
                videoview.start();
            }
        });
Yash ajabiya
  • 134
  • 2
  • 8