控制android中视频的播放速度

发布于 2024-10-10 19:05:37 字数 94 浏览 6 评论 0原文

我正在使用 VideoView 播放保存在 res/raw 中的视频文件。我找不到控制视频播放速度的方法。基本上我想在移动滚动条时减少和增加播放。有没有解决办法来实现这一点?

I am using a VideoView to play a video file kept in res/raw. I couldnt find a way to control the playback speed of the video. Basically i want to reduce and increase the playback while moving a scroll bar. Is there any work around for implementing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雨落星ぅ辰 2024-10-17 19:05:37

你可以使用它,但它适用于 api 23 及更高版本

 mVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {

            //works only from api 23
            PlaybackParams myPlayBackParams = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                myPlayBackParams = new PlaybackParams();
                myPlayBackParams.setSpeed(0.8f); //you can set speed here
                mp.setPlaybackParams(myPlayBackParams);
            }

        }
    });

you can use this but it works on api 23 and above

 mVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {

            //works only from api 23
            PlaybackParams myPlayBackParams = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                myPlayBackParams = new PlaybackParams();
                myPlayBackParams.setSpeed(0.8f); //you can set speed here
                mp.setPlaybackParams(myPlayBackParams);
            }

        }
    });
贪恋 2024-10-17 19:05:37

不,您不能仅使用 VideoView 来更改播放速率。 VideoViewMediaPlayer 仅提供有限的媒体功能。

您必须使用一些第三方库,例如PVPlayer,并自己实现。

这也是 Android 上优秀的媒体播放器如此有价值的原因:)

No, you cannot change the playback rate by simply using VideoView. VideoView and MediaPlayer only provide limited media functions.

You have to use some third party library, e.g., PVPlayer, and implement that yourself.

That's also why good media players on Android are so valuable:)

一身软味 2024-10-17 19:05:37

我想说的是 Mk Kamal 的解决方案有一个意想不到的副作用:调用 setPlaybackParamsOnPreparedListener 中的 > 将强制 VideoView 重复播放最新播放的视频。
我不知道这是一个错误还是一个功能,但我找到了一种方法来避免这种行为:

private float speed = 0.8f;
private final MediaPlayer.OnInfoListener listener = (mp, what, extra) -> {

    if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
        mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(speed));
        return true;
    }
    return false;
};


videoView.setOnPreparedListener(
                mp -> {
                    mp.setOnInfoListener(listener);
                }
        );

仅当播放器已启动时才会发送MEDIA_INFO_VIDEO_RENDERING_START

我想强调的是 getPlaybackParams 被注释为 @NonNull,因此不需要创建新的 PlaybackParams 对象。

I want to say than Mk Kamal's solution have an unexpected side effect: calling setPlaybackParams in OnPreparedListener will force VideoView to repeat the latest played video when the app was returned from the background.
I don't know is it a bug or a feature, but I found a way to avoid such behavior:

private float speed = 0.8f;
private final MediaPlayer.OnInfoListener listener = (mp, what, extra) -> {

    if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
        mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(speed));
        return true;
    }
    return false;
};


videoView.setOnPreparedListener(
                mp -> {
                    mp.setOnInfoListener(listener);
                }
        );

MEDIA_INFO_VIDEO_RENDERING_START will be sent only if the palyer was already started.

And I want to emphasize that getPlaybackParams is annotated as @NonNull, so it's not necessary to create a new PlaybackParams object.

成熟的代价 2024-10-17 19:05:37

Kotlin 变体,API 高于 23

val playerView = itemView.findViewById<VideoView>(R.id.videoview)
playerView.setVideoURI(Uri.parse("android.resource://" + context.packageName + "/" + R.raw.123.mp4))

playerView.setOnPreparedListener { mediaPlayer ->
   playerView.seekTo(1) // for video preview
   mediaPlayer.playbackParams = mediaPlayer.playbackParams.apply {
      speed = 0.6f
   }
   playerView.start()
}

Kotlin variant, API above 23

val playerView = itemView.findViewById<VideoView>(R.id.videoview)
playerView.setVideoURI(Uri.parse("android.resource://" + context.packageName + "/" + R.raw.123.mp4))

playerView.setOnPreparedListener { mediaPlayer ->
   playerView.seekTo(1) // for video preview
   mediaPlayer.playbackParams = mediaPlayer.playbackParams.apply {
      speed = 0.6f
   }
   playerView.start()
}
牵你的手,一向走下去 2024-10-17 19:05:37

DicePlayer 在我的 Asus Transformer 上完美运行。
它有一个速度控制屏幕显示。

我不确定 res/raw 是什么。

DicePlayer works perfectly on my Asus Transformer.
It has a speed control onscreen display.

I'm not sure what res/raw is though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文