Android MediaMetadataRetriever取视频封面,Kotlin(1)

Android MediaMetadataRetriever取视频封面,Kotlin(1)

XML 复制代码
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:padding="1px">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="180px"
        android:background="@android:color/darker_gray"
        android:scaleType="centerCrop" />

</LinearLayout>
Kotlin 复制代码
import android.content.ContentUris
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class MainActivity : AppCompatActivity() {
    companion object {
        const val TAG = "fly"

        const val SPAN_COUNT = 9
        const val VIDEO = 1
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val rv = findViewById<RecyclerView>(R.id.rv)

        val layoutManager = GridLayoutManager(this, SPAN_COUNT)
        layoutManager.orientation = GridLayoutManager.VERTICAL
        rv.layoutManager = layoutManager

        val adapter = MyAdapter(this)

        rv.adapter = adapter
        rv.layoutManager = layoutManager

        val ctx = this
        lifecycleScope.launch(Dispatchers.IO) {
            val videoList = readAllVideo(ctx)

            Log.d(TAG, "readAllVideo size=${videoList.size}")

            val lists = arrayListOf<MyData>()
            lists.addAll(videoList)

            lifecycleScope.launch(Dispatchers.Main) {
                adapter.dataChanged(lists)
            }
        }
    }

    private fun readAllVideo(ctx: Context): ArrayList<MyData> {
        val videos = ArrayList<MyData>()

        //读取视频Video
        val cursor = ctx.contentResolver.query(
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            null,
            null,
            null,
            null
        )

        while (cursor!!.moveToNext()) {
            //路径
            val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))

            val id = cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)
            val videoUri: Uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getLong(id))

            //名称
            //val name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME))

            //大小
            //val size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE))

            videos.add(MyData(videoUri, path, VIDEO))
        }

        cursor.close()

        return videos
    }
}
Kotlin 复制代码
import android.content.Context
import android.graphics.Bitmap
import android.media.MediaMetadataRetriever
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.collection.LruCache
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class MyAdapter : RecyclerView.Adapter<MyAdapter.VideoHolder> {
    private var mCtx: Context? = null
    private var mItems = ArrayList<MyData>()

    private var mFailItemCount = 0
    private var mSuccessItemCount = 0
    private var mTotalCostTime = 0L
    private val mCache = LruCache<String, Bitmap?>(1000)
    private var mIsDecoderCompleted = false

    constructor(ctx: Context) : super() {
        mCtx = ctx
    }

    fun dataChanged(items: ArrayList<MyData>) {
        this.mItems = items

        mSuccessItemCount = 0
        mTotalCostTime = 0
        mFailItemCount = 0

        notifyDataSetChanged()

        (mCtx as AppCompatActivity).lifecycleScope.launch(Dispatchers.IO) {
            mIsDecoderCompleted = false

            mItems.forEachIndexed { idx, data ->
                var bmp: Bitmap? = mCache[data.toString()]
                if (bmp == null) {
                    bmp = getSysMMRBmp(data)

                    if (bmp != null) {
                        mCache.put(data.toString(), bmp)
                    }

                    (mCtx as AppCompatActivity).lifecycleScope.launch(Dispatchers.Main) {
                        notifyItemChanged(idx)
                    }
                }
            }

            mIsDecoderCompleted = true
            withContext(Dispatchers.Main) {
                notifyDataSetChanged()
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoHolder {
        val v = LayoutInflater.from(mCtx).inflate(R.layout.image_layout, null, false)
        return VideoHolder(v)
    }

    override fun onBindViewHolder(holder: VideoHolder, position: Int) {
        loadVideoCover(mItems[position], holder.image)
    }

    override fun getItemCount(): Int {
        return mItems.size
    }

    class VideoHolder : RecyclerView.ViewHolder {
        var image: ImageView? = null

        constructor(itemView: View) : super(itemView) {
            image = itemView.findViewById<ImageView>(R.id.image)
            image?.setImageResource(android.R.drawable.ic_menu_gallery)
        }
    }

    private fun getSysMMRBmp(data: MyData): Bitmap? {
        val sysMMR = MediaMetadataRetriever()

        var bmp: Bitmap? = null
        val t = System.currentTimeMillis()
        try {
            sysMMR.setDataSource(data.path)
            bmp = sysMMR.frameAtTime

            mSuccessItemCount++

            Log.d(
                MainActivity.TAG,
                "android MMR:total success item count=$mSuccessItemCount"
            )
        } catch (e: Exception) {
            Log.e(MainActivity.TAG, "android MMR: total fail item count=${mFailItemCount++} , ${e.message}:$data")
        } finally {
            try {
                sysMMR.release()
                sysMMR.close()
            } catch (exc: Exception) {
                Log.e(MainActivity.TAG, "$exc")
            }
        }

        val costTime = System.currentTimeMillis() - t
        mTotalCostTime = mTotalCostTime + costTime
        Log.d(MainActivity.TAG, "android MMR:total cost time= $mTotalCostTime ms")

        return bmp
    }

    private fun loadVideoCover(data: MyData, image: ImageView?) {
        val bmp: Bitmap? = mCache[data.toString()]
        if (bmp != null) {
            image?.setImageBitmap(bmp)
        } else {
            if (mIsDecoderCompleted) {
                image?.setImageResource(android.R.drawable.stat_notify_error)
            }
        }
    }
}
Kotlin 复制代码
import android.net.Uri

open class MyData {
    var uri: Uri? = null
    var path: String? = null
    var lastModified = 0L
    var width = 0
    var height = 0

    var position = -1
    var type = -1  //-1未知。1,普通图。2,视频。

    constructor(uri: Uri?, path: String?, type: Int = -1) {
        this.uri = uri
        this.path = path
        this.type = type
    }

    override fun toString(): String {
        return "MyData(uri=$uri, path=$path, lastModified=$lastModified, width=$width, height=$height, position=$position, type=$type)"
    }

    override fun equals(other: Any?): Boolean {
        return (this.toString()) == other.toString()
    }
}
复制代码
MediaMetadataRetriever抽帧的耗时太长,对于异常或者超规格的视频,基本上10+秒。通常5、6秒。

500个超规格、残破视频,解码耗时和成功率情况:

android MMR: total fail item count=110

android MMR:total success item count=389

android MMR:total cost time= 198679 ms

3分钟+,500个视频才解码完,成功解码389个,解码失败110个。(0开始计数)

Android MediaMetadataRetriever获取视频宽高,Java_retriever.extractmetadata-CSDN博客文章浏览阅读1k次,点赞3次,收藏5次。文章浏览阅读914次。【Android设置头像,手机拍照或从本地相册选取图片作为头像】像微信、QQ、微博等社交类的APP,通常都有设置头像的功能,设置头像通常有两种方式:1,让用户通过选择本地相册之类的图片库中已有的图像,裁剪后作为头像。文章浏览阅读124次。【Android设置头像,手机拍照或从本地相册选取图片作为头像】像微信、QQ、微博等社交类的APP,通常都有设置头像的功能,设置头像通常有两种方式:1,让用户通过选择本地相册之类的图片库中已有的图像,裁剪后作为头像。_retriever.extractmetadatahttps://blog.csdn.net/zhangphil/article/details/139521977Android MediaMetadataRetriever setDataSource failed: status = 0xFFFFFFEA-CSDN博客文章浏览阅读1.6k次。文章讲述了在Android应用中使用Glide加载视频时,遇到MediaMetadataRetriever的setDataSource抛出异常的情况,原因可能是视频文件损坏或大小为0。作者介绍了如何自定义AppGlideModule来处理这种错误,例如通过添加模型筛选和容错机制来改进加载逻辑。https://blog.csdn.net/zhangphil/article/details/133890245

相关推荐
敢敢のwings3 小时前
GR00T 2:当机器人学会先想象未来,再决定怎么动
android·机器人
传奇开心果编程3 小时前
【Jetpack Compose娓娓道来】第55课:传感器与设备姿态——把物理世界的数据接进Compose
android·kotlin·移动开发·传感器·jetpack compose·ui框架·声明式ui
传奇开心果编程5 小时前
【Jetpack Compose娓娓道来】第52课:蓝牙与位置权限——把“附近”和“在哪”安全地接进Compose
android·kotlin·移动开发·蓝牙·jetpack compose·声明式ui·位置权限
alexhilton5 小时前
卡顿定位:从 UI 线程到 RenderThread
android·kotlin·android jetpack
FungLeo6 小时前
FengCMS· PHP+MySQL升级实录 · 后台参数统一治理:id[]数组、id=undefined 跳转与312组回调组合
android·mysql·php·工程实践·fengcms
北斗星_And6 小时前
七年后再看 Android 底层:机制没变,路变短了
android·ai编程
王二蛋与他的张大花7 小时前
反编译后只剩 2 个类:一次自研 DEX 壳加固的完整复盘
android·安全·逆向
StarsGreen13 小时前
Android 12 中的“域“与“权限“介绍
android
恋猫de小郭15 小时前
Android Keep Rules 解读,直接影响 Google Play 上架和曝光
android·前端·flutter
FlightYe15 小时前
音视频修炼之容器与协议(六):HTTP 协议
android·linux·网络·网络协议·算法·http·音视频