VideoView.cs 2.23 KB
Newer Older
1
2
3
4
5
using System;

using Android.Content;
using Android.Runtime;
using Android.Util;
6
using Android.Views;
7

8
using LibVLCSharp.Shared;
9

10
11
12
13
using Org.Videolan.Libvlc;

namespace LibVLCSharp.Platforms.Android
{
14
    public class VideoView : SurfaceView, IVLCVoutCallback, IVideoView
15
16
17
18
    {
        MediaPlayer _mediaPlayer;
        Instance _instance;
        AWindow _awindow;
19
        LayoutChangeListener _layoutListener;
20

21
        #region ctors
22

23
        public VideoView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) => Init();
24

25
26
27
        public VideoView(Context context) : base(context) => Init();

        public VideoView(Context context, IAttributeSet attrs) : base(context, attrs) => Init();
28

29
30
31
32
33
34
35
36
37
38
39
        public VideoView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) => Init();

        public VideoView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes) => Init();

        #endregion

        public MediaPlayer MediaPlayer => _mediaPlayer;
        public Instance Instance => _instance;

        public void Attach()
        {
40
41
            _awindow = new AWindow(new SurfaceCallback(_mediaPlayer));
            _awindow.AddCallback(this);
42
43
44
            _awindow.SetVideoView(this);
            _awindow.AttachViews();

45
            _mediaPlayer.SetAndroidContext(_awindow.Handle);
46
47
48

            _layoutListener = new LayoutChangeListener(_awindow);
            AddOnLayoutChangeListener(_layoutListener);
49
50
        }

51
        public void Detach()
52
        {
53
54
55
56
57
58
59
60
61
62
63
64
            _awindow.RemoveCallback(this);
            _awindow.DetachViews();

            _mediaPlayer.SetAndroidContext(IntPtr.Zero);

            RemoveOnLayoutChangeListener(_layoutListener);
            _layoutListener.Dispose();
            _layoutListener = null;

            _awindow.Dispose();
            _awindow = null;

65
66
        }

67
        public virtual void OnSurfacesCreated(IVLCVout vout)
68
69
70
        {
        }

71
        public virtual void OnSurfacesDestroyed(IVLCVout vout)
72
73
74
        {
        }

75
76
77
78
79
80
81
        void Init()
        {
            Core.Initialize();

            _instance = new Instance();
            _mediaPlayer = new MediaPlayer(_instance);
        }
82
83
    }
}