Browse Source

修改视频播放错误时可以显示多种不同状态

yangchong211 6 years ago
parent
commit
b8933123a2

+ 1 - 1
README.md

@@ -68,7 +68,7 @@
 - B.1.7 支持切换视频清晰度模式,同时切换清晰度后,支持视频保持观看进度
 - B.1.8 添加锁屏功能,竖屏不提供锁屏按钮,横屏全屏时显示,并且锁屏时,屏蔽手势处理
 - B.1.9 当在播放视频页面,由前台切换到后台时,如果视频正在播放或者正在缓冲时,则暂停视频;当从后台切换到前台时,如果视频暂停时或者缓冲暂停,则重新开启视频播放;当退出页面时则销毁视频资源。
-
+- B.2.0 当正在缓冲或者播放准备中状态时,开启缓冲时更新网络加载速度[默认时kbs/每秒]
 
 
 #### 1.3 拓展功能

+ 1 - 1
YCVideoPlayerLib/src/main/java/org/yczbj/ycvideoplayerlib/controller/AbsVideoPlayerController.java

@@ -167,7 +167,7 @@ public abstract class AbsVideoPlayerController extends FrameLayout implements Vi
 
 
     /**
-     * 开启缓冲时更新网络加载速度
+     * 当正在缓冲或者播放准备中状态时,开启缓冲时更新网络加载速度
      */
     protected void startUpdateNetSpeedTimer() {
         cancelUpdateNetSpeedTimer();

+ 28 - 7
YCVideoPlayerLib/src/main/java/org/yczbj/ycvideoplayerlib/controller/VideoPlayerController.java

@@ -93,6 +93,7 @@ public class VideoPlayerController extends AbsVideoPlayerController implements V
     private LinearLayout mChangeVolume;
     private ProgressBar mChangeVolumeProgress;
     private LinearLayout mError;
+    private TextView mTvError;
     private TextView mRetry;
     private LinearLayout mCompleted;
     private TextView mReplay;
@@ -170,7 +171,8 @@ public class VideoPlayerController extends AbsVideoPlayerController implements V
                 if (info != null) {
                     //如果当前的网络连接成功并且网络连接可用
                     if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
-                        if (info.getType() == ConnectivityManager.TYPE_WIFI || info.getType() == ConnectivityManager.TYPE_MOBILE) {
+                        if (info.getType() == ConnectivityManager.TYPE_WIFI ||
+                                info.getType() == ConnectivityManager.TYPE_MOBILE) {
                             VideoLogUtil.i(getConnectionType(info.getType()) + "连上");
                         }
                     } else {
@@ -330,6 +332,7 @@ public class VideoPlayerController extends AbsVideoPlayerController implements V
         mChangeVolumeProgress = findViewById(R.id.change_volume_progress);
 
         mError = findViewById(R.id.error);
+        mTvError = findViewById(R.id.tv_error);
         mRetry = findViewById(R.id.retry);
         mCompleted = findViewById(R.id.completed);
         mReplay = findViewById(R.id.replay);
@@ -637,6 +640,7 @@ public class VideoPlayerController extends AbsVideoPlayerController implements V
             //播放准备就绪
             case ConstantKeys.CurrentState.STATE_PREPARED:
                 startUpdateProgressTimer();
+                //取消缓冲时更新网络加载速度
                 cancelUpdateNetSpeedTimer();
                 break;
             //正在播放
@@ -670,15 +674,12 @@ public class VideoPlayerController extends AbsVideoPlayerController implements V
                 mRestartPause.setImageResource(R.drawable.ic_player_start);
                 mLoadText.setText("正在准备...");
                 cancelDismissTopBottomTimer();
+                //开启缓冲时更新网络加载速度
                 startUpdateNetSpeedTimer();
                 break;
             //播放错误
             case ConstantKeys.CurrentState.STATE_ERROR:
-                cancelUpdateProgressTimer();
-                setTopBottomVisible(false);
-                mTop.setVisibility(View.VISIBLE);
-                mError.setVisibility(View.VISIBLE);
-                cancelUpdateNetSpeedTimer();
+                stateError();
                 break;
             //播放完成
             case ConstantKeys.CurrentState.STATE_COMPLETED:
@@ -703,10 +704,29 @@ public class VideoPlayerController extends AbsVideoPlayerController implements V
         mBottom.setVisibility(View.GONE);
         mCenterStart.setVisibility(View.GONE);
         mLength.setVisibility(View.GONE);
+        //开启缓冲时更新网络加载速度
         startUpdateNetSpeedTimer();
     }
 
 
+    /**
+     * 播放错误
+     */
+    private void stateError() {
+        setTopBottomVisible(false);
+        mTop.setVisibility(View.VISIBLE);
+        mError.setVisibility(View.VISIBLE);
+        mLine.setVisibility(GONE);
+        cancelUpdateProgressTimer();
+        cancelUpdateNetSpeedTimer();
+        cancelUpdateProgressTimer();
+        if (!VideoPlayerUtils.isConnected(mContext)){
+            mTvError.setText("没有网络,请链接网络");
+        } else {
+            mTvError.setText("播放错误,请重试");
+        }
+    }
+
     /**
      * 播放完成
      */
@@ -809,8 +829,9 @@ public class VideoPlayerController extends AbsVideoPlayerController implements V
     public void destroy() {
         //当播放完成就解除广播
         unRegisterNetChangedReceiver();
-        cancelUpdateProgressTimer();
+        unRegisterBatterReceiver();
         //结束timer
+        cancelUpdateProgressTimer();
         cancelUpdateNetSpeedTimer();
     }
 

+ 1 - 1
YCVideoPlayerLib/src/main/java/org/yczbj/ycvideoplayerlib/view/VideoTextureView.java

@@ -71,7 +71,7 @@ public class VideoTextureView extends TextureView implements TextureView.Surface
         if (onSurfaceListener != null) {
             onSurfaceListener.onSurfaceDestroyed(surface);
         }
-        return true;
+        return false;
     }
 
 

+ 1 - 0
YCVideoPlayerLib/src/main/res/layout/custom_video_player_error.xml

@@ -9,6 +9,7 @@
     android:orientation="vertical"
     android:visibility="gone">
     <TextView
+        android:id="@+id/tv_error"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="播放错误,请重试。"

+ 0 - 2
app/src/main/AndroidManifest.xml

@@ -73,8 +73,6 @@
             android:screenOrientation="portrait"/>
         <activity android:name=".ui.home.view.activity.EmptyActivity"/>
 
-        <service android:name=".service.InitializeService"/>
-
     </application>
 
 </manifest>

+ 32 - 3
app/src/main/java/org/yczbj/ycvideoplayer/base/BaseApplication.java

@@ -7,9 +7,15 @@ import android.support.multidex.MultiDex;
 import android.util.Log;
 
 import com.blankj.utilcode.util.Utils;
+import com.liulishuo.filedownloader.FileDownloader;
+import com.liulishuo.filedownloader.connection.FileDownloadUrlConnection;
 
-import org.yczbj.ycvideoplayer.service.InitializeService;
+import org.yczbj.ycvideoplayer.BuildConfig;
+import org.yczbj.ycvideoplayer.util.LogUtils;
 import org.yczbj.ycvideoplayer.util.ScreenDensityUtils;
+import org.yczbj.ycvideoplayerlib.utils.VideoLogUtil;
+
+import java.net.Proxy;
 
 /**
  * ================================================
@@ -56,8 +62,14 @@ public class BaseApplication extends Application {
         ScreenDensityUtils.setup(this);
         ScreenDensityUtils.register(this,375.0f,
                 ScreenDensityUtils.MATCH_BASE_WIDTH,ScreenDensityUtils.MATCH_UNIT_DP);
-        //在子线程中初始化
-        InitializeService.start(this);
+        BaseConfig.INSTANCE.initConfig();
+        LogUtils.logDebug = true;
+        if(BuildConfig.DEBUG){
+            VideoLogUtil.setIsLog(true);
+        }else {
+            VideoLogUtil.setIsLog(false);
+        }
+        initDownLoadLib();
     }
 
     /**
@@ -109,6 +121,23 @@ public class BaseApplication extends Application {
     }
 
 
+    /**
+     * 初始化下载库
+     */
+    private void initDownLoadLib() {
+        FileDownloader.setupOnApplicationOnCreate(BaseApplication.getInstance())
+                .connectionCreator(new FileDownloadUrlConnection
+                        .Creator(new FileDownloadUrlConnection.Configuration()
+                        .connectTimeout(15_000)
+                        .readTimeout(15_000)
+                        .proxy(Proxy.NO_PROXY)
+                ))
+                .commit();
+        //最简单的初始化
+        //FileDownloader.setup(instance);
+    }
+
+
 }
 
 

