Can not Capture Video Using iOS Default Camera Application. Xamarin.iOS - ios

I am trying to launch the default iOS camera application for video recording but it does not work.
Whenever I launch the application it crashes and and does not show any error log or any other error messages.
The following code works perfectly if I set the imagePicker.CameraCaptureMode to UIImagePickerControllerCameraCaptureMode.Photo.
var imagePicker = new UIImagePickerController();
imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;
imagePicker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video;
var imagePickerDelegate = new ImagePickerDelegate(this);
imagePicker.Delegate = imagePickerDelegate;
NavigationController.PresentModalViewController(imagePicker, true);
Thanks in advance

I got it to work by doing this:
var imagePicker = new UIImagePickerController();
imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;
imagePicker.MediaTypes = new string[]{ UTType.Movie }; // ADD this
var imagePickerDelegate = new ImagePickerDelegate(this);
imagePicker.Delegate = imagePickerDelegate;
NavigationController.PresentModalViewController(imagePicker, true);
Also you can set your delegate calls like so:
imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia;
imagePicker.Canceled += Handle_Canceled;
Then create these methods:
protected void Handle_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
//code to handle picking media
}
void Handle_Canceled(object sender, EventArgs e)
{
imagePicker.DismissViewController(true, null);
}
Update
In iOS 10 you need to add the permissions and provide the description as to why you are asking for the permission in the info.plist
see here:
iOS 10 - Changes in asking permissions of Camera, microphone and Photo Library causing application to crash

Related

iOS 11 application crashes when trying to add attachment from camera

I have an application that crashes when the user tries to add an attachment from the camera (however it doesn't crash when they opt to load the attachment from their Photo Library).
Here's the method that gets called when an option (either Camera or Photo Library) is selected:
public void AddMedia(UIImagePickerControllerSourceType type)
{
_imagePicker = new UIImagePickerController();
// set our source to the photo library
_imagePicker.SourceType = type;
// set what media types
_imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes(type);
_imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia;
_imagePicker.Canceled += (sender, evt) =>
{
Console.WriteLine("picker cancelled");
_imagePicker.DismissViewController(false, () =>
{
});
};
//PresentModalViewController is depreciated in iOS6 so we use PresentViewController
Parent.PresentViewController(_imagePicker, true, null);
}
And this is the line where it crashes:
_imagePicker.SourceType = type;
Why would it crash when setting it to Camera but not Photo Library? Does it have something to do with how the enum is ordered (Photo Library = 0, Camera = 1, Saved Photos Album = 2)?
Are you trying to reproduce it on simulator? Because you don't have an access to camera on it.

I couldn't create AdMob banner in my Xamarin.ios

I'm trying to add AdMob banner in my Xamarin iOS project. You can see my code below.
public void AddAdvirtesement()
{
var adView = new BannerView();
adView.Frame = new CGRect(0, View.Bounds.Height - 50, View.Bounds.Width, 50);
adView.BackgroundColor = UIColor.Gray;
adView.AdUnitID = bannerId;
adView.RootViewController = this;
View.AddSubview(adView);
var request = Request.GetDefaultRequest();
string[] testdevices = new string[2];
//testdevices[0] = Request.SimulatorId.ToString();
//testdevices[1] = "b5f64ec8566bf1a5cd1cb853d7106aa7";
request.TestDevices = testdevices;
adView.LoadRequest(request);
}
i get this output above code:
2016-12-11 22:25:42.659 betcluev4[9220:627724] You are
currently using version 7.11.0 of the SDK. Please consider updating
your SDK to the most recent SDK version to get the latest features and
bug fixes. The latest SDK can be downloaded from .......
but I already have the latest SDK of Firebase (7.11.0). Any thoughts?
Check out the plist configurations for FireBase that are needed in your project , and the sample code below
Configurations
Once you have your GoogleService-Info.plist file downloaded in your computer, do the following steps in Xamarin Studio:
Add GoogleService-Info.plist file to your app project.
Set GoogleService-Info.plist build action behaviour to Bundle Resource by Right clicking/Build Action.
Open GoogleService-Info.plist file and change IS_ADS_ENABLED value to Yes.
Add the following line of code somewhere in your app, typically in your AppDelegate's FinishedLaunching method (don't forget to import Firebase.Analytics namespace):
App.Configure ();
Sample Code
using Google.MobileAds;
const string bannerId = "<Get your ID at google.com/ads/admob>";
BannerView adView;
bool viewOnScreen = false;
public void AddBanner ()
{
// Setup your BannerView, review AdSizeCons class for more Ad sizes.
adView = new BannerView (size: AdSizeCons.Banner, origin: new CGPoint (0, 0)) {
AdUnitID = bannerId,
RootViewController = this
};
// Wire AdReceived event to know when the Ad is ready to be displayed
adView.AdReceived += (object sender, EventArgs e) => {
if (!viewOnScreen) {
View.AddSubview (adView);
viewOnScreen = true;
}
};
var request = Request.GetDefaultRequest ();
// Requests test ads on devices you specify. Your test device ID is printed to the console when
// an ad request is made. GADBannerView automatically returns test ads when running on a
// simulator. After you get your device ID, add it here
request.TestDevices = new [] { Request.SimulatorId.ToString () };
// Request an ad
adView.LoadRequest (request);
}

