Android Studio :MediaMetadataRetriever()无法运作

umuewwlo  于 2022-11-27  发布在  Android
关注(0)|答案(2)|浏览(382)

我正在与MediaMetadataRetriever斗争,因为它根本不工作,甚至我可以在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()

在这里我可以播放名为test_720.mp4的视频,

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()

无论我在setDatasource而不是path_vid中输入什么,它都只显示错误:

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) 

我不明白为什么会发生这种情况,因为我看到的其他例子说这应该可以工作。我做错了什么吗?或者视频文件不够?

35g0bw71

35g0bw711#

尝试使用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))
tyky79it

tyky79it2#

发现存在一种新的方法:setDataSource(链接、标题);我用这个解决了这个问题:

if (Build.VERSION.SDK_INT >= 29)
 mmr.setDataSource(link, new HashMap<String, String>());
else
 mmr.setDataSource(link);

相关问题