/
PlayerActivity.cs
204 lines (181 loc) · 7.75 KB
/
PlayerActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using AndroidX.AppCompat.App;
using AndroidX.AppCompat.Widget;
using AndroidX.Core.Widget;
using com.aa.tvshows.Helper;
using Newtonsoft.Json;
using System.Linq;
using Android.Views;
using Com.Google.Android.Exoplayer2.Source.Dash;
using Com.Google.Android.Exoplayer2.Source.Smoothstreaming;
using Com.Google.Android.Exoplayer2.Source.Dash.Manifest;
using Com.Google.Android.Exoplayer2.Source.Smoothstreaming.Manifest;
using Com.Google.Android.Exoplayer2.UI;
using Com.Google.Android.Exoplayer2.Upstream;
using Com.Google.Android.Exoplayer2.Source;
using Com.Google.Android.Exoplayer2;
using Com.Google.Android.Exoplayer2.Offline;
using Com.Google.Android.Exoplayer2.Source.Hls;
using Com.Google.Android.Exoplayer2.Source.Hls.Playlist;
using Com.Google.Android.Exoplayer2.Util;
using Com.Google.Android.Exoplayer2.Trackselection;
namespace com.aa.tvshows
{
[Activity(Label = "PlayerActivity", ScreenOrientation = Android.Content.PM.ScreenOrientation.SensorLandscape, ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
public class PlayerActivity : AppCompatActivity, PlayerControlView.IVisibilityListener
{
SimpleExoPlayer player;
PlayerView playerView;
AppCompatSpinner linksSpinner;
List<StreamingUri> mediaStreams;
bool isSystemUIVisible = true;
IDataSourceFactory mediaSourceFactory;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.player_view);
Window.DecorView.SystemUiVisibilityChange += VideoPlayer_SystemUiVisibilityChange;
InitializePlayback(null);
}
private void InitializePlayback(StreamingUri itemToPlay)
{
if (mediaStreams == null)
mediaStreams = JsonConvert.DeserializeObject<List<StreamingUri>>(Intent.GetStringExtra("mediaStreams"));
if (player is null)
{
mediaSourceFactory = new DefaultDataSourceFactory(this, Util.GetUserAgent(this, Application.PackageName));
player = new SimpleExoPlayer.Builder(this).Build();
playerView = FindViewById<PlayerView>(Resource.Id.playerView);
playerView.Player = player;
playerView.RequestFocus();
playerView.SetControllerVisibilityListener(this);
linksSpinner = FindViewById<AppCompatSpinner>(Resource.Id.linksSpinner);
player.PlayWhenReady = true;
playerView.ControllerShowTimeoutMs = 3000;
if (mediaStreams != null && mediaStreams.Count > 0)
{
var titles = new List<string>() { };
mediaStreams.ForEach(a => titles.Add(a.StreamingQuality));
linksSpinner.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, titles);
linksSpinner.ItemSelected += LinksSpinner_ItemSelected;
linksSpinner.SetSelection(0);
}
}
if (itemToPlay != null)
{
try
{
player.Prepare(CreateMediaSource(itemToPlay));
playerView.KeepScreenOn = true;
}
catch (Exception ex)
{
Error.Instance.ShowErrorTip("There is something wrong with ExoPlayer. Please try any external player from settings.", this, ToastLength.Long);
}
}
}
public override bool OnTouchEvent(MotionEvent e)
{
if (e.Action == MotionEventActions.Down || e.Action == MotionEventActions.HoverEnter)
{
if (isSystemUIVisible)
{
playerView.HideController();
}
else
{
playerView.ShowController();
}
return true;
}
return base.OnTouchEvent(e);
}
private void VideoPlayer_SystemUiVisibilityChange(object sender, Android.Views.View.SystemUiVisibilityChangeEventArgs e)
{
isSystemUIVisible = e.Visibility == Android.Views.StatusBarVisibility.Visible ? true : false;
if (isSystemUIVisible)
{
if (!playerView.IsControllerVisible) playerView.ShowController();
}
else
{
if (playerView.IsControllerVisible) playerView.HideController();
}
}
private IMediaSource CreateMediaSource(StreamingUri link)
{
Android.Net.Uri uri = Android.Net.Uri.Parse(link.StreamingUrl.OriginalString);
IMediaSource source = null;
if (link.StreamingUrl.OriginalString.Contains(".mpd"))
{
source = new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(mediaSourceFactory), mediaSourceFactory)
.SetManifestParser(new FilteringManifestParser(new DashManifestParser(), new List<StreamKey>()))
.CreateMediaSource(uri);
}
if (link.StreamingUrl.OriginalString.Contains(".m3u8"))
{
source = new HlsMediaSource.Factory(mediaSourceFactory).CreateMediaSource(uri);
}
if (link.StreamingUrl.OriginalString.Contains(".mp4"))
{
source = new ProgressiveMediaSource.Factory(mediaSourceFactory).CreateMediaSource(uri);
}
return source;
}
private void LinksSpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
var item = mediaStreams[e.Position];
InitializePlayback(item);
}
protected override void OnPause()
{
base.OnPause();
if (player != null)
player.PlayWhenReady = false;
}
public override void OnBackPressed()
{
base.OnBackPressed();
player.PlayWhenReady = false;
player.Release();
playerView.KeepScreenOn = false;
player = null;
}
private void HideSoftwareMenuBars()
{
int uiOptions = (int)Window.DecorView.SystemUiVisibility;
uiOptions |= (int)SystemUiFlags.LowProfile;
uiOptions |= (int)SystemUiFlags.LayoutFullscreen | (int)SystemUiFlags.Fullscreen;
uiOptions |= (int)SystemUiFlags.LayoutHideNavigation | (int)SystemUiFlags.HideNavigation;
Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
isSystemUIVisible = false;
}
private void ShowSoftwareMenuBars()
{
int uiOptions = (int)Window.DecorView.SystemUiVisibility;
uiOptions |= (int)SystemUiFlags.Visible;
uiOptions |= (int)SystemUiFlags.LayoutFullscreen;
uiOptions |= (int)SystemUiFlags.LayoutHideNavigation;
Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
Window.SetFlags(WindowManagerFlags.LayoutNoLimits, WindowManagerFlags.LayoutNoLimits);
isSystemUIVisible = true;
}
public void OnVisibilityChange(int visibility)
{
if ((ViewStates)visibility == ViewStates.Gone || (ViewStates)visibility == ViewStates.Invisible)
{
HideSoftwareMenuBars();
}
else
{
ShowSoftwareMenuBars();
}
}
}
}