Playing a video with Xamarin for iOS broken after latest update

The following code in Xamarin for iOS was working fine prior to the Xamarin for iOS update to v2.0.50727
This is the code in a custom renderer in a Xamarin Forms app
class WatchVideoRenderer : PageRenderer
{
MPMoviePlayerController moviePlayer;
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
var url = new NSUrl("http://192.168.12.4:8085/MediaUploads/1/211/520140731170618/DPM202.mp4");
moviePlayer = new MPMoviePlayerController();
moviePlayer.ContentUrl = url;
moviePlayer.View.Frame = new CGRect((float)((NativeView.Bounds.Width - 600) / 2), (float)((NativeView.Bounds.Height - 450) / 2), 600, 400);
MPMoviePlayerController.Notifications.ObserveLoadStateDidChange(OnLoadStateChanged);
MPMoviePlayerController.Notifications.ObservePlaybackDidFinish(OnPlaybackComplete);
View.AddSubview(moviePlayer.View);
moviePlayer.PrepareToPlay();
moviePlayer.ShouldAutoplay = true;
moviePlayer.Play();
}
private void OnLoadStateChanged(object sender, NSNotificationEventArgs e)
{
if (moviePlayer.LoadState == MPMovieLoadState.Playable)
{
}
}
private void OnPlaybackComplete(object sender, MPMoviePlayerFinishedEventArgs e)
{
}
}
As i said this was working till day before yesterday, after which I installed 2 updates on Xamarin. iOS & this is now failing. All i see is a black canvas & the video never loads.
No notifications from the MPMoviePlayerController are ever raised.
There is a release of this app scheduled for next week & this last minute bug is causing me headaches. Any help is really appreciated.
I resolved a similar issue by pushing a new image context and that did the trick. I modified the code to include the BeginImageContext() and EndImageContext().
UIGraphics.BeginImageContext(new CGSize(1,1));
var url = new NSUrl("http://192.168.12.4:8085/MediaUploads/1/211/520140731170618/DPM202.mp4");
moviePlayer = new MPMoviePlayerController();
moviePlayer.ContentUrl = url;
moviePlayer.View.Frame = new CGRect((float)((NativeView.Bounds.Width - 600) / 2), (float)((NativeView.Bounds.Height - 450) / 2), 600, 400);
UIGraphics.EndImageContext();
Try switching to AVPlayerViewController instead of MPMoviePlayerController. Throw this code inside your OnElementChanged method.
My renderer was a view renderer and not a page renderer so you might have to tweak it a bit.
if(Control == null)
{
AVPlayerViewController avpvc;
AVPlayer avp;
var url = NSUrl.FromString("SOME URL HERE"); //or NSUrl.FromFile
avp = new AVPlayer(url);
avpvc = new AVPlayerViewController();
avpvc.Player = avp;
avpvc.ShowsPlaybackControls = true;
avp.Play();
this.SetNativeControl(avpvc.View);
}

