-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathYouTubeView.java
More file actions
204 lines (164 loc) · 6.38 KB
/
Copy pathYouTubeView.java
File metadata and controls
204 lines (164 loc) · 6.38 KB
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
package com.inprogress.reactnativeyoutube;
import android.app.FragmentManager;
import android.os.Build;
import android.os.Parcelable;
import android.widget.FrameLayout;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;
public class YouTubeView extends FrameLayout {
private YouTubePlayerController mYouTubeController;
private VideoFragment mVideoFragment;
private boolean mHasSavedInstance = false;
public YouTubeView(ReactContext context) {
super(context);
init();
}
public ReactContext getReactContext() {
return (ReactContext) getContext();
}
public void init() {
inflate(getContext(), R.layout.youtube_layout, this);
mVideoFragment = VideoFragment.newInstance(this);
mYouTubeController = new YouTubePlayerController(this);
}
@Nullable
@Override
protected Parcelable onSaveInstanceState() {
mHasSavedInstance = true;
return super.onSaveInstanceState();
}
@Override
protected void onAttachedToWindow() {
if (!mHasSavedInstance) {
FragmentManager fragmentManager = getReactContext().getCurrentActivity().getFragmentManager();
fragmentManager.beginTransaction().add(getId(), mVideoFragment).commit();
}
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
if (getReactContext().getCurrentActivity() != null) {
FragmentManager fragmentManager = getReactContext().getCurrentActivity().getFragmentManager();
// Code crashes with java.lang.IllegalStateException: Activity has been destroyed
// if our activity has been destroyed when this runs
if (mVideoFragment != null) {
boolean isDestroyed = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
isDestroyed = getReactContext().getCurrentActivity().isDestroyed();
}
if (!isDestroyed) {
// https://stackoverflow.com/a/34508430/61072
fragmentManager.beginTransaction().remove(mVideoFragment).commitAllowingStateLoss();
}
}
}
super.onDetachedFromWindow();
}
public void seekTo(int second) {
mYouTubeController.seekTo(second);
}
public int getCurrentTime() {
return mYouTubeController.getCurrentTime();
}
public int getDuration() {
return mYouTubeController.getDuration();
}
public void nextVideo() {
mYouTubeController.nextVideo();
}
public void previousVideo() {
mYouTubeController.previousVideo();
}
public void playVideoAt(int index) {
mYouTubeController.playVideoAt(index);
}
public int getVideosIndex() {
return mYouTubeController.getVideosIndex();
}
public void onVideoFragmentResume() {
mYouTubeController.onVideoFragmentResume();
}
public void receivedError(String param) {
WritableMap event = Arguments.createMap();
ReactContext reactContext = getReactContext();
event.putString("error", param);
event.putInt("target", getId());
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "error", event);
}
public void playerViewDidBecomeReady() {
WritableMap event = Arguments.createMap();
ReactContext reactContext = getReactContext();
event.putInt("target", getId());
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "ready", event);
}
public void didChangeToSeeking(int milliSeconds) {
WritableMap event = Arguments.createMap();
event.putString("state", "seeking");
event.putInt("currentTime", milliSeconds / 1000);
event.putInt("target", getId());
ReactContext reactContext = getReactContext();
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "state", event);
}
public void didChangeToState(String param) {
WritableMap event = Arguments.createMap();
event.putString("state", param);
event.putInt("target", getId());
ReactContext reactContext = getReactContext();
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "state", event);
}
public void didChangeToQuality(String param) {
WritableMap event = Arguments.createMap();
event.putString("quality", param);
event.putInt("target", getId());
ReactContext reactContext = getReactContext();
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "quality", event);
}
public void didChangeToFullscreen(boolean isFullscreen) {
WritableMap event = Arguments.createMap();
ReactContext reactContext = getReactContext();
event.putBoolean("isFullscreen", isFullscreen);
event.putInt("target", getId());
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "fullscreen", event);
}
public void setApiKey(String apiKey) {
try {
mVideoFragment.initialize(apiKey, mYouTubeController);
} catch (Exception e) {
receivedError(e.getMessage());
}
}
public void setVideoId(String str) {
mYouTubeController.setVideoId(str);
}
public void setVideoIds(ReadableArray arr) {
mYouTubeController.setVideoIds(arr);
}
public void setPlaylistId(String str) {
mYouTubeController.setPlaylistId(str);
}
public void setPlay(boolean bool) {
mYouTubeController.setPlay(bool);
}
public void setLoop(boolean bool) {
mYouTubeController.setLoop(bool);
}
public void setFullscreen(boolean bool) {
mYouTubeController.setFullscreen(bool);
}
public void setFullscreenControlFlags(int flags) {
mYouTubeController.setFullscreenControlFlags(flags);
}
public void setControls(int nb) {
mYouTubeController.setControls(nb);
}
public void setShowFullscreenButton(boolean bool) {
mYouTubeController.setShowFullscreenButton(bool);
}
public void setResumePlay(boolean bool) {
mYouTubeController.setResumePlay(bool);
}
}