BaseStreamActivity.java
24.1 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
package com.cnlive.demo.streamqn;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
import com.cnlive.demo.streamqn.gles.FBO;
import com.cnlive.demo.streamqn.ui.CameraPreviewFrameView;
import com.cnlive.demo.streamqn.ui.RotateLayout;
import com.cnlive.libs.lib_cnstream.CNCameraSetting;
import com.cnlive.libs.lib_cnstream.CNMicrophoneSetting;
import com.cnlive.libs.lib_cnstream.CNStreamManager;
import com.cnlive.libs.lib_cnstream.CNStreamProfile;
import com.cnlive.libs.lib_cnstream.CNWatermarkSetting;
import com.cnlive.libs.lib_cnstream.base.CNAspectFrameLayout;
import com.cnlive.libs.lib_cnstream.base.CNPLFourCC;
import com.cnlive.libs.lib_cnstream.base.EncodeType;
import com.cnlive.libs.lib_cnstream.base.ICNCameraSetting;
import com.cnlive.libs.lib_cnstream.base.ICNStreamManager;
import com.cnlive.libs.lib_cnstream.base.ICNStreamProfile;
import com.cnlive.libs.lib_cnstream.base.ICNWatermarkSetting;
import com.cnlive.libs.lib_cnstream.callback.CNAudioSourceCallback;
import com.cnlive.libs.lib_cnstream.callback.CNFrameCapturedCallback;
import com.cnlive.libs.lib_cnstream.callback.CNStreamStatusCallback;
import com.cnlive.libs.lib_cnstream.callback.CNStreamingPreviewCallback;
import com.cnlive.libs.lib_cnstream.callback.CNStreamingSessionListener;
import com.cnlive.libs.lib_cnstream.callback.CNStreamingStateChangedListener;
import com.cnlive.libs.lib_cnstream.callback.CNSurfaceTextureCallback;
import com.cnlive.libs.lib_cnstream.model.ActivityConfig;
import com.cnlive.libs.lib_util.dns.CNAndroidDnsServer;
import com.cnlive.libs.lib_util.dns.CNDnsManager;
import com.cnlive.libs.lib_util.dns.CNDnspodFree;
import com.cnlive.libs.lib_util.dns.CNIResolver;
import com.cnlive.libs.lib_util.dns.CNNetworkInfo;
import com.cnlive.libs.lib_util.dns.CNResolver;
import com.qiniu.pili.droid.streaming.StreamingEnv;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.List;
import static android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK;
/**
* Created by Administrator on 2016/11/16.
*/
public class BaseStreamActivity extends Activity implements View.OnClickListener,
View.OnLayoutChangeListener,
CNStreamStatusCallback,
CNStreamingPreviewCallback,
CNSurfaceTextureCallback,
CNAudioSourceCallback,
CameraPreviewFrameView.Listener,
CNStreamingSessionListener,
CNStreamingStateChangedListener,
ICNStreamManager.OnInfoListener,
ICNStreamManager.OnErrorListener {
private static final String TAG = "StreamingBaseActivity";
private String activityId;
private String channelId;
private ImageView flashBtn;
private ImageView flipBtn;
private ImageView closeBtn;
private ImageView beautyBtn;
private SeekBar beautyLevel;
private Button orientationBtn;
protected Button startStreamBtn;
private Button muteBtn;
private Button captureBtn;
private CameraPreviewFrameView cameraPreviewFrameView;
private Screenshooter screenshooter = new Screenshooter();
private boolean isFlashOpened = false;
private boolean mIsNeedFB = false;
private boolean mIsNeedMute = false;
protected boolean mIsRecording = false;
private RotateLayout mRotateLayout;
private boolean isEncOrientationPort = true;
protected String mLogContent = "\n";
protected CNStreamManager mMediaManager;
protected CNCameraSetting mCameraSetting;
protected CNMicrophoneSetting mMicrophoneSetting;
protected CNStreamProfile mProfile;
private CNWatermarkSetting watermarksetting;
private CNAspectFrameLayout afl;
private int orientation = 1;
private boolean mOrientationChanged = false;
protected boolean mIsReady = false;
private int mCurrentZoom = 0;
private int mMaxZoom = 0;
private FBO mFBO = new FBO();
@Override
protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
} else {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
super.onCreate(savedInstanceState);
StreamingEnv.init(getApplicationContext());
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (Config.SCREEN_ORIENTATION == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
isEncOrientationPort = true;
} else if (Config.SCREEN_ORIENTATION == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
isEncOrientationPort = false;
}
setRequestedOrientation(Config.SCREEN_ORIENTATION);
setContentView(R.layout.activity_stream);
CNStreamProfile.AudioProfile aProfile = new CNStreamProfile.AudioProfile(44100, 96 * 1024);
CNStreamProfile.VideoProfile vProfile = new CNStreamProfile.VideoProfile(30, 1000 * 1024, 48);
CNStreamProfile.AVProfile avProfile = new CNStreamProfile.AVProfile(vProfile, aProfile);
initStreamProfile();
initCameraSetting();
mIsNeedFB = true;
initWatermark();
initMicrophoneSetting();
initView();
initData();
setUpMediaManager();
setFocusAreaIndicator();
}
//推流参数配置
private void initStreamProfile() {
mProfile = new CNStreamProfile();
mProfile.setVideoQuality(ICNStreamProfile.VIDEO_QUALITY_MEDIUM2);
mProfile.setAudioQuality(ICNStreamProfile.AUDIO_QUALITY_MEDIUM2);
mProfile.setEncodingSizeLevel(ICNStreamProfile.VIDEO_ENCODING_HEIGHT_480);
mProfile.setEncoderRCMode(ICNStreamProfile.BITRATE_PRIORITY);
mProfile.setDnsManager(getMyDnsManager());
mProfile.setAdaptiveBitrateEnable(true);
mProfile.setFpsControllerEnable(true);
mProfile.setStreamStatusConfig(new CNStreamProfile.StreamStatusConfig(3));
mProfile.setSendingBufferProfile(new CNStreamProfile.SendingBufferProfile(0.2f, 0.8f, 3.0f, 20 * 1000));
}
//摄像头参数配置
private void initCameraSetting() {
mCameraSetting = new CNCameraSetting();
mCameraSetting.setCameraId(CAMERA_FACING_BACK);
mCameraSetting.setContinuousFocusModeEnabled(true);
mCameraSetting.setRecordingHint(false);
mCameraSetting.setCameraFacingId(ICNCameraSetting.CAMERA_FACING_FRONT);
mCameraSetting.setBuiltInFaceBeautyEnabled(true);
mCameraSetting.setResetTouchFocusDelayInMs(3000);
mCameraSetting.setCameraPrvSizeLevel(ICNCameraSetting.SMALL);
mCameraSetting.setCameraPrvSizeRatio(ICNCameraSetting.RATIO_16_9);
mCameraSetting.setFaceBeautySetting(new CNCameraSetting.FaceBeautySetting(1.0f, 1.0f, 0.8f));
}
//麦克风参数配置
private void initMicrophoneSetting() {
mMicrophoneSetting = new CNMicrophoneSetting();
mMicrophoneSetting.setBluetoothSCOEnabled(false);
}
//水印参数配置
private void initWatermark() {
watermarksetting = new CNWatermarkSetting(this);
watermarksetting.setResourceId(R.drawable.cnlive_logo);
watermarksetting.setAlpha(100);
watermarksetting.setSize(ICNWatermarkSetting.SMALL);
watermarksetting.setCustomPosition(0.5f, 0.5f);
}
//初始化推流所需参数
private void initData() {
Intent intent = getIntent();
activityId = intent.getStringExtra("activityId");
channelId = intent.getStringExtra("channelId");
}
//配置推流核心类
private void setUpMediaManager() {
mMediaManager = new CNStreamManager(this, afl, cameraPreviewFrameView, EncodeType.SW_VIDEO_WITH_SW_AUDIO_CODEC); // sw codec
mMediaManager.prepare(mCameraSetting, mMicrophoneSetting, watermarksetting, mProfile);
mMediaManager.setStreamingStateListener(this);
mMediaManager.setSurfaceTextureCallback(this);
mMediaManager.setStreamingSessionListener(this);
mMediaManager.setStreamStatusCallback(this);
mMediaManager.setStreamingPreviewCallback(this);
mMediaManager.setAudioSourceCallback(this);
mMediaManager.setOnErrorListener(this);
mMediaManager.setOnInfoListener(this);
}
@Override
public void onResume() {
super.onResume();
mMediaManager.resume();
}
public void onPause() {
super.onPause();
mIsReady = false;
mMediaManager.pause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMediaManager.destroy();
}
//开始推流
protected void startStreaming() {
mMediaManager.startStream(activityConfig);
startStreamBtn.setText("结束推流");
startStreamBtn.postInvalidate();
mIsRecording = true;
}
//结束推流
protected void stopStreaming() {
mMediaManager.stopStream();
startStreamBtn.setText("开始推流");
startStreamBtn.postInvalidate();
mIsRecording = false;
}
//返回按钮
protected void onBackoffClick() {
new AlertDialog.Builder(BaseStreamActivity.this).setCancelable(true)
.setTitle("结束直播?")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
mMediaManager.stopStream();
mIsRecording = false;
new Handler().postDelayed(new Runnable() {
public void run() {
//execute the task
BaseStreamActivity.this.finish();
}
}, 1000);
}
}).show();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
onBackoffClick();
break;
default:
break;
}
return true;
}
@Override
public boolean onRecordAudioFailedHandled(int err) {
return false;
}
@Override
public boolean onRestartStreamingHandled(int err) {
Log.i(TAG, "onRestartStreamingHandled");
return mMediaManager.startStreaming();
}
@Override
public Camera.Size onPreviewSizeSelected(List<Camera.Size> list) {
Camera.Size size = null;
if (list != null) {
for (Camera.Size s : list) {
if (s.height >= 480) {
size = s;
break;
}
}
}
return size;
}
//对焦
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.i(TAG, "onSingleTapUp X:" + e.getX() + ",Y:" + e.getY());
if (mIsReady) {
setFocusAreaIndicator();
mMediaManager.doSingleTapUp((int) e.getX(), (int) e.getY());
return true;
}
return false;
}
//变焦
@Override
public boolean onZoomValueChanged(float factor) {
if (mIsReady && mMediaManager.isZoomSupported()) {
mCurrentZoom = (int) (mMaxZoom * factor);
mCurrentZoom = Math.min(mCurrentZoom, mMaxZoom);
mCurrentZoom = Math.max(0, mCurrentZoom);
Log.d(TAG, "zoom ongoing, scale: " + mCurrentZoom + ",factor:" + factor + ",maxZoom:" + mMaxZoom);
mMediaManager.setZoomValue(mCurrentZoom);
}
return false;
}
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
Log.i(TAG, "view!!!!:" + v);
}
@Override
public boolean onPreviewFrame(byte[] bytes, int width, int height, int rotation, int fmt, long tsInNanoTime) {
Log.i(TAG, "onPreviewFrame " + width + "x" + height + ",fmt:" + (fmt == CNPLFourCC.FOURCC_I420 ? "I420" : "NV21") + ",ts:" + tsInNanoTime + ",rotation:" + rotation);
return true;
}
@Override
public void onSurfaceCreated() {
Log.i(TAG, "onSurfaceCreated");
mFBO.initialize(this);
}
@Override
public void onSurfaceChanged(int width, int height) {
Log.i(TAG, "onSurfaceChanged width:" + width + ",height:" + height);
mFBO.updateSurfaceSize(width, height);
}
@Override
public void onSurfaceDestroyed() {
Log.i(TAG, "onSurfaceDestroyed");
mFBO.release();
}
@Override
public int onDrawFrame(int texId, int texWidth, int texHeight, float[] transformMatrix) {
int newTexId = mFBO.drawFrame(texId, texWidth, texHeight);
return newTexId;
}
@Override
public void onAudioSourceAvailable(ByteBuffer byteBuffer, int size, long tsInNanoTime, boolean eof) {
}
@Override
public void notifyStreamStatusChanged(final CNStreamProfile.StreamStatus streamStatus) {
}
//推流过程中的状态改变监听
@Override
public void onStateChanged(com.cnlive.libs.lib_cnstream.base.StreamingState streamingState, Object extra) {
Log.i(TAG, "StreamingState streamingState:" + streamingState + ",extra:" + extra);
switch (streamingState) {
case PREPARING:
break;
case READY:
mIsReady = true;
mMaxZoom = mMediaManager.getMaxZoom();
break;
case CONNECTING:
break;
case STREAMING:
break;
case SHUTDOWN:
break;
case IOERROR:
break;
case UNKNOWN:
break;
case SENDING_BUFFER_EMPTY:
break;
case SENDING_BUFFER_FULL:
break;
case AUDIO_RECORDING_FAIL:
break;
case OPEN_CAMERA_FAIL:
Log.e(TAG, "Open Camera Fail. id:" + extra);
break;
case DISCONNECTED:
mLogContent += "DISCONNECTED\n";
break;
case INVALID_STREAMING_URL:
Log.e(TAG, "Invalid streaming url:" + extra);
break;
case UNAUTHORIZED_STREAMING_URL:
Log.e(TAG, "Unauthorized streaming url:" + extra);
mLogContent += "Unauthorized Url\n";
break;
case TORCH_INFO:
if (extra != null) {
final boolean isSupportedTorch = (Boolean) extra;
Log.i(TAG, "isSupportedTorch=" + isSupportedTorch);
}
break;
}
Log.e(TAG, streamingState.toString());
}
//初始化view
private void initView() {
afl = (CNAspectFrameLayout) findViewById(R.id.cameraPreview_afl);
afl.setShowMode(CNAspectFrameLayout.SHOW_MODE.FULL);
cameraPreviewFrameView = (CameraPreviewFrameView) findViewById(R.id.cameraPreview_surfaceView);
cameraPreviewFrameView.setListener(this);
flashBtn = (ImageView) findViewById(R.id.flashBtn);
flashBtn.setOnClickListener(this);
flipBtn = (ImageView) findViewById(R.id.flipBtn);
flipBtn.setOnClickListener(this);
closeBtn = (ImageView) findViewById(R.id.closeBtn);
closeBtn.setOnClickListener(this);
beautyBtn = (ImageView) findViewById(R.id.beautyBtn);
beautyBtn.setOnClickListener(this);
orientationBtn = (Button) findViewById(R.id.orientationBtn);
orientationBtn.setOnClickListener(this);
startStreamBtn = (Button) findViewById(R.id.startStreamBtn);
startStreamBtn.setOnClickListener(this);
muteBtn = (Button) findViewById(R.id.muteBtn);
muteBtn.setOnClickListener(this);
captureBtn = (Button) findViewById(R.id.captureBtn);
captureBtn.setOnClickListener(this);
beautyLevel = (SeekBar) findViewById(R.id.beautyLevel_seekBar);
beautyLevel.setOnSeekBarChangeListener(beautyLevelListener);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.flashBtn:
if (isFlashOpened) {
mMediaManager.turnLightOff();
isFlashOpened = false;
} else {
mMediaManager.turnLightOn();
isFlashOpened = true;
}
break;
case R.id.flipBtn:
mMediaManager.switchCamera();
break;
case R.id.beautyBtn:
mIsNeedFB = !mIsNeedFB;
mMediaManager.setVideoFilterType(mIsNeedFB ? ICNCameraSetting.VIDEO_FILTER_BEAUTY
: ICNCameraSetting.VIDEO_FILTER_NONE);
beautyBtn.setBackgroundResource(mIsNeedFB ? R.drawable.icon_record_meiyan_on : R.drawable.icon_record_meiyan_off);
break;
case R.id.closeBtn:
onBackoffClick();
break;
case R.id.orientationBtn:
mOrientationChanged = !mOrientationChanged;
isEncOrientationPort = !isEncOrientationPort;
mProfile.setEncodingOrientation(isEncOrientationPort ? ICNStreamProfile.PORT : ICNStreamProfile.LAND);
orientation = isEncOrientationPort ? 0 : 1;
mMediaManager.setStreamingProfile(mProfile);
setRequestedOrientation(isEncOrientationPort ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mMediaManager.notifyActivityOrientationChanged();
orientationBtn.setText(isEncOrientationPort ? "横屏" : "竖屏");
break;
case R.id.startStreamBtn:
if (mIsRecording) {
stopStreaming();
} else {
startStreaming();
}
break;
case R.id.muteBtn:
mIsNeedMute = !mIsNeedMute;
mMediaManager.mute(mIsNeedMute);
muteBtn.setText(mIsNeedMute ? "恢复" : "静音");
break;
case R.id.captureBtn:
new Handler().postDelayed(screenshooter, 100);
break;
}
}
//美颜seekbar监听
SeekBar.OnSeekBarChangeListener beautyLevelListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
CNCameraSetting.FaceBeautySetting faceBeautySetting = mCameraSetting.getFaceBeautySetting();
faceBeautySetting.beautyLevel = progress / 100.0f;
faceBeautySetting.whiten = progress / 100.0f;
faceBeautySetting.redden = progress / 100.0f;
mMediaManager.updateFaceBeautySetting(faceBeautySetting);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
protected void setFocusAreaIndicator() {
if (mRotateLayout == null) {
mRotateLayout = (RotateLayout) findViewById(R.id.focus_indicator_rotate_layout);
mMediaManager.setFocusAreaIndicator(mRotateLayout, mRotateLayout.findViewById(R.id.focus_indicator));
}
}
private static CNDnsManager getMyDnsManager() {
CNIResolver r0 = new CNDnspodFree();
CNIResolver r1 = CNAndroidDnsServer.defaultResolver();
CNIResolver r2 = null;
try {
r2 = new CNResolver(InetAddress.getByName("119.29.29.29"));
} catch (IOException ex) {
ex.printStackTrace();
}
return new CNDnsManager(CNNetworkInfo.normal, new CNIResolver[]{r0, r1, r2});
}
//传入推流所需参数
ActivityConfig activityConfig = new ActivityConfig() {
@Override
public String getActivityID() {
return activityId;
}
@Override
public String getActivityName() {
return " ";
}
@Override
public int getActivityStatus() {
return 0;
}
@Override
public int getIsHorizontalScreen() {
return orientation;
}
@Override
public String getChannelID() {
return channelId;
}
@Override
public String getStartTime() {
return " ";
}
@Override
public String getEndTime() {
return " ";
}
@Override
public String getCoverImgUrl() {
return " ";
}
@Override
public String getActivityCategory() {
return " ";
}
@Override
public String getExtensions() {
return " ";
}
@Override
public String getUuid() {
return " ";
}
@Override
public String getCallbackAcitvityStatushttpPostUrl() {
return " ";
}
};
//推流过程中的错误监听
@Override
public void onError(int what, String extra) {
stopStreaming();
Log.e(TAG, what + " " + extra);
}
//推流过程中返回的信息
@Override
public void onInfo(int what, String extra) {
Log.e(TAG, what + " " + extra);
}
private class Screenshooter implements Runnable {
@Override
public void run() {
final String fileName = "PLStreaming_" + System.currentTimeMillis() + ".jpg";
mMediaManager.captureFrame(100, 100, new CNFrameCapturedCallback() {
private Bitmap bitmap;
@Override
public void onFrameCaptured(Bitmap bmp) {
if (bmp == null) {
return;
}
bitmap = bmp;
new Thread(new Runnable() {
@Override
public void run() {
try {
saveToSDCard(fileName, bitmap);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
}
}
}).start();
}
});
}
}
private void saveToSDCard(String filename, Bitmap bmp) throws IOException {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory(), filename);
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(file));
bmp.compress(Bitmap.CompressFormat.PNG, 90, bos);
bmp.recycle();
bmp = null;
} finally {
if (bos != null) bos.close();
}
final String info = "Save frame to:" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + filename;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(BaseStreamActivity.this, info, Toast.LENGTH_LONG).show();
}
});
}
}
}