CNVideoTextureActivity.java
9.35 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package com.cnlive.demo.videoqn;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.cnlive.demo.videoqn.utils.Utils;
import com.cnlive.demo.videoqn.widget.MediaController;
import com.cnlive.libs.lib_cnvideo.base.IAVOptions;
import com.cnlive.libs.lib_cnvideo.base.ICNVideoTextureView;
import com.cnlive.libs.lib_cnvideo.base.IMediaPlayer;
import com.cnlive.libs.lib_cnvideo.player.AVOptions;
import com.cnlive.libs.lib_cnvideo.player.widget.CNVideoTextureView;
/**
* This is a demo activity of PLVideoTextureView
*/
public class CNVideoTextureActivity extends VideoPlayerBaseActivity {
private static final int MESSAGE_ID_RECONNECTING = 0x01;
private MediaController mMediaController;
private CNVideoTextureView mVideoView;
private Toast mToast = null;
private String mVideoPath = null;
private int mRotation = 0;
private int mDisplayAspectRatio = ICNVideoTextureView.ASPECT_RATIO_FIT_PARENT; //default
private View mLoadingView;
private View mCoverView = null;
private boolean mIsActivityPaused = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pl_video_texture);
mVideoView = (CNVideoTextureView) findViewById(R.id.VideoView);
mLoadingView = findViewById(R.id.LoadingView);
mVideoView.setBufferingIndicator(mLoadingView);
mLoadingView.setVisibility(View.VISIBLE);
mCoverView = (ImageView) findViewById(R.id.CoverView);
mVideoView.setCoverView(mCoverView);
mVideoPath = getIntent().getStringExtra("videoPath");
// If you want to fix display orientation such as landscape, you can use the code show as follow
//
// if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// mVideoView.setPreviewOrientation(0);
// }
// else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// mVideoView.setPreviewOrientation(270);
// }
mVideoPath = getIntent().getStringExtra("videoPath");
AVOptions options = new AVOptions();
int isLiveStreaming = getIntent().getIntExtra("liveStreaming", 1);
// the unit of timeout is ms
options.setInteger(IAVOptions.KEY_PREPARE_TIMEOUT, 10 * 1000);
options.setInteger(IAVOptions.KEY_GET_AV_FRAME_TIMEOUT, 10 * 1000);
// Some optimization with buffering mechanism when be set to 1
options.setInteger(IAVOptions.KEY_LIVE_STREAMING, isLiveStreaming);
if (isLiveStreaming == 1) {
options.setInteger(IAVOptions.KEY_DELAY_OPTIMIZATION, 1);
}
// 1 -> hw codec enable, 0 -> disable [recommended]
int codec = getIntent().getIntExtra("mediaCodec", 0);
options.setInteger(IAVOptions.KEY_MEDIACODEC, codec);
// whether start play automatically after prepared, default value is 1
options.setInteger(IAVOptions.KEY_START_ON_PREPARED, 0);
mVideoView.setAVOptions(options);
// You can mirror the display
// mVideoView.setMirror(true);
// You can also use a custom `MediaController` widget
mMediaController = new MediaController(this, false, isLiveStreaming == 1);
mVideoView.setMediaController(mMediaController);
mVideoView.setOnCompletionListener(mOnCompletionListener);
mVideoView.setOnErrorListener(mOnErrorListener);
mVideoView.setOnPreparedListener(onPreparedListener);
mVideoView.setVideoPath(mVideoPath);
}
@Override
protected void onPause() {
super.onPause();
mToast = null;
mIsActivityPaused = true;
mVideoView.pause();
}
@Override
protected void onResume() {
super.onResume();
mIsActivityPaused = false;
}
@Override
protected void onDestroy() {
super.onDestroy();
mVideoView.stopPlayback();
}
public void onClickRotate(View v) {
mRotation = (mRotation + 90) % 360;
mVideoView.setDisplayOrientation(mRotation);
}
public void onClickSwitchScreen(View v) {
mDisplayAspectRatio = (mDisplayAspectRatio + 1) % 5;
mVideoView.setDisplayAspectRatio(mDisplayAspectRatio);
switch (mVideoView.getDisplayAspectRatio()) {
case ICNVideoTextureView.ASPECT_RATIO_ORIGIN:
showToastTips("Origin mode");
break;
case ICNVideoTextureView.ASPECT_RATIO_FIT_PARENT:
showToastTips("Fit parent !");
break;
case ICNVideoTextureView.ASPECT_RATIO_PAVED_PARENT:
showToastTips("Paved parent !");
break;
case ICNVideoTextureView.ASPECT_RATIO_16_9:
showToastTips("16 : 9 !");
break;
case ICNVideoTextureView.ASPECT_RATIO_4_3:
showToastTips("4 : 3 !");
break;
default:
break;
}
}
private IMediaPlayer.OnPreparedListener onPreparedListener = new IMediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(IMediaPlayer mp) {
mVideoView.start();
}
};
private IMediaPlayer.OnErrorListener mOnErrorListener = new IMediaPlayer.OnErrorListener() {
@Override
public boolean onError(IMediaPlayer mp, int errorCode) {
boolean isNeedReconnect = false;
switch (errorCode) {
case IMediaPlayer.ERROR_CODE_INVALID_URI:
showToastTips("Invalid URL !");
break;
case IMediaPlayer.ERROR_CODE_404_NOT_FOUND:
showToastTips("404 resource not found !");
break;
case IMediaPlayer.ERROR_CODE_CONNECTION_REFUSED:
showToastTips("Connection refused !");
break;
case IMediaPlayer.ERROR_CODE_CONNECTION_TIMEOUT:
showToastTips("Connection timeout !");
isNeedReconnect = true;
break;
case IMediaPlayer.ERROR_CODE_EMPTY_PLAYLIST:
showToastTips("Empty playlist !");
break;
case IMediaPlayer.ERROR_CODE_STREAM_DISCONNECTED:
showToastTips("Stream disconnected !");
isNeedReconnect = true;
break;
case IMediaPlayer.ERROR_CODE_IO_ERROR:
showToastTips("Network IO Error !");
isNeedReconnect = true;
break;
case IMediaPlayer.ERROR_CODE_UNAUTHORIZED:
showToastTips("Unauthorized Error !");
break;
case IMediaPlayer.ERROR_CODE_PREPARE_TIMEOUT:
showToastTips("Prepare timeout !");
isNeedReconnect = true;
break;
case IMediaPlayer.ERROR_CODE_READ_FRAME_TIMEOUT:
showToastTips("Read frame timeout !");
isNeedReconnect = true;
break;
case IMediaPlayer.MEDIA_ERROR_UNKNOWN:
break;
default:
showToastTips("unknown error !");
break;
}
// Todo pls handle the error status here, reconnect or call finish()
if (isNeedReconnect) {
sendReconnectMessage();
} else {
finish();
}
// Return true means the error has been handled
// If return false, then `onCompletion` will be called
return true;
}
};
private IMediaPlayer.OnCompletionListener mOnCompletionListener = new IMediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(IMediaPlayer plMediaPlayer) {
showToastTips("Play Completed !");
finish();
}
};
private void showToastTips(final String tips) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(CNVideoTextureActivity.this, tips, Toast.LENGTH_SHORT);
mToast.show();
}
});
}
private void sendReconnectMessage() {
showToastTips("正在重连...");
mLoadingView.setVisibility(View.VISIBLE);
mHandler.removeCallbacksAndMessages(null);
mHandler.sendMessageDelayed(mHandler.obtainMessage(MESSAGE_ID_RECONNECTING), 500);
}
protected Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
if (msg.what != MESSAGE_ID_RECONNECTING) {
return;
}
if (mIsActivityPaused || !Utils.isLiveStreamingAvailable()) {
finish();
return;
}
if (!Utils.isNetworkAvailable(CNVideoTextureActivity.this)) {
sendReconnectMessage();
return;
}
mVideoView.setVideoPath(mVideoPath);
mVideoView.start();
}
};
}