-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPlayerActivity.java
190 lines (161 loc) · 8.68 KB
/
PlayerActivity.java
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
package com.theoplayer.sample.playback.cast;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import com.google.android.gms.cast.framework.CastButtonFactory;
import com.theoplayer.android.api.cast.CastConfiguration;
import com.theoplayer.android.api.cast.CastIntegration;
import com.theoplayer.android.api.cast.CastIntegrationFactory;
import com.theoplayer.android.api.cast.CastStrategy;
import com.theoplayer.android.api.cast.chromecast.Chromecast;
import com.theoplayer.android.api.cast.chromecast.ChromecastConnectionCallback;
import com.theoplayer.android.api.event.chromecast.ChromecastEventTypes;
import com.theoplayer.android.api.event.player.PlayerEventTypes;
import com.theoplayer.android.api.player.Player;
import com.theoplayer.android.api.source.SourceDescription;
import com.theoplayer.sample.common.SourceManager;
import com.theoplayer.sample.playback.cast.databinding.ActivityPlayerBinding;
public class PlayerActivity extends AppCompatActivity {
private static final String TAG = PlayerActivity.class.getSimpleName();
private ActivityPlayerBinding viewBinding;
private Player theoPlayer;
private Chromecast theoChromecast;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(com.theoplayer.sample.common.R.style.TheoTheme_Base);
super.onCreate(savedInstanceState);
// Inflating view and obtaining an instance of the binding class.
viewBinding = DataBindingUtil.setContentView(this, R.layout.activity_player);
// Gathering THEO objects references.
theoPlayer = viewBinding.theoPlayerView.getPlayer();
// Add Cast integration.
CastConfiguration configuration = new CastConfiguration.Builder().castStrategy(CastStrategy.AUTO).build();
CastIntegration castIntegration = CastIntegrationFactory.createCastIntegration(viewBinding.theoPlayerView, configuration);
theoPlayer.addIntegration(castIntegration);
if (viewBinding.theoPlayerView.getCast() != null) {
theoChromecast = viewBinding.theoPlayerView.getCast().getChromecast();
}
// Configuring action bar.
setSupportActionBar(viewBinding.toolbarLayout.toolbar);
// Configuring THEOplayer playback with default parameters.
configureTHEOplayer();
// configureChromecast();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.activity_player_menu, menu);
CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), menu, R.id.castMenuItem);
return true;
}
private void configureTHEOplayer() {
// Coupling the orientation of the device with the fullscreen state.
// The player will go fullscreen when the device is rotated to landscape
// and will also exit fullscreen when the device is rotated back to portrait.
viewBinding.theoPlayerView.getFullScreenManager().setFullScreenOrientationCoupled(true);
theoPlayer.setAutoplay(true);
// Configuring THEOplayer with defined SourceDescription object.
theoPlayer.setSource(SourceManager.Companion.getELEPHANTS_DREAM_HLS_WITH_CAST_METADATA());
theoPlayer.play();
// Adding listeners to THEOplayer basic playback events.
theoPlayer.addEventListener(PlayerEventTypes.PLAY, event -> Log.i(TAG, "Event: PLAY"));
theoPlayer.addEventListener(PlayerEventTypes.PLAYING, event -> Log.i(TAG, "Event: PLAYING"));
theoPlayer.addEventListener(PlayerEventTypes.PAUSE, event -> Log.i(TAG, "Event: PAUSE"));
theoPlayer.addEventListener(PlayerEventTypes.ENDED, event -> Log.i(TAG, "Event: ENDED"));
theoPlayer.addEventListener(PlayerEventTypes.ERROR, event -> Log.i(TAG, "Event: ERROR, error=" + event.getErrorObject().getMessage()));
}
private void configureChromecast() {
// Adding listeners to THEOplayer cast events.
theoChromecast.addEventListener(ChromecastEventTypes.STATECHANGE, event -> Log.i(TAG, "Event: CAST_STATECHANGE, state=" + event.getState()));
theoChromecast.addEventListener(ChromecastEventTypes.ERROR, event -> Log.i(TAG, "Event: CAST_ERROR, error=" + event.getError()));
// Some applications that do not require to have MediaRouteButton to control the connection
// with the Cast Receiver device can use the below APIs instead.
// theoChromecast.start();
// theoChromecast.stop();
// theoChromecast.join();
// theoChromecast.leave();
// Some streaming setups requires casting a different stream to a Cast Receiver device
// than the one playing on a Cast Sender device, e.g. different DRM capabilities.
// Code below shows how to configure such different stream to cast.
theoChromecast.setConnectionCallback(new ChromecastConnectionCallback() {
/**
* Called after the player has started the connection to the receiver.
*
* - At this point we are trying to load the media from the sender to the receiver.
* - Returning null will behave same as returning the provided SourceDescription.
*
* @param sourceDescription The current SourceDescription on the sender device. (<b>Nullable</b>)
* @return The SourceDescription to be loaded on the receiver device. (<b>Nullable</b>)
*/
@Nullable
@Override
public SourceDescription onStart(@Nullable SourceDescription sourceDescription) {
return null;
}
/**
* Called after the player has stopped the connection to the receiver.
*
* - At this point we are trying to load the media from the receiver to the sender.
* - Returning null will behave same as returning the provided SourceDescription.
*
* @param sourceDescription The current SourceDescription on the receiver device. (<b>Nullable</b>)
* @return The SourceDescription to be loaded on the sender device. (<b>Nullable</b>)
*/
@Nullable
@Override
public SourceDescription onStop(@Nullable SourceDescription sourceDescription) {
return null;
}
/**
* Called after the player has joined an already existing connection to the receiver.
*
* - At this point it's possible to load a new media from the sender to the receiver.
* - Returning null will not change the source on the receiver.
*
* @param sourceDescription The current SourceDescription on the current sender device. (<b>Nullable</b>)
* @return The SourceDescription to be loaded on the receiver device. (<b>Nullable</b>)
*/
@Nullable
@Override
public SourceDescription onJoin(@Nullable SourceDescription sourceDescription) {
return null;
}
/**
* Called after the player has left the connection to the receiver.
*
* - At this point we are trying to load the media from the receiver to the sender.
* - Returning null will behave same as returning the provided SourceDescription.
*
* @param sourceDescription The current SourceDescription on the receiver device. (<b>Nullable</b>)
* @return The SourceDescription to be loaded on the sender device. (<b>Nullable</b>)
*/
@Nullable
@Override
public SourceDescription onLeave(@Nullable SourceDescription sourceDescription) {
return null;
}
});
}
// In order to work properly and in sync with the activity lifecycle changes (e.g. device
// is rotated, new activity is started or app is moved to background) we need to call
// the "onResume", "onPause" and "onDestroy" methods of the THEOplayerView when the matching
// activity methods are called.
@Override
protected void onPause() {
super.onPause();
viewBinding.theoPlayerView.onPause();
}
@Override
protected void onResume() {
super.onResume();
viewBinding.theoPlayerView.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
viewBinding.theoPlayerView.onDestroy();
}
}