Skip to content

Commit 062136e

Browse files
committed
iOS - events implemented
1 parent 6031fbe commit 062136e

File tree

10 files changed

+175
-4
lines changed

10 files changed

+175
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
## Progress
99

10-
- [x] ios (implemented %80 - onReady, didChangeTo, onChangeState, onChangeFullscreen not implemented yet)
10+
- [x] ios (implemented %99)
1111
- [x] android (implemented 'com.pierfrancescosoffritti.androidyoutubeplayer')
1212

1313
## Note

example/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ export default class App extends React.Component {
1010
<YouTubePlayer
1111
ref={ref => (this.youTubePlayer = ref)}
1212
videoId="6Ofd2G89qJY"
13-
autoPlay={false}
13+
autoPlay={true}
1414
fullscreen={false}
1515
showFullScreenButton={false}
16-
showSeekBar={false}
16+
showSeekBar={true}
1717
showPlayPauseButton={true}
1818
startTime={5}
1919
style={{ width: "100%", height: 200 }}

ios/Media.xcassets/Contents.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"version" : 1,
4+
"author" : "xcode"
5+
}
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"info" : {
3+
"version" : 1,
4+
"author" : "xcode"
5+
},
6+
"data" : [
7+
{
8+
"idiom" : "universal",
9+
"filename" : "YTPlayer.html"
10+
}
11+
]
12+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!--
2+
Copyright 2014 Google Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<!DOCTYPE html>
17+
<html>
18+
<head>
19+
<style>
20+
* { margin: 0; padding: 0; }
21+
html, body { width: 100%; height: 100%; }
22+
</style>
23+
</head>
24+
<body>
25+
<div id="player"></div>
26+
<script src="https://www.youtube.com/iframe_api"></script>
27+
<script>
28+
var player;
29+
YT.ready(function() {
30+
player = new YT.Player('player', %@);
31+
window.location.href = 'ytplayer://onYouTubeIframeAPIReady';
32+
});
33+
function onReady(event) {
34+
window.location.href = 'ytplayer://onReady?data=' + event.data;
35+
}
36+
37+
function onStateChange(event) {
38+
window.location.href = 'ytplayer://onStateChange?data=' + event.data;
39+
}
40+
41+
function onPlaybackQualityChange(event) {
42+
window.location.href = 'ytplayer://onPlaybackQualityChange?data=' + event.data;
43+
}
44+
function onPlayerError(event) {
45+
window.location.href = 'ytplayer://onError?data=' + event.data;
46+
}
47+
</script>
48+
</body>
49+
</html>

ios/YTPlayer.html

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!--
2+
Copyright 2014 Google Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<!DOCTYPE html>
17+
<html>
18+
<head>
19+
<style>
20+
* { margin: 0; padding: 0; }
21+
html, body { width: 100%; height: 100%; }
22+
</style>
23+
</head>
24+
<body>
25+
<div id="player"></div>
26+
<script src="https://www.youtube.com/iframe_api"></script>
27+
<script>
28+
var player;
29+
YT.ready(function() {
30+
player = new YT.Player('player', %@);
31+
window.location.href = 'ytplayer://onYouTubeIframeAPIReady';
32+
});
33+
function onReady(event) {
34+
window.location.href = 'ytplayer://onReady?data=' + event.data;
35+
}
36+
37+
function onStateChange(event) {
38+
window.location.href = 'ytplayer://onStateChange?data=' + event.data;
39+
}
40+
41+
function onPlaybackQualityChange(event) {
42+
window.location.href = 'ytplayer://onPlaybackQualityChange?data=' + event.data;
43+
}
44+
function onPlayerError(event) {
45+
window.location.href = 'ytplayer://onError?data=' + event.data;
46+
}
47+
</script>
48+
</body>
49+
</html>

ios/YTPlayerView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ open class YTPlayerView: UIView {
916916
}
917917
}
918918

919+
919920
/**
920921
* Private helper method to load an iframe player with the given player parameters.
921922
*
@@ -960,6 +961,8 @@ open class YTPlayerView: UIView {
960961

961962
let embedHTMLTemplate: String!
962963

964+
965+
963966
if let url = NSURL(string: "https://charmy.app/YTPlayer.html") {
964967

965968
do {

ios/YouTubeSdk.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
AA21F29022A10823003D37F8 /* YTPlayerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YTPlayerView.swift; sourceTree = "<group>"; };
3737
AA21F29922A13D54003D37F8 /* YouTubeSdk.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YouTubeSdk.swift; sourceTree = "<group>"; };
3838
AA21F29B22A1D18F003D37F8 /* YouTubeSdk.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = YouTubeSdk.m; sourceTree = "<group>"; };
39+
AAE6AA9122A259800057733E /* YTPlayer.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = YTPlayer.html; sourceTree = "<group>"; };
40+
AAE6AA9322A25A3B0057733E /* Media.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Media.xcassets; sourceTree = "<group>"; };
3941
/* End PBXFileReference section */
4042

