From d763a916ac225527004d74a7b6a29460bc0bf233 Mon Sep 17 00:00:00 2001 From: shuyu <359369982@qq.com> Date: Wed, 5 Jul 2017 21:08:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9Appbar=E4=B8=AD=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20(2017-07-05)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UPDATE_VERSION.md | 3 ++ .../shuyu/gsyvideoplayer/GSYVideoPlayer.java | 30 +++++++++++-------- .../gsyvideoplayer/utils/CommonUtil.java | 11 +++++++ .../video/CustomGSYVideoPlayer.java | 2 +- .../video/GSYBaseVideoPlayer.java | 9 ++++-- .../video/StandardGSYVideoPlayer.java | 22 +++++++------- 6 files changed, 50 insertions(+), 27 deletions(-) diff --git a/UPDATE_VERSION.md b/UPDATE_VERSION.md index ef97aa3..3dd7e5e 100644 --- a/UPDATE_VERSION.md +++ b/UPDATE_VERSION.md @@ -2,6 +2,9 @@ ### 1.6.9() +* 兼容Appbar中使用,感谢[@loveRose](https://github.com/loveRose) +* 修改setup的设置参数。 +* 升级修改所有回调接口,回调接口中返回当前播放器。 * 非全屏播放器可获取全屏播放器对象。 ``` /** diff --git a/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/GSYVideoPlayer.java b/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/GSYVideoPlayer.java index 52b5000..c8b65bd 100644 --- a/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/GSYVideoPlayer.java +++ b/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/GSYVideoPlayer.java @@ -172,7 +172,13 @@ public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View. } protected void init(Context context) { - this.mContext = context; + + if (getActivityContext() != null) { + this.mContext = getActivityContext(); + } else { + this.mContext = context; + } + View.inflate(context, getLayoutId(), this); mStartButton = findViewById(R.id.start); mSmallClose = findViewById(R.id.small_close); @@ -196,11 +202,11 @@ public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View. mTextureViewContainer.setOnTouchListener(this); mFullscreenButton.setOnTouchListener(this); - mScreenWidth = getContext().getResources().getDisplayMetrics().widthPixels; - mScreenHeight = getContext().getResources().getDisplayMetrics().heightPixels; - mAudioManager = (AudioManager) getContext().getApplicationContext().getSystemService(Context.AUDIO_SERVICE); + mScreenWidth = getActivityContext().getResources().getDisplayMetrics().widthPixels; + mScreenHeight = getActivityContext().getResources().getDisplayMetrics().heightPixels; + mAudioManager = (AudioManager) getActivityContext().getApplicationContext().getSystemService(Context.AUDIO_SERVICE); - mSeekEndOffset = CommonUtil.dip2px(getContext(), 50); + mSeekEndOffset = CommonUtil.dip2px(getActivityContext(), 50); } @@ -217,7 +223,7 @@ public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View. * * @param url 播放url * @param cacheWithPlay 是否边播边缓存 - * @param title title + * @param title title * @return */ public boolean setUp(String url, boolean cacheWithPlay, String title) { @@ -232,7 +238,7 @@ public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View. * @param cacheWithPlay 是否边播边缓存 * @param cachePath 缓存路径,如果是M3U8或者HLS,请设置为false * @param mapHeadData 头部信息 - * @param title title + * @param title title * @return */ @Override @@ -252,7 +258,7 @@ public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View. * @param url 播放url * @param cacheWithPlay 是否边播边缓存 * @param cachePath 缓存路径,如果是M3U8或者HLS,请设置为false - * @param title title + * @param title title * @return */ @Override @@ -265,7 +271,7 @@ public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View. return false; mCurrentState = CURRENT_STATE_NORMAL; if (cacheWithPlay && url.startsWith("http") && !url.contains("127.0.0.1") && !url.contains(".m3u8")) { - HttpProxyCacheServer proxy = GSYVideoManager.getProxy(getContext().getApplicationContext(), cachePath); + HttpProxyCacheServer proxy = GSYVideoManager.getProxy(getActivityContext().getApplicationContext(), cachePath); //此处转换了url,然后再赋值给mUrl。 url = proxy.getProxyUrl(url); mCacheFile = (!url.startsWith("http")); @@ -333,7 +339,7 @@ public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View. } if (i == R.id.start) { if (TextUtils.isEmpty(mUrl)) { - Toast.makeText(getContext(), getResources().getString(R.string.no_url), Toast.LENGTH_SHORT).show(); + Toast.makeText(getActivityContext(), getResources().getString(R.string.no_url), Toast.LENGTH_SHORT).show(); return; } if (mCurrentState == CURRENT_STATE_NORMAL || mCurrentState == CURRENT_STATE_ERROR) { @@ -1058,7 +1064,7 @@ public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View. CommonUtil.deleteFile(path); } else { String path = StorageUtils.getIndividualCacheDirectory - (getContext().getApplicationContext()).getAbsolutePath() + (getActivityContext().getApplicationContext()).getAbsolutePath() + File.separator + name + ".download"; CommonUtil.deleteFile(path); } @@ -1232,7 +1238,7 @@ public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View. */ protected void createNetWorkState() { if (mNetInfoModule == null) { - mNetInfoModule = new NetInfoModule(getContext().getApplicationContext(), new NetInfoModule.NetChangeListener() { + mNetInfoModule = new NetInfoModule(getActivityContext().getApplicationContext(), new NetInfoModule.NetChangeListener() { @Override public void changed(String state) { if (!mNetSate.equals(state)) { diff --git a/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/utils/CommonUtil.java b/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/utils/CommonUtil.java index b5b78de..22d32da 100644 --- a/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/utils/CommonUtil.java +++ b/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/utils/CommonUtil.java @@ -255,4 +255,15 @@ public class CommonUtil { } } } + + public static Activity getActivityContext(Context context) { + if (context == null) + return null; + else if (context instanceof Activity) + return (Activity) context; + else if (context instanceof ContextWrapper) + return scanForActivity(((ContextWrapper) context).getBaseContext()); + + return null; + } } diff --git a/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/video/CustomGSYVideoPlayer.java b/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/video/CustomGSYVideoPlayer.java index 60c8da8..6b0685f 100644 --- a/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/video/CustomGSYVideoPlayer.java +++ b/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/video/CustomGSYVideoPlayer.java @@ -72,7 +72,7 @@ public class CustomGSYVideoPlayer extends StandardGSYVideoPlayer { mPreviewLayout.removeAllViews(); } mPreviewTexture = null; - mPreviewTexture = new GSYTextureView(getContext()); + mPreviewTexture = new GSYTextureView(getActivityContext()); mPreviewTexture.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { diff --git a/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/video/GSYBaseVideoPlayer.java b/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/video/GSYBaseVideoPlayer.java index da1d15c..3407513 100644 --- a/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/video/GSYBaseVideoPlayer.java +++ b/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/video/GSYBaseVideoPlayer.java @@ -169,6 +169,9 @@ public abstract class GSYBaseVideoPlayer extends FrameLayout implements GSYMedia super(context, attrs, defStyleAttr); } + public Context getActivityContext() { + return CommonUtil.getActivityContext(getContext()); + } private ViewGroup getViewGroup() { return (ViewGroup) (CommonUtil.scanForActivity(getContext())).findViewById(Window.ID_ANDROID_CONTENT); @@ -333,10 +336,10 @@ public abstract class GSYBaseVideoPlayer extends FrameLayout implements GSYMedia final GSYBaseVideoPlayer gsyVideoPlayer; if (!hadNewConstructor) { constructor = (Constructor) GSYBaseVideoPlayer.this.getClass().getConstructor(Context.class); - gsyVideoPlayer = constructor.newInstance(getContext()); + gsyVideoPlayer = constructor.newInstance(getActivityContext()); } else { constructor = (Constructor) GSYBaseVideoPlayer.this.getClass().getConstructor(Context.class, Boolean.class); - gsyVideoPlayer = constructor.newInstance(getContext(), true); + gsyVideoPlayer = constructor.newInstance(getActivityContext(), true); } gsyVideoPlayer.setId(FULLSCREEN_ID); @@ -540,7 +543,7 @@ public abstract class GSYBaseVideoPlayer extends FrameLayout implements GSYMedia try { Constructor constructor = (Constructor) GSYBaseVideoPlayer.this.getClass().getConstructor(Context.class); - GSYBaseVideoPlayer gsyVideoPlayer = constructor.newInstance(getContext()); + GSYBaseVideoPlayer gsyVideoPlayer = constructor.newInstance(getActivityContext()); gsyVideoPlayer.setId(SMALL_ID); FrameLayout.LayoutParams lpParent = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); diff --git a/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/video/StandardGSYVideoPlayer.java b/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/video/StandardGSYVideoPlayer.java index 364c121..7a8d7e9 100644 --- a/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/video/StandardGSYVideoPlayer.java +++ b/gsyVideoPlayer/src/main/java/com/shuyu/gsyvideoplayer/video/StandardGSYVideoPlayer.java @@ -297,11 +297,11 @@ public class StandardGSYVideoPlayer extends GSYVideoPlayer { return; } if (TextUtils.isEmpty(mUrl)) { - Toast.makeText(getContext(), getResources().getString(R.string.no_url), Toast.LENGTH_SHORT).show(); + Toast.makeText(getActivityContext(), getResources().getString(R.string.no_url), Toast.LENGTH_SHORT).show(); return; } if (mCurrentState == CURRENT_STATE_NORMAL) { - if (!mUrl.startsWith("file") && !CommonUtil.isWifiConnected(getContext()) && mNeedShowWifiTip) { + if (!mUrl.startsWith("file") && !CommonUtil.isWifiConnected(getActivityContext()) && mNeedShowWifiTip) { showWifiDialog(); return; } @@ -330,7 +330,7 @@ public class StandardGSYVideoPlayer extends GSYVideoPlayer { Toast.makeText(mContext, getResources().getString(R.string.no_net), Toast.LENGTH_LONG).show(); return; } - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); + AlertDialog.Builder builder = new AlertDialog.Builder(getActivityContext()); builder.setMessage(getResources().getString(R.string.tips_not_wifi)); builder.setPositiveButton(getResources().getString(R.string.tips_not_wifi_confirm), new DialogInterface.OnClickListener() { @Override @@ -643,7 +643,7 @@ public class StandardGSYVideoPlayer extends GSYVideoPlayer { protected void showProgressDialog(float deltaX, String seekTime, int seekTimePosition, String totalTime, int totalTimeDuration) { super.showProgressDialog(deltaX, seekTime, seekTimePosition, totalTime, totalTimeDuration); if (mProgressDialog == null) { - View localView = LayoutInflater.from(getContext()).inflate(R.layout.video_progress_dialog, null); + View localView = LayoutInflater.from(getActivityContext()).inflate(R.layout.video_progress_dialog, null); mDialogProgressBar = ((ProgressBar) localView.findViewById(R.id.duration_progressbar)); if (mDialogProgressBarDrawable != null) { mDialogProgressBar.setProgressDrawable(mDialogProgressBarDrawable); @@ -651,7 +651,7 @@ public class StandardGSYVideoPlayer extends GSYVideoPlayer { mDialogSeekTime = ((TextView) localView.findViewById(R.id.tv_current)); mDialogTotalTime = ((TextView) localView.findViewById(R.id.tv_duration)); mDialogIcon = ((ImageView) localView.findViewById(R.id.duration_image_tip)); - mProgressDialog = new Dialog(getContext(), R.style.video_style_dialog_progress); + mProgressDialog = new Dialog(getActivityContext(), R.style.video_style_dialog_progress); mProgressDialog.setContentView(localView); mProgressDialog.getWindow().addFlags(Window.FEATURE_ACTION_BAR); mProgressDialog.getWindow().addFlags(32); @@ -702,12 +702,12 @@ public class StandardGSYVideoPlayer extends GSYVideoPlayer { protected void showVolumeDialog(float deltaY, int volumePercent) { super.showVolumeDialog(deltaY, volumePercent); if (mVolumeDialog == null) { - View localView = LayoutInflater.from(getContext()).inflate(R.layout.video_volume_dialog, null); + View localView = LayoutInflater.from(getActivityContext()).inflate(R.layout.video_volume_dialog, null); mDialogVolumeProgressBar = ((ProgressBar) localView.findViewById(R.id.volume_progressbar)); if (mVolumeProgressDrawable != null) { mDialogVolumeProgressBar.setProgressDrawable(mVolumeProgressDrawable); } - mVolumeDialog = new Dialog(getContext(), R.style.video_style_dialog_progress); + mVolumeDialog = new Dialog(getActivityContext(), R.style.video_style_dialog_progress); mVolumeDialog.setContentView(localView); mVolumeDialog.getWindow().addFlags(8); mVolumeDialog.getWindow().addFlags(32); @@ -742,9 +742,9 @@ public class StandardGSYVideoPlayer extends GSYVideoPlayer { @Override protected void showBrightnessDialog(float percent) { if (mBrightnessDialog == null) { - View localView = LayoutInflater.from(getContext()).inflate(R.layout.video_brightness, null); + View localView = LayoutInflater.from(getActivityContext()).inflate(R.layout.video_brightness, null); mBrightnessDialogTv = (TextView) localView.findViewById(R.id.app_video_brightness); - mBrightnessDialog = new Dialog(getContext(), R.style.video_style_dialog_progress); + mBrightnessDialog = new Dialog(getActivityContext(), R.style.video_style_dialog_progress); mBrightnessDialog.setContentView(localView); mBrightnessDialog.getWindow().addFlags(8); mBrightnessDialog.getWindow().addFlags(32); @@ -918,8 +918,8 @@ public class StandardGSYVideoPlayer extends GSYVideoPlayer { if (mCurrentState != CURRENT_STATE_NORMAL && mCurrentState != CURRENT_STATE_ERROR && mCurrentState != CURRENT_STATE_AUTO_COMPLETE) { - if (getContext() != null && getContext() instanceof Activity) { - ((Activity) getContext()).runOnUiThread(new Runnable() { + if (getActivityContext() != null) { + ((Activity) getActivityContext()).runOnUiThread(new Runnable() { @Override public void run() { hideAllWidget(); -- GitLab