This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
forked from TheWidlarzGroup/react-native-video
-
Notifications
You must be signed in to change notification settings - Fork 4
/
RCTAVPlayerManager.m
150 lines (128 loc) · 3.91 KB
/
RCTAVPlayerManager.m
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
#import "RCTAVPlayerManager.h"
#import "RCTAVPlayer.h"
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import <AVFoundation/AVFoundation.h>
@implementation RCTAVPlayerManager
RCT_EXPORT_MODULE(AVPlayer);
@synthesize bridge;
/* Should support: onLoadStart, onLoad, and onError to stay consistent with Image */
- (NSArray *)customDirectEventTypes
{
return @[
@"onVideoLoadStart",
@"onVideoLoad",
@"onVideoError",
@"onVideoProgress",
@"onVideoSeek",
@"onVideoEnd"
];
}
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
static NSMutableDictionary* _players;
RCT_EXPORT_METHOD(createVideoPlayer:(NSString*)uuid)
{
if (_players == nil)
_players = [NSMutableDictionary dictionary];
RCTAVPlayer* player = [[RCTAVPlayer alloc] initWithEventDispatcher:self.bridge.eventDispatcher];
player.uuid = uuid;
[_players setObject:player forKey:player.uuid];
}
RCT_EXPORT_METHOD(setSource:(NSString*)playerUuid withSource:(NSDictionary*)source withCallback:(RCTResponseSenderBlock)callback)
{
if (_players == nil || !_players[playerUuid])
{
callback(@[@"ERROR: Player with uuid not found!"]);
return;
}
RCTAVPlayer* player = _players[playerUuid];
[player setSrc:source];
callback(@[[NSNull null], player.uuid]);
}
RCT_EXPORT_METHOD(setRepeat:(NSString*)playerUuid withRepeat:(BOOL)repeat withCallback:(RCTResponseSenderBlock)callback)
{
if (_players == nil || !_players[playerUuid])
{
callback(@[@"ERROR: Player with uuid not found!"]);
return;
}
RCTAVPlayer* player = _players[playerUuid];
[player setRepeat:repeat];
callback(@[[NSNull null], player.uuid]);
}
RCT_EXPORT_METHOD(setMuted:(NSString*)playerUuid withMuted:(BOOL)muted withCallback:(RCTResponseSenderBlock)callback)
{
if (_players == nil || !_players[playerUuid])
{
callback(@[@"ERROR: Player with uuid not found!"]);
return;
}
RCTAVPlayer* player = _players[playerUuid];
[player setMuted:muted];
callback(@[[NSNull null], player.uuid]);
}
RCT_EXPORT_METHOD(setVolume:(NSString*)playerUuid withVolume:(float)volume withCallback:(RCTResponseSenderBlock)callback)
{
if (_players == nil || !_players[playerUuid])
{
callback(@[@"ERROR: Player with uuid not found!"]);
return;
}
RCTAVPlayer* player = _players[playerUuid];
[player setVolume:volume];
callback(@[[NSNull null], player.uuid]);
}
RCT_EXPORT_METHOD(setRate:(NSString*)playerUuid withRate:(float)rate withCallback:(RCTResponseSenderBlock)callback)
{
if (_players == nil || !_players[playerUuid])
{
callback(@[@"ERROR: Player with uuid not found!"]);
return;
}
RCTAVPlayer* player = _players[playerUuid];
[player setRate:rate];
callback(@[[NSNull null], player.uuid]);
}
RCT_EXPORT_METHOD(setSeek:(NSString*)playerUuid withSeek:(float)seek withCallback:(RCTResponseSenderBlock)callback)
{
if (_players == nil || !_players[playerUuid])
{
callback(@[@"ERROR: Player with uuid not found!"]);
return;
}
RCTAVPlayer* player = _players[playerUuid];
[player setSeek:seek];
callback(@[[NSNull null], player.uuid]);
}
RCT_EXPORT_METHOD(removePlayer:(NSString*)playerUuid withCallback:(RCTResponseSenderBlock)callback)
{
if (_players == nil || !_players[playerUuid])
{
callback(@[@"ERROR: Player with uuid not found!"]);
return;
}
[_players[playerUuid] invalidate];
[_players removeObjectForKey:playerUuid];
callback(@[[NSNull null], playerUuid]);
}
+(RCTAVPlayer*)getPlayer:(NSString *)playerUuid
{
if (_players == nil || !_players[playerUuid])
{
NSLog(@"Failed getting player with uuid!");
return nil;
}
return _players[playerUuid];
}
-(void)invalidate
{
for (NSString* playerUuid in _players)
{
[_players[playerUuid] invalidate];
}
_players = nil;
}
@end