Search This Blog

Saturday 22 August 2020

Xamarin From Play Video In iOS Default Media Player From Local Storage.

Here I am using Plugin.MediaManager Plugin to Play video from local storage in iOS Default Media player.

PlayVideoInIos (Xamarin Form Project)
MainPage.xaml
<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="PlayVideoInIOS.MainPage">
 
    <StackLayout>
        <!-- Place new controls here -->
        <Button  Text="Play Video From Local Storage"  Clicked="Button_Clicked"/>
    </StackLayout>
</ContentPage>

MainPage.xaml.cs
using PlayVideoInIOS.Interface;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
 
namespace PlayVideoInIOS
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
        private void Button_Clicked(object sender, EventArgs e)
        {
            if (Device.RuntimePlatform == Device.iOS)
            {
                Xamarin.Forms.DependencyService.Get<IPlayVideoFromLocalStorage>().PlayVideoFromLocalStorage("set here local video absolute path");
            }
        }
    }
}

PlayVideoInIos.IOS (Xamarin IOS Project)
PlayVideoFromLocalStorageService.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using AVKit;
using Foundation;
using MediaManager;
using MediaManager.Library;
using MediaManager.Platforms.Ios.Video;
using PlayVideoInIOS.Interface;
using PlayVideoInIOS.iOS.Services;
using UIKit;
using Xamarin.Forms;
 
[assembly:Dependency(typeof(PlayVideoFromLocalStorageService))]
namespace PlayVideoInIOS.iOS.Services
{
    public class PlayVideoFromLocalStorageService : IPlayVideoFromLocalStorage
    {
        private NSObject observer;
        AVPlayerViewController avpvc;
        UIViewController viewController;
 
        public void PlayVideoFromLocalStorage(string localFilePath)
        {
            try
            {
                viewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
                while (viewController.PresentedViewController != null)
                {
                    viewController = viewController.PresentedViewController;
                }
                IMediaItem item = new MediaItem();
                item.FileName = Path.GetFileName(localFilePath);
                item.MediaUri = "file://" + localFilePath;
                item.MediaLocation = MediaLocation.FileSystem;
                item.MediaType = MediaType.Video;
 
                VideoView videoView = new VideoView();
                videoView.PlayerViewController = new AVPlayerViewController();
 
                CrossMediaManager.Current.MediaPlayer.VideoView = videoView;
                CrossMediaManager.Current.MediaPlayer.ShowPlaybackControls = true;
                Device.BeginInvokeOnMainThread(() =>
                {
                    CrossMediaManager.Current.Play(item);
                });
                viewController.PresentViewController(videoView.PlayerViewController, true, null);
            }
            catch (Exception ex)
            {
            }
        }
    }
}



Popular Posts