Android Media3(五)— 实现视频截图功能

最近在某个技术交流群中,有人问怎么实现视频截图。本文简单介绍下如何使用Media3实现视频截图功能。

添加依赖

在app module下的build.gradle中添加代码,如下:

scss 复制代码
dependencies {
    implementation("androidx.media3:media3-ui:1.1.0")
    implementation("androidx.media3:media3-session:1.1.0")
    implementation("androidx.media3:media3-exoplayer:1.1.0")
}

实现视频截图功能

TextureView提供了获取视频截图的方法getBitmap(),可以获取Bitmap.Config.ARGB_8888像素格式的图片,图片的宽高与TextureView的宽高一致。

  • 设置PlayerViewsurface_type

在布局文件中将PlayerViewsurface_type设置为texture_view

ini 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    ......

    <androidx.media3.ui.AspectRatioFrameLayout
        android:id="@+id/arfl_player_container"
        android:layout_width="match_parent"
        android:layout_height="320dp"
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toBottomOf="@id/btn_screenshot">

        <androidx.media3.ui.PlayerView
            android:id="@+id/play_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:resize_mode="fill"
            app:show_buffering="always"
            app:surface_type="texture_view"
            app:use_controller="false" />

    </androidx.media3.ui.AspectRatioFrameLayout>

    ......
    
</androidx.constraintlayout.widget.ConstraintLayout>
  • 生成截图

通过PlayViewgetVideoSurfaceView()获取TextureView,并生成截图,代码如下:

kotlin 复制代码
class Media3ExampleActivity : AppCompatActivity() {

    private lateinit var binding: LayoutMedia3ExampleActivityBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = LayoutMedia3ExampleActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.includeTitle.tvTitle.text = "Media3 Example"
        binding.playView.player = ExoPlayer.Builder(this)
            .build()
        binding.playView.player?.run {
            addListener(object : Player.Listener {
                ......
            })
            repeatMode = Player.REPEAT_MODE_ALL
            playWhenReady = true
        }
        binding.btnPlaySingleVideo.setOnClickListener {
            binding.playView.player?.run {
                stop()
                setMediaItem(MediaItem.fromUri("https://minigame.vip/Uploads/images/2021/09/18/1631951892_page_img.mp4"))
                prepare()
            }
        }
        binding.btnScreenshot.setOnClickListener {
            // 获取TextureView并生成截图
            (binding.playView.videoSurfaceView as? TextureView)?.bitmap?.let {
                binding.ivScreenshotContainer.setImageBitmap(it)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        binding.playView.onResume()
    }

    override fun onPause() {
        super.onPause()
        binding.playView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.playView.player?.release()
        binding.playView.player = null
    }
}

效果如图:

示例

演示代码已在示例Demo中添加。

ExampleDemo github

ExampleDemo gitee

相关推荐
一笑的小酒馆5 小时前
Android性能优化之截屏时黑屏卡顿问题
android
懒人村杂货铺8 小时前
Android BLE 扫描完整实战
android
TeleostNaCl10 小时前
如何安装 Google 通用的驱动以便使用 ADB 和 Fastboot 调试(Bootloader)设备
android·经验分享·adb·android studio·android-studio·android runtime
fatiaozhang952711 小时前
中国移动浪潮云电脑CD1000-系统全分区备份包-可瑞芯微工具刷机-可救砖
android·网络·电脑·电视盒子·刷机固件·机顶盒刷机
2501_9159184112 小时前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
lichong95112 小时前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之dist打包发布在Android工程asserts里
android·vue.js·iphone
Android出海12 小时前
Android 15重磅升级:16KB内存页机制详解与适配指南
android·人工智能·新媒体运营·产品运营·内容运营
一只修仙的猿12 小时前
毕业三年后,我离职了
android·面试
编程乐学12 小时前
安卓非原创--基于Android Studio 实现的新闻App
android·ide·android studio·移动端开发·安卓大作业·新闻app
雅雅姐13 小时前
Android14 init.rc中on boot阶段操作4
android