4143
/* Begin PBXFrameworksBuildPhase section */
@@ -60,6 +62,8 @@
6062
58B511D21A9E6C8500147676 = {
6163
isa = PBXGroup;
6264
children = (
65+
AAE6AA9322A25A3B0057733E /* Media.xcassets */,
66+
AAE6AA9122A259800057733E /* YTPlayer.html */,
6367
AA21F29B22A1D18F003D37F8 /* YouTubeSdk.m */,
6468
AA21F29922A13D54003D37F8 /* YouTubeSdk.swift */,
6569
AA21F25722A100D4003D37F8 /* YouTubeView.swift */,

ios/YouTubeView.swift

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import UIKit
1212
@objc class YouTubeView: UIView {
1313

1414
@objc var onError: RCTDirectEventBlock?
15+
@objc var onReady: RCTDirectEventBlock?
16+
@objc var onChangeState: RCTDirectEventBlock?
17+
@objc var onChangeFullscreen: RCTDirectEventBlock?
1518

1619
@objc var autoPlay: Bool = false;
1720

@@ -52,6 +55,18 @@ import UIKit
5255
override init(frame: CGRect) {
5356
super.init(frame: frame)
5457
self.addSubview(player)
58+
59+
// listen for videos playing in fullscreen
60+
NotificationCenter.default.addObserver(self, selector: #selector(onDidEnterFullscreen(_:)), name: UIWindow.didBecomeVisibleNotification, object: self.window)
61+
62+
// listen for videos stopping to play in fullscreen
63+
NotificationCenter.default.addObserver(self, selector: #selector(onDidLeaveFullscreen(_:)), name: UIWindow.didBecomeHiddenNotification, object: self.window)
64+
}
65+
66+
deinit{
67+
// remove video listeners
68+
NotificationCenter.default.removeObserver(self, name: UIWindow.didBecomeVisibleNotification, object: self.window)
69+
NotificationCenter.default.removeObserver(self, name: UIWindow.didBecomeHiddenNotification, object: self.window)
5570
}
5671

5772
@objc func reactSetFrame(frame:CGRect) {
@@ -101,17 +116,46 @@ import UIKit
101116
@objc func getVideoDuration() -> NSInteger{
102117
return NSInteger(player.duration)
103118
}
119+
120+
@objc func onDidEnterFullscreen(_ notification: Notification) {
121+
print("video is now playing in fullscreen")
122+
onChangeFullscreen!(["isFullscreen" : true])
123+
}
124+
125+
@objc func onDidLeaveFullscreen(_ notification: Notification) {
126+
print("video has stopped playing in fullscreen")
127+
onChangeFullscreen!(["isFullscreen" : false])
128+
}
104129
}
105130

106131
extension YouTubeView: YTPlayerViewDelegate{
107132
func playerViewDidBecomeReady(_ playerView: YTPlayerView) {
133+
//player is ready to go
134+
onReady!(["type" : "ready"])
108135
if autoPlay{
109136
playerView.playVideo()
110137
}
111138
}
112139

113140
func playerView(_ playerView: YTPlayerView, didChangeTo state: YTPlayerState){
114-
print(state)
141+
onChangeState!(
142+
["state" :
143+
{
144+
switch state {
145+
case .unstarted: return "unstarted"
146+
case .ended: return "ended"
147+
case .playing: return "playing"
148+
case .paused: return"paused"
149+
case .buffering: return "buffering"
150+
case .queued: return "queued"
151+
case .unknown: return "unknown"
152+
}
153+
}()
154+
])
155+
}
156+
157+
func playerView(_ playerView: YTPlayerView, receivedError error: YTPlayerError) {
158+
onError!(["error" : error.rawValue])
115159
}
116160

117161
}

ios/YouTubeViewManager.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ @interface RCT_EXTERN_REMAP_MODULE(YouTubeView,YouTubeViewManager, RCTViewManage
1717
RCT_EXPORT_VIEW_PROPERTY(fullscreen, BOOL)
1818
RCT_EXPORT_VIEW_PROPERTY(videoId, NSString)
1919

20+
RCT_EXPORT_VIEW_PROPERTY(onReady, RCTDirectEventBlock)
2021
RCT_EXPORT_VIEW_PROPERTY(onError, RCTDirectEventBlock)
22+
RCT_EXPORT_VIEW_PROPERTY(onChangeState, RCTDirectEventBlock)
23+
RCT_EXPORT_VIEW_PROPERTY(onChangeFullscreen, RCTDirectEventBlock)
24+
2125

2226
RCT_EXTERN_METHOD(play:(nonnull NSNumber *)node)
2327
RCT_EXTERN_METHOD(pause:(nonnull NSNumber *)node)

0 commit comments

Comments
 (0)