安卓视频上传问题

3

我正在尝试从SD卡上传MP4文件到远程服务器。上传成功,但是当我尝试通过URL使用VideoView播放该文件时,它显示“无法播放此视频”。这个问题只发生在使用手机拍摄的视频上,如果我从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();
                }

你已经验证视频上传到远程服务器成功了吗? - Heshan Sandeepa
@HeshanSandeepa 是的,它也可以在浏览器中播放。但是感觉有些问题存在,有时会卡住。 - Ramees
那么真正的问题是什么?是“无法播放此视频”还是“卡住了”? - Heshan Sandeepa
@HeshanSandeepa 在使用网络浏览器播放上传的视频时遇到了卡顿问题。如果在 Android 应用程序中尝试使用 VideoView 加载该 URL,则会显示无法播放此视频。 - Ramees
请问你能否发布URL链接? - Heshan Sandeepa
显示剩余3条评论
2个回答

0

是的,这可能会发生,因为您的URL包含空格,所以您只需要删除空格并设置

%20

代替空格,例如:

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

希望这对您有用。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接