Android VideoView 从本地服务器流式传输时无法播放此视频

Android VideoView Can't play this video when streaming from localserver

我想从我的本地 Spring 引导服务器播放 VideoView 中的视频,当我将 URL 传递到 VideoView 时它显示(不能播放这个视频)。

例如URL 传递给 VideoView 是 (http://172.20.10.3:8090/api/v1/video/downloadVideoURL?userName=Test&videoID=6655:)

Spring 启动代码:

    public ResponseEntity<Object> downloadFileURL(@RequestParam(name = "userName", required = true) String userName,
                                                  @RequestParam(name = "videoID", required = true) String videoID)
            throws FileNotFoundException, MalformedURLException {

        String filePath = videoService.findVideoPathByUserNameAndVideoId(userName, videoID);
        File file = new File(filePath);

        InputStreamResource inputStreamResource = new InputStreamResource(new FileInputStream(file));

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/mp4"))
                .body(inputStreamResource);

    }

客户端代码:


            txtLikes.setText(videoItem.getTxtLikes());
            txtComments.setText(videoItem.getTxtComments());
            videoView.setVideoURI(Uri.parse(videoItem.getVideoPath()));

            videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    videoProgressBar.setVisibility(View.GONE);
                    mediaPlayer.start();

                    float videoRate = mediaPlayer.getVideoWidth() / (float) mediaPlayer.getVideoHeight();
                    float screenRatio = videoView.getWidth() / (float) videoView.getHeight();
                    float scale = videoRate / screenRatio;

                    if (scale >= 1f) {
                        videoView.setScaleX(scale);
                    } else {
                        videoView.setScaleY(1f / scale);
                    }
                }
            });

            videoView.setOnCompletionListener(MediaPlayer::start);

            videoView.setOnTouchListener((view, motionEvent) -> {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    if (videoView.isPlaying()) {
                        videoView.pause();
                    }
                    else {
                        videoView.start();
                    }
                }
                return true;
            });
        } 

通过实施 Spring WebFlux.

解决了问题