Hi All,
I'm currently writing a custom renderer, in order to display a video control on a ContentPage.
The basics of the player are working - however, I'm unable to find any documentation/guidance on how I should set the size of my custom view. A majority of the help seems to be around doing things like adding support for rounded corners or borders to existing renders, rather than creating entirely new views.
My problem is that the aspect-ratio of the video is not being respected, and the WidthRequest property is being ignored. I'm able to set a custom height, but this results in distorted video.
I can see that the class ViewRenderer<TView, TNativeView>
has a method called GetDesiredSize(int widthConstraint, int heightConstraint)
- I'm assuming that I'll have to use this some-how? The Native control (an Android VideoView) has SetMinimumWidth(int minWidth)
and SetMinimumHeight(int minHeight)
methods, but the Width
and Height
properties are read-only.
I'm currently focusing on the Android Renderer.
Can anyone offer any advice?
Many thanks.
I suggest that you create two views in your custom renderer, one that is the parent that will be sized according to the data passed from Xamarin Forms, and one inner view that contains the video player that you can resize correctly from the video you are showing.
Answers
I suggest that you create two views in your custom renderer, one that is the parent that will be sized according to the data passed from Xamarin Forms, and one inner view that contains the video player that you can resize correctly from the video you are showing.
Thanks, @ChristianFalch. I'll give that a try and let you know how I get on.
@JosephRedfern did you manage to progress this - i am trying to do something similar
Oops - sorry @ChristianFalch, I forget to get back to you (I hate it when people do that!).
@AndyEvans, I found a compromise, which was to do as Christian suggested and put the VideoView inside a RelativeLayout. This allowed me to use WidthRequest on the custom control in the Xamarin forms project, which seemed to get passed on to the actual Android View and respected. The video view inside the RelativeLayout then resizes to fit, and respects aspect ratio etc.
Could really do with some more documentation around this, though - having to work things out by trial and error isn't ideal!
Thanks @JosephRedfern - that helps, I am still struggling to maintain video aspect ratio though. Do you have any code you can post?
@AndyEvans I'm afraid I can't share the code, as it doesn't belong to me. Do you have any code that you could post, so we can attempt to debug it?
Thanks - would be helpful. I am using C# rather than xaml. This is the constructor of the content page that the video sits in. The video is part of a form along with a couple of buttons to choose the video / take a new video. The video takes about 25% of the screen height. This all works correctly, however the apsect ratio is wrong. If I don't set a width the video stretches to full screen width and if I set a width it is honoured (maybe i need to work out the width request for each video?) What I want to happen is to set the height (25%) and then for the width to be calculated from that, maintaining the aspect. I have a similar page for photos and that works fine - there is an Aspect property in the OOTB image view.
//create the buttons to take and choose photos
var takeVideoButton = new MediaButton(CaptureType.Take, displayService);
var getVideoButton= new MediaButton(CaptureType.Gallery, displayService);
Ah, I should have been more clear in my reply - The RelativeLayout I was referring to was for the renderer itself - i.e. VideoViewRenderer should implement
ViewRenderer<VideoView, Android.Widget.RelativeLayout>
, and the Native Control that the renderer should export is an Android RelativeLayout containing an Android VideoView.That works in my case - I'm able to do a HeightRequest directly on the custom control.
aha - that does it - many thanks
Xamarin Forms really needs a simple video control as its such a common requirement. I am including the full Android renderer code in case it helps anyone else.
using System;
using System.ComponentModel;
using Android.Views;
using Android.Widget;
using MyShowcase.Droid.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using VideoView = Android.Widget.VideoView;
[assembly: ExportRenderer(typeof(MyShowcase.Views.VideoView), typeof(VideoViewRenderer))]
namespace MyShowcase.Droid.Renderers
{
///
/// This renderer displays a video along with a video control set (play / pause)
///
public class VideoViewRenderer : ViewRenderer<MyShowcase.Views.VideoView, Android.Widget.RelativeLayout>
{
private VideoView _videoview;
private string _currentFileSource="";
private MediaController _mediaController;
}
@AndyEvans thanks for your source code. I have the problem with your code, that I only get a black screen. I hear the sound though. Would you mind sharing the Xaml section where you have your VideoView?
@RogerSchmidlin - I'm not using xaml. Here is the C# code - HTH
In my Xamarin Forms page in the shared project
_public class AddVideoItemPage : AddItemBasePage
{
private readonly IMediaPicker _mediaPicker;
private MediaFile _mediaFile;
private MediaButton _takeVideoButton;
private MediaButton _getVideoButton;
private readonly VideoView _video;
My Xamarin Forms video view that uses the Android render:
public class VideoView : View
{
public Action StopAction;
public VideoView()
{
BackgroundColor=Color.White;
}
Thanks @AndyEvans, I found my bug. I called play on the MediaPlayer rather than the VideoView. Now it works
Would you mind sharing your implementation of the VideoViewRenderer for iOS?
Glad it works - I didn't do an implementation for iOS unfortunately, I was asked to move on to other work after the Android app was complete with the idea of going back to it at a later date.
I got two more questions
1 - Does your video fill up the whole page? I would like to use up as much space as I can, but it only covers about 80% in landscape mode. It fills the whole width in portrait.
2 - Fullscreen mode: Have you found a way to get rid of the status bar? The methods mentioned in some of the threads don't work for me.
Found what I had to do to always resize the video to the full view (in the renderer):
`
// make sure video fills the parent view
LayoutParams layoutParams = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
videoview.LayoutParameters = layoutParams;
`
Still looking for a way to get rid of the status bar on top to have real full screen video