简体   繁体   English

Xamarin.Forms MediaManager 从本地存储播放视频

[英]Xamarin.Forms MediaManager play video from local storage

I am creating custom cross-platform video player with MediaManager.我正在使用 MediaManager 创建自定义跨平台视频播放器。 It plays video from https but I want it play video from local storage.它从 https 播放视频,但我希望它从本地存储播放视频。

        var folder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        folder = Path.Combine(folder, "UserVideos/testvideo.mp4");
        videoPlayer.Source = folder;
        CrossMediaManager.Current.Play("file://" + "/UserVideos/testvideo.mp4");

I tried this way, but it doesn't play video.我试过这种方式,但它不播放视频。 Folder UserVideos is in main namespace.文件夹 UserVideos 位于主命名空间中。

Would you confirm whether the file testvideo.mp4 is stored in internal storage or external storage?请确认文件testvideo.mp4是存储在内部存储还是外部存储中? From your code, I guess that you want to get video from internal storage, but files in internal storage that cannot be shared, and you don't see it.从你的代码中,我猜你想从内部存储中获取视频,但是内部存储中的文件无法共享,你看不到它。

https://docs.microsoft.com/en-us/xamarin/android/platform/files/#internal-vs-external-storage https://docs.microsoft.com/en-us/xamarin/android/platform/files/#internal-vs-external-storage

So when using a file:// based URL, I suggest you can copy your video in Assets folder and BuildAction is AndroidAsset .因此,当使用基于 file:// 的 URL 时,我建议您可以将视频复制到Assets 文件夹中,并且BuildActionAndroidAsset

Then play video like this:然后像这样播放视频:

CrossMediaManager.Current.Play("file:///android_asset/minions.mp4");

I install Plugin.MediaManager.Forms by Nuget package, then init in Mainactivity.cs:我通过 Nuget 包安装Plugin.MediaManager.Forms ,然后在 Mainactivity.cs 中初始化:

CrossMediaManager.Current.Init(this);

MainPage.xaml:主页.xaml:

 <StackLayout>
        <video:VideoView HeightRequest="300" WidthRequest="200" />

        <Button Text="play local video" Clicked="BtnPlayLocal_Clicked"/>
    </StackLayout>



 private void BtnPlayLocal_Clicked(object sender, EventArgs e)
    {           
        CrossMediaManager.Current.Play("file:///android_asset/minions.mp4");
    }

在此处输入图片说明

Update更新

If you want to play video in Xamarin.ios, please add video in Resource folder and BuildAction is BundleResource如果要在 Xamarin.ios 中播放视频,请在Resource 文件夹中添加视频, BuildActionBundleResource

Then Play video like this:然后像这样播放视频:

CrossMediaManager.Current.PlayFromResource("minions.mp4");

Full code:完整代码:

private  void BtnPlayLocal_Clicked(object sender, EventArgs e)
    {
        if (Device.RuntimePlatform == Device.iOS)
        {
            CrossMediaManager.Current.PlayFromResource("minions.mp4");
        }
        else if (Device.RuntimePlatform == Device.Android)
        {
            CrossMediaManager.Current.Play("file:///android_asset/minions.mp4");
        }

    }

Here is the sample in Github:这是 Github 中的示例:

https://github.com/CherryBu/VideoSample https://github.com/CherryBu/VideoSample

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM