无法播放此视频 Xamarin.Android

Can't play this video Xamarin.Android

我需要在 VideoView 上播放来自 sdcard 的视频,并且总是得到 "Can't play this video message"。我正在物理设备上进行测试。文件在外部存储上,可以通过内置 phone 应用程序毫无问题地播放。

我之前阅读过一些主题并尝试更改文件格式和分辨率但没有效果。

第二次猜测是文件路径,因此尝试使用 Android.OS.Environment.ExternalStorageDirectory.AbsolutePath 在许多变体中多次更改它,创建文件变量并从 File.Path.

获取路径

得到我每次放置路径的 TextView,它总是正确的。

我不知道为什么它不起作用。

代码:

Java.IO.File file = new Java.IO.File (Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/myimage/", "video.mp4");

tView.Text = file.Path;

MediaController mediaController = new MediaController(context: this);
vView.SetMediaController(mediaController);
mediaController.SetAnchorView(vView);
vView.SetVideoPath(file.Path);
vView.Start();

File.Path 值:

"/storage/emulated/0/myimage/video.mp4"

编辑:我在资源放置文件中创建了 "raw" 文件夹,然后使用以下路径:

vView.SetVideoPath("android.resource://" + PackageName + "/" + Resource.Raw.video);

它可以工作,但是如果有人知道如何获取 sdCard 上文件夹的正确路径?

我终于明白了。问题是我的 phone 安装卡在路径 /storage/xxxx-xxxx/ 我的研究表明目录的名称来自卷序列号(其中 android 自动安装卡)并且 ExternalStorageDirectory 指的是其他位置每个 android 设备。有时是内部有时是 USB 大容量存储。

无论如何都没有找到任何方法 returns 按照以下方法创建的路径:

string GetCardMountPoint()
    {
        string[] listOfDirs = Directory.GetDirectories("/storage/");
        string path = null;

        foreach ( string dir in listOfDirs)
        {
            if(dir.Contains('-'))
            {
                path = dir;
            }
        }

        return path;

这不是一个复杂的解决方案,但现在可以使用。