Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rn-0.31+ events, local assets support #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ protected ReactMediaPlayerView createViewInstance(ThemedReactContext reactContex

@ReactProp(name = "src")
public void setSrc(ReactMediaPlayerView view, @Nullable String uri) {
if (!uri.startsWith("http")) {
uri = "asset:///" + uri; // app/src/main/assets/<filepath>
}
Log.d(TAG, "setSrc...src=" + uri);
view.setUri(uri);
}
Expand Down Expand Up @@ -92,7 +95,7 @@ protected void addEventEmitters(final ThemedReactContext reactContext, final Rea
@Override
public void onPlayerPlaying() {
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()
.dispatchEvent(new Event(view.getId(), SystemClock.uptimeMillis()) {
.dispatchEvent(new Event(view.getId()) {
@Override
public String getEventName() {
return EVENT_ON_PLAYER_PLAYING;
Expand All @@ -108,7 +111,7 @@ public void dispatch(RCTEventEmitter rctEventEmitter) {
@Override
public void onPlayerPaused() {
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()
.dispatchEvent(new Event(view.getId(), SystemClock.uptimeMillis()) {
.dispatchEvent(new Event(view.getId()) {
@Override
public String getEventName() {
return EVENT_ON_PLAYER_PAUSED;
Expand All @@ -124,7 +127,7 @@ public void dispatch(RCTEventEmitter rctEventEmitter) {
@Override
public void onPlayerFinished() {
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()
.dispatchEvent(new Event(view.getId(), SystemClock.uptimeMillis()) {
.dispatchEvent(new Event(view.getId()) {
@Override
public String getEventName() {
return EVENT_ON_PLAYER_FINISHED;
Expand All @@ -140,7 +143,7 @@ public void dispatch(RCTEventEmitter rctEventEmitter) {
@Override
public void onPlayerBuffering() {
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()
.dispatchEvent(new Event(view.getId(), SystemClock.uptimeMillis()) {
.dispatchEvent(new Event(view.getId()) {
@Override
public String getEventName() {
return EVENT_ON_PLAYER_BUFFERING;
Expand All @@ -156,7 +159,7 @@ public void dispatch(RCTEventEmitter rctEventEmitter) {
@Override
public void onPlayerBufferReady() {
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()
.dispatchEvent(new Event(view.getId(), SystemClock.uptimeMillis()) {
.dispatchEvent(new Event(view.getId()) {
@Override
public String getEventName() {
return EVENT_ON_PLAYER_BUFFER_OK;
Expand All @@ -172,7 +175,7 @@ public void dispatch(RCTEventEmitter rctEventEmitter) {
@Override
public void onPlayerProgress(final long current, final long total, final long buffered) {
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()
.dispatchEvent(new Event(view.getId(), SystemClock.uptimeMillis()) {
.dispatchEvent(new Event(view.getId()) {
@Override
public String getEventName() {
return EVENT_ON_PLAYER_PROGRESS;
Expand All @@ -190,7 +193,7 @@ public void dispatch(RCTEventEmitter rctEventEmitter) {
if (buffered > 0 && this.buffered != buffered) {
this.buffered = buffered;
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher()
.dispatchEvent(new Event(view.getId(), SystemClock.uptimeMillis()) {
.dispatchEvent(new Event(view.getId()) {
@Override
public String getEventName() {
return EVENT_ON_PLAYER_BUFFER_CHANGE;
Expand Down
14 changes: 13 additions & 1 deletion ios/react-native-media-kit/RCTMediaPlayerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,21 @@ - (void)willMoveToSuperview:(UIView *)newSuperview {
}
}


- (void)initPlayerIfNeeded {
if(!player) {
player = [AVPlayer playerWithURL:[NSURL URLWithString:self.src]];
NSURL *url;
NSString *httpPrefix = @"http";
if ([self.src hasPrefix:httpPrefix]) {
url = [NSURL URLWithString:self.src];
} else {
NSString *resourceType = [self.src pathExtension];
NSString *resource = [[self.src lastPathComponent] stringByDeletingPathExtension];
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *file = [mainBundle pathForResource:resource ofType:resourceType];
url = [NSURL fileURLWithPath:file];
}
player = [AVPlayer playerWithURL:url];
[self setPlayer:player];
[self addProgressObserver];
[self addObservers];
Expand Down