简体   繁体   中英

How to Play Local Video file (.mp4) in Xamarin Plateform (Windos, Android and IOS)

I am facing the problem while playing the local Video file (TestMediaPlayer.Video.Demo_Video.mp4) in Xamarin Platform.

I have created the Cross Platform Test application in Xamarin Platform. I would like to play local mp4 file from device Windows or Android. When I play the sample video from the web URL that work fine. But When I Play at local file system crash or not play that file. I have already set the property BuildAction = Embedded Resources for that Video file.

How to Play Local video (mp4) file in Xamarin platform

My Sample code as below :

MainPage.xml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TestMediaPlayer"   
             xmlns:forms="clr-namespace:Plugin.MediaManager.Forms;assembly=Plugin.MediaManager.Forms"  
             x:Class="TestMediaPlayer.MainPage">

    <StackLayout> 
        <forms:VideoView x:Name="VideoPlayer" HeightRequest="404"  WidthRequest="404"  />         
    </StackLayout>

</ContentPage>

Code Behind File MainPage.cs:

using Plugin.MediaManager;
using Plugin.MediaManager.Abstractions;
using Plugin.MediaManager.Abstractions.Enums;
using Plugin.MediaManager.Abstractions.Implementations;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace TestMediaPlayer
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            testVideo();

            // CrossMediaManager.Current.Play("https://archive.org/download/BigBuckBunny_328/BigBuckBunny_512kb.mp4", MediaFileType.Video); // Online resources

        }

        private   async void testVideo()
        { 
            string fileFullName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalizedResources), "TestMediaPlayer/Video/Demo_Video.mp4");

            await CrossMediaManager.Current.Play(new MediaFile("file://" + fileFullName, MediaFileType.Video, ResourceAvailability.Local));
        }
     }
}

Resource File Print Screen 在此处输入图片说明

MediaManager provides native playback of media files and thus Assembly-based embedded resources are not directly supported.

So when using a file:// based URL, you would need to provide a URL to the local OS's filesystem.

So, if you have to use an embedded resource (which I personally would not recommend), you would need to copy it from your assembly to a physical file. There are numerous SOs question/answers that show how that it done, here is just one:

Otherwise you would need to look at using iOS bundle-based resources or Android Asset/Raw-based resources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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