【问题标题】:Video Upload issue in androidandroid中的视频上传问题
【发布时间】:2017-02-22 07:53:57
【问题描述】:

我正在尝试将 mp4 文件从 sd 卡上传到远程服务器。上传成功,但是当我尝试使用 VideoView 通过 url 播放该文件时,它显示“无法播放此视频”。仅使用手机捕获的视频才会出现此问题,假设如果我从 watsapp 文件夹上传视频,一切正常,没有任何问题。在上传之前我需要做任何压缩吗? 这是我用于上传视频的代码

 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();
                }

【问题讨论】:

  • 您是否验证视频已成功上传到远程服务器?
  • @HeshanSandeepa 是的,它也可以在浏览器中播放。但感觉有些问题存在,有时会卡住。
  • 那么这里真正的问题是什么? “无法播放此视频”或“卡住”??
  • @HeshanSandeepa 在网络浏览器中播放上传的视频时发生卡住。如果尝试在 Android 应用的 VideoView 中加载该 url,它将显示无法播放此视频
  • 可以发一下网址吗

标签: android video upload video-streaming


【解决方案1】:

是的,它可能会发生,因为您的 URL 包含空格,因此您只需删除空格并设置

%20

插入空格,如:

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

希望对你有用

【讨论】:

    【解决方案2】:
    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();
                }
            });
    

    【讨论】:

      猜你喜欢
      • 2012-03-28
      • 2014-03-11
      • 1970-01-01
      • 2022-08-17
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多