Commit acb115bc authored by Martin Finkel's avatar Martin Finkel

Forms.Android: Workaround the first layout change not being raised when...

Forms.Android: Workaround the first layout change not being raised when VideoView is behind a Xamarin.Forms custom renderer on Android.

Also removed dead code in SurfaceCallback and refactored it into the VideoView
parent c98941d7
...@@ -39,6 +39,7 @@ namespace LibVLCSharp.Forms.Platforms.Android ...@@ -39,6 +39,7 @@ namespace LibVLCSharp.Forms.Platforms.Android
private void OnMediaPlayerChanged(object sender, MediaPlayerChangedEventArgs e) private void OnMediaPlayerChanged(object sender, MediaPlayerChangedEventArgs e)
{ {
Control.MediaPlayer = e.NewMediaPlayer; Control.MediaPlayer = e.NewMediaPlayer;
Control.TriggerLayoutChangeListener();
} }
} }
} }
\ No newline at end of file
using System;
using LibVLCSharp.Shared;
using Org.Videolan.Libvlc;
namespace LibVLCSharp.Platforms.Android
{
public class SurfaceCallback : Java.Lang.Object, AWindow.ISurfaceCallback
{
static readonly object _locker = new object();
readonly MediaPlayer _mp;
public SurfaceCallback() { }
public SurfaceCallback(MediaPlayer mp)
{
_mp = mp;
}
public void OnSurfacesCreated(AWindow vout)
{
var play = false;
var enableVideo = false;
lock (_locker)
{
if (!_mp.IsPlaying /* && mp.PlayRequested*/)
{
play = true;
}
else if (_mp.VoutCount == 0)
enableVideo = true;
}
if (play)
_mp.Play();
else if (enableVideo)
{
lock (_locker)
{
_mp.SetVideoTrack(0);
}
}
}
public void OnSurfacesDestroyed(AWindow vout)
{
var disableVideo = false;
lock (_locker)
{
if (_mp.VoutCount > 0)
disableVideo = true;
}
if (disableVideo)
{
//_mp.VideoTrackEnabled = false;
}
}
}
}
\ No newline at end of file
...@@ -11,7 +11,7 @@ using Org.Videolan.Libvlc; ...@@ -11,7 +11,7 @@ using Org.Videolan.Libvlc;
namespace LibVLCSharp.Platforms.Android namespace LibVLCSharp.Platforms.Android
{ {
public class VideoView : SurfaceView, IVLCVoutCallback, IVideoView public class VideoView : SurfaceView, IVLCVoutCallback, IVideoView, AWindow.ISurfaceCallback
{ {
MediaPlayer _mediaPlayer; MediaPlayer _mediaPlayer;
AWindow _awindow; AWindow _awindow;
...@@ -68,7 +68,7 @@ namespace LibVLCSharp.Platforms.Android ...@@ -68,7 +68,7 @@ namespace LibVLCSharp.Platforms.Android
if (_mediaPlayer == null) if (_mediaPlayer == null)
throw new NullReferenceException(nameof(_mediaPlayer)); throw new NullReferenceException(nameof(_mediaPlayer));
_awindow = new AWindow(new SurfaceCallback(_mediaPlayer)); _awindow = new AWindow(this);
_awindow.AddCallback(this); _awindow.AddCallback(this);
_awindow.SetVideoView(this); _awindow.SetVideoView(this);
_awindow.AttachViews(); _awindow.AttachViews();
...@@ -96,6 +96,11 @@ namespace LibVLCSharp.Platforms.Android ...@@ -96,6 +96,11 @@ namespace LibVLCSharp.Platforms.Android
_awindow = null; _awindow = null;
} }
/// <summary>
/// This is to workaround the first layout change not being raised when VideoView is behind a Xamarin.Forms custom renderer on Android.
/// </summary>
public void TriggerLayoutChangeListener() => _awindow?.SetWindowSize(Width, Height);
public virtual void OnSurfacesCreated(IVLCVout vout) public virtual void OnSurfacesCreated(IVLCVout vout)
{ {
} }
...@@ -114,5 +119,13 @@ namespace LibVLCSharp.Platforms.Android ...@@ -114,5 +119,13 @@ namespace LibVLCSharp.Platforms.Android
Detach(); Detach();
} }
void AWindow.ISurfaceCallback.OnSurfacesCreated(AWindow aWindow)
{
}
void AWindow.ISurfaceCallback.OnSurfacesDestroyed(AWindow aWindow)
{
}
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment