Skip to content

Commit

Permalink
fix: android context null pointer error
Browse files Browse the repository at this point in the history
fixes #272
  • Loading branch information
befovy committed Jul 5, 2020
1 parent 051c5a4 commit 9f80359
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
5 changes: 4 additions & 1 deletion android/src/main/java/com/befovy/fijkplayer/FijkEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@

interface FijkEngine {

@Nullable
TextureRegistry.SurfaceTextureEntry createSurfaceEntry();

@Nullable
BinaryMessenger messenger();

@Nullable
Context context();

@Nullable
String lookupKeyForAsset(@NonNull String asset, @Nullable String packageName);


void onPlayingChange(int delta);

void onPlayableChange(int delta);
Expand Down
32 changes: 23 additions & 9 deletions android/src/main/java/com/befovy/fijkplayer/FijkPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package com.befovy.fijkplayer;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.SurfaceTexture;
Expand Down Expand Up @@ -131,7 +132,7 @@ int getPlayerId() {
void setup() {
if (mJustSurface)
return;
if (mHostOptions.getIntOption(HostOption.ENABLE_SNAPSHOT,0) > 0) {
if (mHostOptions.getIntOption(HostOption.ENABLE_SNAPSHOT, 0) > 0) {
mIjkMediaPlayer.setAmcGlesRender();
mIjkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "overlay-format", "fcc-_es2");
}
Expand All @@ -142,13 +143,20 @@ long setupSurface() {
if (mSurfaceTextureEntry == null) {
TextureRegistry.SurfaceTextureEntry surfaceTextureEntry = mEngine.createSurfaceEntry();
mSurfaceTextureEntry = surfaceTextureEntry;
mSurfaceTexture = surfaceTextureEntry.surfaceTexture();
mSurface = new Surface(mSurfaceTexture);
if (surfaceTextureEntry != null) {
mSurfaceTexture = surfaceTextureEntry.surfaceTexture();
mSurface = new Surface(mSurfaceTexture);
}
if (!mJustSurface) {
mIjkMediaPlayer.setSurface(mSurface);
}
}
return mSurfaceTextureEntry.id();
if (mSurfaceTextureEntry != null)
return mSurfaceTextureEntry.id();
else {
Log.e("FIJKPLAYER", "setup surface, null SurfaceTextureEntry");
return 0;
}
}

void release() {
Expand Down Expand Up @@ -332,7 +340,7 @@ private void applyOptions(Object options) {
for (Object o : optionsMap.keySet()) {
Object option = optionsMap.get(o);
if (o instanceof Integer && option instanceof Map) {
Integer cat = (Integer) o;
int cat = (Integer) o;
Map optionMap = (Map) option;
for (Object key : optionMap.keySet()) {
Object value = optionMap.get(key);
Expand Down Expand Up @@ -404,20 +412,26 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
}
}
try {
if (openAsset) {
AssetManager assetManager = mEngine.context().getAssets();
Context context = mEngine.context();
if (openAsset && context != null) {
AssetManager assetManager = context.getAssets();
InputStream is = assetManager.open(uri.getPath() != null ? uri.getPath() : "", AssetManager.ACCESS_RANDOM);
mIjkMediaPlayer.setDataSource(new RawMediaDataSource(is));
} else {
} else if (context != null){
if (TextUtils.isEmpty(uri.getScheme()) || "file".equals(uri.getScheme())) {
String path = uri.getPath() != null ? uri.getPath() : "";
IMediaDataSource dataSource = new FileMediaDataSource(new File(path));
mIjkMediaPlayer.setDataSource(dataSource);
} else {
mIjkMediaPlayer.setDataSource(mEngine.context(), uri);
}
} else {
Log.e("FIJKPLAYER", "context null, can't setDataSource");
}
handleEvent(PLAYBACK_STATE_CHANGED, initialized, -1, null);
if (context == null) {
handleEvent(PLAYBACK_STATE_CHANGED, error, -1, null);
}
result.success(null);
} catch (FileNotFoundException e) {
result.error("-875574348", "Local File not found:" + e.getMessage(), null);
Expand Down Expand Up @@ -468,7 +482,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
} else if (call.method.equals("snapshot")) {
if (mHostOptions.getIntOption(HostOption.ENABLE_SNAPSHOT, 0) > 0) {
mIjkMediaPlayer.snapShot();
} else {
} else {
mMethodChannel.invokeMethod("_onSnapshot", "not support");
}
result.success(null);
Expand Down
37 changes: 22 additions & 15 deletions android/src/main/java/com/befovy/fijkplayer/FijkPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ public void onDetachedFromActivityForConfigChanges() {
@Override
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
mActivity = new WeakReference<>(binding.getActivity());

if (mActivity.get() instanceof FijkVolume.CanListenVolumeKey) {
FijkVolume.CanListenVolumeKey canListenVolumeKey = (FijkVolume.CanListenVolumeKey) mActivity.get();
canListenVolumeKey.setVolumeKeyListener(this);
Expand All @@ -164,6 +163,7 @@ public void onDetachedFromActivity() {
}

@Override
@Nullable
public TextureRegistry.SurfaceTextureEntry createSurfaceEntry() {
if (mBinding != null) {
return mBinding.getTextureRegistry().createSurfaceTexture();
Expand All @@ -174,6 +174,7 @@ public TextureRegistry.SurfaceTextureEntry createSurfaceEntry() {
}

@Override
@Nullable
public BinaryMessenger messenger() {
if (mBinding != null) {
return mBinding.getBinaryMessenger();
Expand All @@ -184,19 +185,27 @@ public BinaryMessenger messenger() {
}

@Override
@Nullable
public Context context() {
return mContext.get();
if (mContext != null)
return mContext.get();
else
return null;
}

@Nullable
private Activity activity() {
if (mRegistrar != null) {
return mRegistrar.activity();
} else {
} else if (mActivity != null) {
return mActivity.get();
} else {
return null;
}
}

@Override
@Nullable
public String lookupKeyForAsset(@NonNull String asset, @Nullable String packageName) {
String path = null;
if (mBinding != null) {
Expand Down Expand Up @@ -262,6 +271,7 @@ public void onCancel(Object o) {

@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
Activity activity;
switch (call.method) {
case "getPlatformVersion":
result.success("Android " + android.os.Build.VERSION.RELEASE);
Expand Down Expand Up @@ -305,8 +315,8 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
}
case "setOrientationPortrait":
boolean changedPort = false;
if (activity() != null) {
final Activity activity = activity();
activity = activity();
if (activity != null) {
int current_orientation = activity.getResources().getConfiguration().orientation;
if (current_orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
Expand All @@ -321,8 +331,8 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
break;
case "setOrientationLandscape":
boolean changedLand = false;
if (activity() != null) {
final Activity activity = activity();
activity = activity();
if (activity != null) {
int current_orientation = activity.getResources().getConfiguration().orientation;
if (current_orientation == Configuration.ORIENTATION_PORTRAIT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
Expand All @@ -336,8 +346,8 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
result.success(changedLand);
break;
case "setOrientationAuto":
if (activity() != null) {
final Activity activity = activity();
activity = activity();
if (activity != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_USER);
} else {
Expand Down Expand Up @@ -481,7 +491,6 @@ public void setScreenOn(boolean on) {
*/
private boolean isScreenKeptOn() {
Activity activity = activity();

if (activity == null || activity.getWindow() == null)
return false;
int flag = activity.getWindow().getAttributes().flags;
Expand All @@ -491,12 +500,11 @@ private boolean isScreenKeptOn() {

private float getScreenBrightness() {
Activity activity = activity();

if (activity == null || activity.getWindow() == null)
return 0;
float brightness = activity.getWindow().getAttributes().screenBrightness;
if (brightness < 0) {
Context context = mContext.get();
Context context = context();
Log.w("FIJKPLAYER", "window attribute brightness less than 0");
try {
if (context != null) {
Expand All @@ -512,7 +520,6 @@ private float getScreenBrightness() {

private void setScreenBrightness(float brightness) {
Activity activity = activity();

if (activity == null || activity.getWindow() == null)
return;
WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
Expand Down Expand Up @@ -549,7 +556,7 @@ private void requestAudioFocus() {
}

@TargetApi(26)
@SuppressWarnings("deprecation")
// @SuppressWarnings("deprecation")
private void abandonAudioFocus() {
AudioManager audioManager = audioManager();
if (audioManager == null)
Expand Down Expand Up @@ -581,7 +588,7 @@ public void audioFocus(boolean request) {

@Nullable
private AudioManager audioManager() {
Context context = mContext.get();
Context context = context();
if (context != null) {
return (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
} else {
Expand Down

0 comments on commit 9f80359

Please sign in to comment.