0

I am struggling with mediaMetadataRetriever since it does not work at all, even I could play the video file on android studio.

    val dancerView = findViewById<VideoView>(R.id.dancerView)


    val path_vid = "android.resource://" + getPackageName() + "/" + R.raw.test_720
    var intent_cam = Intent(MediaStore.ACTION_VIDEO_CAPTURE)
    var mediaController = MediaController(this)
    mediaController.setAnchorView(dancerView)
    dancerView.setVideoPath(path_vid)
    dancerView.requestFocus()
    dancerView.start()

Here I could play the video named test_720.mp4. However,

    val vid_file = File(path_vid)
    val vid_file_uri = Uri.fromFile(vid_file)
    val vid_abspath =vid_file.absolutePath
    Log.e("Path", path_vid)
    Log.e("absPath", vid_abspath)
    val meta_ret = MediaMetadataRetriever()
    meta_ret.setDataSource(path_vid)
    val frame_num = meta_ret.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_FRAME_COUNT)?.toInt()
    val frame_rate = meta_ret.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CAPTURE_FRAMERATE)?.toInt()

No matter what I put in setDatasource instead of path_vid it only showed error :

Caused by: java.lang.IllegalArgumentException
    at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:77)
    at com.example.teamov.MainActivity.onCreate(MainActivity.kt:70)
    at android.app.Activity.performCreate(Activity.java:7802)
    at android.app.Activity.performCreate(Activity.java:7791)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7356) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

I cannot understant why this happen, since other examples I saw said that this should work. Am I doing something wrong? Or is the video file not sufficient ?

4

1 回答 1

0

尝试使用 android 资源 uri 本身访问您的文件:

val path_vid = "android.resource://" + getPackageName() + "/" + R.raw.test_720
val meta_ret = MediaMetadataRetriever()
val context = this
meta_ret.setDataSource(context, Uri.parse(path_vid))
于 2021-08-10T09:08:33.977 回答