首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当从本地服务器流时,Android VideoView无法播放此视频

当从本地服务器流时,Android VideoView无法播放此视频
EN

Stack Overflow用户
提问于 2022-02-26 19:27:14
回答 1查看 153关注 0票数 0

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

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

弹簧启动代码:

代码语言:javascript
运行
复制
    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);

    }

客户端代码:

代码语言:javascript
运行
复制
            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;
            });
        } 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-27 15:06:30

通过实现Spring WebFlux来解决问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71279677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档