2021年4月4日星期日

Xamarin IOS - Picking Video from Gallery or Camera and display in CollectionViewCell

I'm trying to display a video picked from Gallery or Camera roll in iOS using Xamarin. It works great for images and documents (iCloud) but for video U can't achieve my goal.

Here is the UIImagePickerControllerDelegate for Gallery:

public class UIGalleryMediaPickerDelegate : UIImagePickerControllerDelegate  {      Action<string, string> onMediaPicked;        public UIGalleryMediaPickerDelegate(Action<string, string> pOnMediaPicked)      {          onMediaPicked = pOnMediaPicked;      }        public override void FinishedPickingImage(UIImagePickerController pPicker, UIImage pImage, NSDictionary pInfo)      {          pPicker.DismissViewController(true, null);      }        public override void Canceled(UIImagePickerController pPicker)      {          pPicker.DismissViewController(true, null);      }        public override void FinishedPickingMedia(UIImagePickerController pPicker, NSDictionary pInfo)      {          PHAsset pHAsset = null;          string filename = null;          string extension = null;            if (pInfo[UIImagePickerController.MediaType].ToString() == "public.image")          {              NSUrl imageUrl = null;                if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))              {                  imageUrl = pInfo[UIImagePickerController.ImageUrl] as NSUrl;                  pHAsset = pInfo[UIImagePickerController.PHAsset] as PHAsset;              }              else              {                  imageUrl = pInfo[UIImagePickerController.ReferenceUrl] as NSUrl;                  pHAsset = PHAsset.FetchAssets(new NSUrl[] { imageUrl }, null).firstObject as PHAsset;              }                filename = ((NSString)pHAsset.ValueForKey(new NSString("filename"))).DeletePathExtension().ToString();              extension = imageUrl.PathExtension;                PCL.Storage.SaveImageToFolder(pInfo[UIImagePickerController.OriginalImage] as UIImage, filename, PCL.storage.GetPrivateMediaFolder());          }          else          {              NSUrl videoUrl = null;                videoUrl = pInfo[UIImagePickerController.MediaURL] as NSUrl;                if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))              {                  pHAsset = pInfo[UIImagePickerController.PHAsset] as PHAsset;              }              else              {                  pHAsset = PHAsset.FetchAssets(new NSUrl[] { videoUrl }, null).firstObject as PHAsset;              }                filename = ((NSString)pHAsset.ValueForKey(new NSString("filename"))).DeletePathExtension().ToString();              extension = videoUrl.PathExtension;                PCL.Storage.SaveVideoToFolder(pInfo[UIImagePickerController.MediaURL] as NSUrl, filename, PCL.storage.GetPrivateMediaFolder());          }            pPicker.DismissViewController(true, () => onMediaPicked?.Invoke(filename, extension));      }  }  

Here is the Delegate when picking from camera

public class UICameraMediaPickerDelegate : UIImagePickerControllerDelegate  {      Action<string, string> onMediaPicked;        public UICameraMediaPickerDelegate(Action<string, string> pOnMediaPicked)      {          onMediaPicked = pOnMediaPicked;      }        public override void FinishedPickingImage(UIImagePickerController pPicker, UIImage pImage, NSDictionary pInfo)      {          pPicker.DismissViewController(true, null);      }        public override void Canceled(UIImagePickerController pPicker)      {          pPicker.DismissViewController(true, null);      }        public override void FinishedPickingMedia(UIImagePickerController pPicker, NSDictionary pInfo)      {          string filename = Guid.NewGuid().ToString("N");          string extension = null;            if (pInfo[UIImagePickerController.MediaType].ToString() == "public.image")          {              PCL.Storage.SaveImageToFolder(pInfo[UIImagePickerController.OriginalImage] as UIImage, filename, PCL.storage.GetPrivateMediaFolder());              extension = "JPEG";          }          else          {              PCL.Storage.SaveVideoToFolder(pInfo[UIImagePickerController.MediaURL] as NSUrl, filename, PCL.storage.GetPrivateMediaFolder());              extension = "MOV";          }            pPicker.DismissViewController(true, () => onMediaPicked?.Invoke(filename, extension));      }  }  

As you can see i save all video and images picked from gallery or camera to the app private storage. Then it's the code that will display the video inside a collectionview cell

Player = new AVPlayer(new AVPlayerItem(NSUrl.FromString(PCL.Storage.GetMediaFilenamePath(pMedia.Filename))));              Player = new AVPlayer(NSUrl.FromString(PCL.Storage.GetMediaFilenamePath(pMedia.Filename)));              PlayerLayer = AVPlayerLayer.FromPlayer(Player);              PlayerLayer.VideoGravity = AVLayerVideoGravity.ResizeAspectFill;              PlayerLayer.Frame = videoContainerView.Frame;              PlayerLayer.BackgroundColor = UIColor.Red.CGColor;              videoContainerView.Layer.Sublayers = null;              videoContainerView.Layer.AddSublayer(PlayerLayer);              videoContainerView.Hidden = false;  

What I find really strange is that when I pick a video from Gallery the filename starts with "IMG" which means image obviously ... but I picked a video ... So I really don't understand the point.

Following screenshot of the debugger showing picked files. The first 4 files are videos, 2 lasts are images.

enter image description here

https://stackoverflow.com/questions/66945968/xamarin-ios-picking-video-from-gallery-or-camera-and-display-in-collectionview April 05, 2021 at 05:19AM

没有评论:

发表评论