Hello I am using AVPlayer with AvController for the playing Video in iOS. Video Playing Successfully but once it finish playing my App is getting Crash.
using AVFoundation;
using AVKit;
using TestMobile.iOS.PlatformSpecifics;
using UIKit;
using Xamarin.Forms;
[assembly: Dependency(typeof(LocalFileProvider))]
namespace TestMobile.iOS.PlatformSpecifics
{
public class LocalFileProvider : UIViewController,ILocalFileProvider
{
private NSObject observer;
AVPlayer player;
AVPlayerViewController avpvc;
UIViewController viewController ;
public void PlayVideoFromLocalStorage(string filePath)
{
try
{
observer = NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification, OnFinishedPlayingFromUrl);
viewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
while (viewController.PresentedViewController != null)
{
viewController = viewController.PresentedViewController;
}
var url = new NSUrl(filePath, false);
if (player == null)
player = new AVPlayer(url);
avpvc = new AVPlayerViewController();
avpvc.Player = player;
player.ActionAtItemEnd = AVPlayerActionAtItemEnd.Pause;
player.Play();
viewController.PresentViewController(avpvc, true, null);
}
catch (Exception ex)
{
}
}
private void OnFinishedPlayingFromUrl(NSNotification obj)
{
if (player != null)
{
player.Pause();
player.ReplaceCurrentItemWithPlayerItem(null);
viewController.RemoveFromParentViewController();
viewController.DismissViewController(false, null);
}
NSNotificationCenter.DefaultCenter.RemoveObserver(observer);
}
}
}
I am using Dependency Service to Invoke PlayVideoFromLocalStorage Method.
Xamarin.Forms.DependencyService.Get<ILocalFileProvider>().PlayVideoFromLocalStorage(filePath);
Once the video is finish playing i was getting error like
{System.NullReferenceException: Object reference not set to an instance of an object
at MediaManager.Platforms.Apple.Player.AppleMediaPlayer.SeekTo (System.TimeSpan position) [0x00080] in C:\Users\mhvdi\Documents\OpenSource\XamarinMediaManager\MediaManager\Platforms\Apple\Player\AppleMediaPlayer.cs:225
at MediaManager.Platforms.Apple.Player.AppleMediaPlayer.Stop () [0x0002d] in C:\Users\mhvdi\Documents\OpenSource\XamarinMediaManager\MediaManager\Platforms\Apple\Player\AppleMediaPlayer.cs:233
at MediaManager.Platforms.Apple.Player.AppleMediaPlayer.DidFinishPlaying (Foundation.NSNotification obj) [0x000af] in C:\Users\mhvdi\Documents\OpenSource\XamarinMediaManager\MediaManager\Platforms\Apple\Player\AppleMediaPlayer.cs:162
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.3.2/src/Xamarin.iOS/Foundation/NSAction.cs:178
--- End of stack trace from previous location where exception was thrown ---
at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.3.2/src/Xamarin.iOS/UIKit/UIApplication.cs:86
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.3.2/src/Xamarin.iOS/UIKit/UIApplication.cs:65