Playing a video with Xamarin for iOS broken after latest update - ios

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);
}

Related

Xamarin Forms: Webview showing slight black line at bottom

I am creating an app using Xamarin Forms using PCL project. In ios, a black line is shown at the bottom of the web view only. I don't experience the same issue in android or windows 10.
As noted in the comments, I dealt with this before. It has to do with the background color of the webview. I have tried several things, the only thing that worked was setting this:
webView.Opaque = false;
webView.BackgroundColor = UIColor.Clear;
In a custom renderer on the UIWebView.
A complete implemented custom renderer would then look like this:
[assembly: ExportRenderer (typeof (WebView), typeof (WebViewRenderer))]
namespace YourApp.iOS.Renderers
{
public class WebViewRenderer : Xamarin.Forms.Platform.iOS.WebViewRenderer
{
protected override void OnElementChanged (VisualElementChangedEventArgs e)
{
base.OnElementChanged (e);
if (NativeView != null) {
var webView = (UIWebView)NativeView;
webView.Opaque = false;
webView.BackgroundColor = UIColor.Clear;
}
}
}
}

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);
}

Can not Capture Video Using iOS Default Camera Application. Xamarin.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

Multitouch not working on iOS with robovm and libGDX when displaying banner ads

I'm having a problem with input in libGDX in the iOS backend. It happens when I have Mopub banner ads displayed. When I put my first finger on the screen, I get a touchDown event (pointer = 0) and when it comes to my second finger, nothing is triggered. BUT for some reason it works when I put my second finger near the banner area (I think it's the banner's frame that I'm hitting). When banner ads aren't displayed, everything works fine. Also everything works fine on Android.
I'd really appreciate the help to tackle this problem here.
Thanks in advance.
iOS 8.3;
roboVM 1.2.0;
gdx 1.6.0;
Here's how I load the Banner:
UIApplication application;
String id;
BANNER_SIZE = MPConstants.MOPUB_BANNER_SIZE;
id = BANNER_ID;
rootViewController = application.getKeyWindow().getRootViewController();
banner = new MPAdView(id, BANNER_SIZE);
double bannerWidth = UIScreen.getMainScreen().getBounds().getWidth();
double bannerHeight = bannerWidth / BANNER_SIZE.getWidth() * BANNER_SIZE.getHeight();
banner.setFrame(new CGRect((UIScreen.getMainScreen().getBounds().getWidth() / 2d) - (BANNER_SIZE.getWidth() * .5d), 0, bannerWidth, bannerHeight));
adViewController = new MPAdViewController(banner);
MPAdViewDelegate bannerDelegate = new MPAdViewDelegateAdapter(){
#Override
public UIViewController getViewController() {
return adViewController;
}
};
banner.setDelegate(bannerDelegate);
adViewController.getView().addSubview(banner);
rootViewController.getView().addSubview(adViewController.getView());
if(!isBannerLoaded) {
banner.loadAd();
isBannerLoaded = true;
}
Here's my didFinishLaunching method:
#Override
public boolean didFinishLaunching(UIApplication application, UIApplicationLaunchOptions launchOptions) {
super.didFinishLaunching(application, launchOptions);
//The 0 doesn't do anything. It was something I was trying out.
adController.loadBanner(application, 0);
rootViewController = application.getKeyWindow().getRootViewController();
application.getKeyWindow().setRootViewController(rootViewController);
application.getKeyWindow().addSubview(rootViewController.getView());
application.getKeyWindow().makeKeyAndVisible();
return false;
}
You should add this line to your adViewController
adViewController.getView().setMultipleTouchEnabled(true);

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

Resources