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

问题描述 投票:0回答:1

我想从本地 Spring Boot 服务器在 VideoView 中播放视频,当我将 URL 传递到 VideoView 时,它会显示(无法播放此视频)。

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

Spring Boot 代码:

    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;
            });
        } 
android spring spring-boot android-videoview
1个回答
0
投票

通过实施Spring WebFlux解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.