Unable to play mp4 file on iOS7

I'm currently trying to get an mp4 to play in my app. The MP4 plays fine in vlc (basic check to ensure it's not broken).
The code I'm using looks like this
private void startAnimation()
{
using (var pool = new NSAutoreleasePool())
{
InvokeOnMainThread(() =>
{
player = new MPMoviePlayerController(NSUrl.FromFilename("Graphics/videos/noaudio-data-download.mp4"))
{
AllowsAirPlay = true,
Fullscreen = true,
ScalingMode = MPMovieScalingMode.Fill,
RepeatMode = MPMovieRepeatMode.One,
SourceType = MPMovieSourceType.File,
ShouldAutoplay = true,
ControlStyle = MPMovieControlStyle.Fullscreen,
};
player.View.Frame = View.Bounds;
View.AddSubview(player.View);
View.BringSubviewToFront(player.View);
player.PrepareToPlay();
player.Play();
});
}
}
private void stopAnimation()
{
player.Stop();
player.Dispose();
}
All I get is a black screen and the incredibly unhelpful error
2014-01-05 22:00:44.995 ftrack2ios[85614:80b] _itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
From what I've read on here and other forums, this error can be down to a pile of different reasons, most of them seem to be down to resizing.
The error occurs on a device as well as on the simulator. I'm not sure if this in iOS 7 issue as it used to work on iOS 6 after some playing around.
I have same problem, and I found that
https://discussions.apple.com/message/23203292#23203292
http://www.techisky.com/how-to/play-mp4-on-ios-7-ipad.html

Monotouch - UIImagePickerController with an iPad App

I have an iPad only application that I'm trying to allow users to select images from their PhotoLibrary, near as I can tell I have to use the UIImagePickerController in a UIPopOverController. I have attempted many different ways to do this but I can get anything to work. I've seen a lot of code snippets but I can't seem to get them working under Monotouch.
Could somebody point me at the correct way to do this? I greatly appreciate it.
I had to call the code that creates the image picker and the code that reacts to the image being picked from the main thread to get it to work:
partial void OnImport (UIButton s)
{
BeginInvokeOnMainThread(delegate
{
UIImagePickerController picker = new UIImagePickerController();
picker.ContentSizeForViewInPopover = new System.Drawing.SizeF(320,480);
UIPopoverController popover = new UIPopoverController(picker);
picker.FinishedPickingImage += delegate(object sender, UIImagePickerImagePickedEventArgs e)
{
BeginInvokeOnMainThread(delegate
{
UIImage image = (UIImage)info.ObjectForKey(new NSString("UIImagePickerControllerOriginalImage"));
picker.DismissModalViewControllerAnimated(true);
// do something with image
});
};
picker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
popover.PresentFromRect(s.Frame, this.View, UIPopoverArrowDirection.Left, true);
});
}
Here is code that I've used in an app - this should serve as a good start for you to get it working.
UIImagePickerController imagePicker;
UIPopoverController popOver;
void AttachImageBtnTouched(object sender, EventArgs e)
{
if (popOver == null || popOver.ContentViewController == null)
{
imagePicker = new UIImagePickerController();
popOver = new UIPopoverController(imagePicker);
ImagePickerDelegate imgDel = new ImagePickerDelegate();
imagePicker.Delegate = imgDel;
imagePicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
}
if (popOver.PopoverVisible)
{
popOver.Dismiss(true);
imagePicker.Dispose();
popOver.Dispose();
return;
}
else
{
popOver.PresentFromRect(btnAttachment.Frame, this.View, UIPopoverArrowDirection.Any, true);
}
}
// The Delegate class looks something like
public class ImagePickerDelegate : UIImagePickerControllerDelegate
{
public ImagePickerDelegate()
{}
public override void FinishedPickingMedia(UIImagePickerController picker, NSDictionary info)
{
UIImage image = (UIImage)info.ObjectForKey(new NSString("UIImagePickerControllerOriginalImage"));
// do whatever else you'd like to with the image
}
}

Resources