+ 0 - 79
app/src/main/java/org/yczbj/ycvideoplayer/service/InitializeService.java

@@ -1,79 +0,0 @@
-package org.yczbj.ycvideoplayer.service;
-
-import android.app.IntentService;
-import android.content.Context;
-import android.content.Intent;
-
-import com.liulishuo.filedownloader.FileDownloader;
-import com.liulishuo.filedownloader.connection.FileDownloadUrlConnection;
-
-import org.yczbj.ycvideoplayer.BuildConfig;
-import org.yczbj.ycvideoplayer.base.BaseApplication;
-import org.yczbj.ycvideoplayer.base.BaseConfig;
-import org.yczbj.ycvideoplayer.util.LogUtils;
-import org.yczbj.ycvideoplayerlib.utils.VideoLogUtil;
-
-import java.net.Proxy;
-
-public class InitializeService extends IntentService {
-
-    private static final String ACTION_INIT = "initApplication";
-
-    public static void start(Context context) {
-        Intent intent = new Intent(context, InitializeService.class);
-        intent.setAction(ACTION_INIT);
-        context.startService(intent);
-    }
-
-    /**
-     * Creates an IntentService.  Invoked by your subclass's constructor.
-     * Used to name the worker thread, important only for debugging.
-     */
-    /*public InitializeService(String name) {
-        super(name);
-    }*/
-
-    public InitializeService(){
-        super("InitializeService");
-    }
-
-    @Override
-    protected void onHandleIntent(Intent intent) {
-        if (intent != null) {
-            final String action = intent.getAction();
-            if (ACTION_INIT.equals(action)) {
-                initApplication();
-            }
-        }
-    }
-
-    private void initApplication() {
-        BaseConfig.INSTANCE.initConfig();
-        LogUtils.logDebug = true;
-        if(BuildConfig.DEBUG){
-            VideoLogUtil.setIsLog(true);
-        }else {
-            VideoLogUtil.setIsLog(false);
-        }
-        initDownLoadLib();
-    }
-
-    /**
-     * 初始化下载库
-     */
-    private void initDownLoadLib() {
-        FileDownloader.setupOnApplicationOnCreate(BaseApplication.getInstance())
-                .connectionCreator(new FileDownloadUrlConnection
-                        .Creator(new FileDownloadUrlConnection.Configuration()
-                        .connectTimeout(15_000)
-                        .readTimeout(15_000)
-                        .proxy(Proxy.NO_PROXY)
-                ))
-                .commit();
-        //最简单的初始化
-        //FileDownloader.setup(instance);
-    }
-
-
-
-}