CNAudioPlayerActivity.java
10.6 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package com.cnlive.demo.videoqn;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.cnlive.demo.videoqn.utils.Utils;
import com.cnlive.libs.lib_cnvideo.base.IAVOptions;
import com.cnlive.libs.lib_cnvideo.base.IMediaPlayer;
import com.cnlive.libs.lib_cnvideo.player.AVOptions;
import com.cnlive.libs.lib_cnvideo.player.MediaPlayer;
import java.io.IOException;
/**
* This demo shows how to use PLMediaPlayer API playing audio stream
*/
public class CNAudioPlayerActivity extends AppCompatActivity {
private static final String TAG = CNAudioPlayerActivity.class.getSimpleName();
private static final int MESSAGE_ID_RECONNECTING = 0x01;
private MediaPlayer mMediaPlayer;
private String mAudioPath;
private View mLoadingView;
private AVOptions mAVOptions;
private boolean mIsStopped = false;
private boolean mIsActivityPaused = true;
private Toast mToast = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_player);
mLoadingView = findViewById(R.id.LoadingView);
mAudioPath = getIntent().getStringExtra("videoPath");
//配置播放参数
mAVOptions = new AVOptions();
int isLiveStreaming = getIntent().getIntExtra("liveStreaming", 1);
//
mAVOptions.setInteger(IAVOptions.KEY_PREPARE_TIMEOUT, 10 * 1000);
mAVOptions.setInteger(IAVOptions.KEY_GET_AV_FRAME_TIMEOUT, 10 * 1000);
// Some optimization with buffering mechanism when be set to 1
mAVOptions.setInteger(IAVOptions.KEY_LIVE_STREAMING, isLiveStreaming);
if (isLiveStreaming == 1) {
mAVOptions.setInteger(IAVOptions.KEY_DELAY_OPTIMIZATION, 1);
}
// 1 -> hw codec enable, 0 -> disable [recommended]
int codec = getIntent().getIntExtra("mediaCodec", 0);
mAVOptions.setInteger(IAVOptions.KEY_MEDIACODEC, codec);
// whether start play automatically after prepared, default value is 1
mAVOptions.setInteger(IAVOptions.KEY_START_ON_PREPARED, 0);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
prepare();
}
@Override
protected void onDestroy() {
super.onDestroy();
release();
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.abandonAudioFocus(null);
}
@Override
protected void onResume() {
super.onResume();
mIsActivityPaused = false;
// mMediaPlayer.start();
}
@Override
protected void onPause() {
super.onPause();
mIsActivityPaused = true;
// mMediaPlayer.pause();
}
public void onClickPlay(View v) {
if (mIsStopped) {
prepare();
} else {
mMediaPlayer.start();
}
}
public void onClickPause(View v) {
if (mMediaPlayer != null) {
mMediaPlayer.pause();
}
}
public void onClickResume(View v) {
if (mMediaPlayer != null) {
mMediaPlayer.start();
}
}
public void onClickStop(View v) {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.reset();
}
mIsStopped = true;
mMediaPlayer = null;
}
public void release() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
private void prepare() {
if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer(getApplicationContext(), mAVOptions);
mMediaPlayer.setOnPreparedListener(mOnPreparedListener);
mMediaPlayer.setOnCompletionListener(mOnCompletionListener);
mMediaPlayer.setOnErrorListener(mOnErrorListener);
mMediaPlayer.setOnInfoListener(mOnInfoListener);
mMediaPlayer.setOnBufferingUpdateListener(mOnBufferingUpdateListener);
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
}
try {
mMediaPlayer.setDataSource(mAudioPath);
mMediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private IMediaPlayer.OnPreparedListener mOnPreparedListener = new IMediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(IMediaPlayer mp) {
Log.i(TAG, "On Prepared !");
mMediaPlayer.start();
mIsStopped = false;
}
};
private IMediaPlayer.OnInfoListener mOnInfoListener = new IMediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(IMediaPlayer mp, int what, int extra) {
Log.i(TAG, "OnInfo, what = " + what + ", extra = " + extra);
switch (what) {
case IMediaPlayer.MEDIA_INFO_BUFFERING_START:
mLoadingView.setVisibility(View.VISIBLE);
break;
case IMediaPlayer.MEDIA_INFO_BUFFERING_END:
case IMediaPlayer.MEDIA_INFO_AUDIO_RENDERING_START:
mLoadingView.setVisibility(View.GONE);
break;
default:
break;
}
return true;
}
};
private IMediaPlayer.OnBufferingUpdateListener mOnBufferingUpdateListener = new IMediaPlayer.OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(IMediaPlayer mp, int percent) {
Log.d(TAG, "onBufferingUpdate: " + percent + "%");
}
};
/**
* Listen the event of playing complete
* For playing local file, it's called when reading the file EOF
* For playing network stream, it's called when the buffered bytes played over
*
* If setLooping(true) is called, the player will restart automatically
* And `onCompletion` will not be called
*/
private IMediaPlayer.OnCompletionListener mOnCompletionListener = new IMediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(IMediaPlayer mp) {
Log.d(TAG, "Play Completed !");
}
};
private IMediaPlayer.OnErrorListener mOnErrorListener = new IMediaPlayer.OnErrorListener() {
@Override
public boolean onError(IMediaPlayer mp, int errorCode) {
boolean isNeedReconnect = false;
Log.e(TAG, "Error happened, errorCode = " + errorCode);
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()
release();
if (isNeedReconnect) {
sendReconnectMessage();
} else {
finish();
}
// Return true means the error has been handled
// If return false, then `onCompletion` will be called
return true;
}
};
private void showToastTips(final String tips) {
if (mIsActivityPaused) {
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(CNAudioPlayerActivity.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(CNAudioPlayerActivity.this)) {
sendReconnectMessage();
return;
}
// The PLMediaPlayer has moved to the Error state, if you want to retry, must reset first !
prepare();
}
};
}