最近公司项目需要视频后台播放,后台播放关键点:
判断应用是否进入后台(Application ActivityLifecycleCallbacks)
- Application生命周期是最长的,由registerActivityLifecycleCallbacks监控当前到底是哪个Activity,并通过mActivityCount记录是否进入后台,进入后台使用发送广播显示notification
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113/**
* @author maoqitian
* @Description:
* @date 2019/1/4 0004 10:30
*/
public class MyApplication extends Application {
private static final String TAG = "MyApplication";
/**
* 当获取的为0则当前应用在后台,否则不为0则为应用在前台 by maoqitian
*/
private int mActivityCount = 0;
/**
* 是否进入后台
*/
private boolean isBackground=false;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Log.d(TAG,"onActivityCreated");
}
@Override
public void onActivityStarted(Activity activity) {
mActivityCount++;
Log.d(TAG,"onActivityStarted"+"mActivityCount :"+mActivityCount +"activityName:"+ activity.getClass().getSimpleName());
if(mActivityCount == 1 && isBackground
&& "PlayerActivity".equals(activity.getClass().getSimpleName())
){ //只有播放器才进行操作判断
Log.d(TAG, "onActivityStarted: 播放器进入前台" );
Toast.makeText(MyApplication.this,"播放器进入前台",Toast.LENGTH_LONG).show();
isBackground=false;
}
}
@Override
public void onActivityResumed(Activity activity) {
Log.d(TAG,"onActivityResumed");
}
@Override
public void onActivityPaused(Activity activity) {
Log.d(TAG,"onActivityCreated");
}
@Override
public void onActivityStopped(Activity activity) {
Log.d(TAG,"onActivityStopped");
mActivityCount--;
Log.d(TAG,"onActivityStopped"+"mActivityCount :"+mActivityCount+"activityName:"+ activity.getClass().getSimpleName());
if (mActivityCount <= 0 && !isBackground && isRun(activity)
&& "PlayerActivity".equals(activity.getClass().getSimpleName())
) { //只有播放器才进行操作判断,如果应用在后台运行
Log.e(TAG, "onActivityStarted: 播放器进入后台" );
isBackground =true;
//说明应用进入了后台
PlayerActivity playerActivity = (PlayerActivity) activity;
Bundle bundle = playerActivity.onNotificationMsg();
Toast.makeText(MyApplication.this,"播放器进入后台播放",Toast.LENGTH_LONG).show();
//发送显示 notify 广播
Intent intent=new Intent(PlayerNotifyBroadcastReceiver.ACTION_NOTIFY_MESSAGE);
intent.setClass(getApplicationContext(),PlayerNotifyBroadcastReceiver.class);
Log.e(TAG, "string" +bundle.getString("unitTitle"));
intent.putExtra("unitTitle",bundle.getString("unitTitle"));
intent.putExtra("unitImage",bundle.getString("unitImage"));
sendBroadcast(intent);
}
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
Log.d(TAG,"onActivitySaveInstanceState");
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.d(TAG,"onActivityDestroyed");
}
});
}
/**
* 判断应用是否在运行
* @param context
* @return
*/
private boolean isRun(Context context) {
ActivityManager activityManager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
assert activityManager != null;
List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(100);
boolean isAppRunning = false;
String MY_PKG_NAME = "mao.com.backgroundplay";
//100表示取的最大的任务数,info.topActivity表示当前正在运行的Activity,info.baseActivity表系统后台有此进程在运行
for (ActivityManager.RunningTaskInfo info : runningTasks) {
if (info.topActivity.getPackageName().equals(MY_PKG_NAME) || info.baseActivity.getPackageName().equals(MY_PKG_NAME)) {
isAppRunning = true;
Log.i(TAG,info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName());
break;
}
}
return isAppRunning;
}
}
监听后台播放广播
1 | /** |
添加Notification通知(Notification如何加载网络图片(Glide target 或者 直接remoteview 直接设置 bitmap))
1 | /** |
视频后台播放处理(Activity 生命周期 surfacedestory 中不进行释放 player)
- 对应播放器Activity的各个生命周期方法做相应的播放位置记录(onRestart恢复视频播放 onDestory释放相应资源 onBackPressed中也得需要及时pause视频)
1
2
3
4
5
6
7
8
9public void surfaceDestroyed(SurfaceHolder holder) {
......
if(ismPauseInBackground()){
//尝试进入后台播放不释放播放器,继续播放音频
mMediaPlayer.setDisplay(null);
}else {
.....
}
}
手动设置是否进行后台播放 (使用SharedPreferences)
- 使用SharedPreferences,获取状态
1
SharedPreferences toggleBtnSP = this.getSharedPreferences("toggleButtonState", Context.MODE_PRIVATE);