Skip to content

Commit

Permalink
Merge pull request #227 from cornejobarraza/fix/autoplay-ios
Browse files Browse the repository at this point in the history
Add autoplay on iOS
  • Loading branch information
razorRun authored Jan 15, 2025
2 parents 7347285 + c2a9ae9 commit d906739
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 59 deletions.
1 change: 1 addition & 0 deletions VLCPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ VLCPlayer.propTypes = {
/* Wrapper component */
source: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
subtitleUri: PropTypes.string,
autoplay: PropTypes.bool,

onError: PropTypes.func,
onProgress: PropTypes.func,
Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ export type VLCPlayerProps = VLCPlayerCallbackProps & {
* React native view stylesheet styles
*/
style?: StyleProp<ViewStyle>;

/**
* Enables autoplay
*
* @default true
*/
autoplay?: boolean;
};

/**
Expand Down
86 changes: 27 additions & 59 deletions ios/RCTVLCPlayer/RCTVLCPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ @implementation RCTVLCPlayer
NSString * _subtitleUri;

NSDictionary * _videoInfo;

BOOL _autoplay;
}

- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
Expand Down Expand Up @@ -75,6 +75,11 @@ - (void)applyModifiers
[self play];
}

- (void)setAutoplay:(BOOL)autoplay
{
_autoplay = autoplay;
}

- (void)setPaused:(BOOL)paused
{
if(_player){
Expand All @@ -97,67 +102,28 @@ -(void)play
}
}

-(void)setResume:(BOOL)autoplay
{
if(_player){
[self _release];
}
// [bavv edit start]
NSString* uri = [_source objectForKey:@"uri"];
NSURL* _uri = [NSURL URLWithString:uri];
NSDictionary* initOptions = [_source objectForKey:@"initOptions"];

_player = [[VLCMediaPlayer alloc] init];
// [bavv edit end]

[_player setDrawable:self];
_player.delegate = self;
_player.scaleFactor = 0;
VLCMedia *media = [VLCMedia mediaWithURL:_uri];

for (NSString* option in initOptions) {
[media addOption:[option stringByReplacingOccurrencesOfString:@"--" withString:@""]];
}

_player.media = media;
_player.media.delegate = self;
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
NSLog(@"autoplay: %i",autoplay);
self.onVideoLoadStart(@{
@"target": self.reactTag
});

}

-(void)setSource:(NSDictionary *)source
{
if(_player){
[self _release];
}
_source = source;
_videoInfo = nil;

// [bavv edit start]
NSString* uri = [source objectForKey:@"uri"];
BOOL autoplay = [RCTConvert BOOL:[source objectForKey:@"autoplay"]];
NSString* uri = [_source objectForKey:@"uri"];
NSURL* _uri = [NSURL URLWithString:uri];
NSDictionary* initOptions = [source objectForKey:@"initOptions"];

_player = [[VLCMediaPlayer alloc] init];
// [bavv edit end]

[_player setDrawable:self];
_player.delegate = self;
_player.scaleFactor = 0;

VLCMedia *media = [VLCMedia mediaWithURL:_uri];
int initType = [_source objectForKey:@"initType"];
NSDictionary* initOptions = [_source objectForKey:@"initOptions"];

for (NSString* option in initOptions) {
[media addOption:[option stringByReplacingOccurrencesOfString:@"--" withString:@""]];
if(initType == 1) {
_player = [[VLCMediaPlayer alloc] init];
}else {
_player = [[VLCMediaPlayer alloc] initWithOptions:initOptions];
}
_player.delegate = self;
_player.drawable = self;
// [bavv edit end]

_player.media = media;
_player.media.delegate = self;
_player.media = [VLCMedia mediaWithURL:_uri];

[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
NSLog(@"autoplay: %i",autoplay);
self.onVideoLoadStart(@{
Expand All @@ -167,8 +133,8 @@ -(void)setSource:(NSDictionary *)source
[_player addPlaybackSlave:_subtitleUri type:VLCMediaPlaybackSlaveTypeSubtitle enforce:YES];
}

// if(autoplay)
[self play];
if(_autoplay)
[_player play];
}

- (void)setSubtitleUri:(NSString *)subtitleUri
Expand Down Expand Up @@ -389,13 +355,15 @@ - (void)setMuted:(BOOL)value

- (void)_release
{
if(_player){
[_player pause];
[[NSNotificationCenter defaultCenter] removeObserver:self];

if(_player.media)
[_player stop];

if (_player)
_player = nil;
_eventDispatcher = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

_eventDispatcher = nil;
}


Expand Down
1 change: 1 addition & 0 deletions ios/RCTVLCPlayer/RCTVLCPlayerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ - (dispatch_queue_t)methodQueue
};
RCT_EXPORT_VIEW_PROPERTY(audioTrack, int);
RCT_EXPORT_VIEW_PROPERTY(textTrack, int);
RCT_EXPORT_VIEW_PROPERTY(autoplay, BOOL);

@end

0 comments on commit d906739

Please sign in to comment.