Skip to content

Commit 63f78f2

Browse files
committed
增加rtsp/rtmp直播流截图
增加rtsp/rtmp直播流截图
1 parent 81b7ee5 commit 63f78f2

File tree

10 files changed

+101
-9
lines changed

10 files changed

+101
-9
lines changed

OnLive/src/main/java/com/frank/living/activity/RtspLiveActivity.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.frank.living.activity;
22

3+
import android.Manifest;
4+
import android.content.pm.PackageManager;
5+
import android.graphics.Bitmap;
36
import android.os.Bundle;
47

58
import androidx.appcompat.app.AppCompatActivity;
69

10+
import android.os.Environment;
711
import android.util.Log;
812
import android.view.View;
913
import android.widget.ImageButton;
@@ -12,10 +16,14 @@
1216
import com.frank.living.R;
1317
import com.frank.living.listener.IjkPlayerListener;
1418

19+
import androidx.core.app.ActivityCompat;
1520
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
1621

22+
import com.frank.living.util.PhotoUtil;
1723
import com.frank.living.widget.IjkVideoView;
1824

25+
import java.io.File;
26+
1927
public class RtspLiveActivity extends AppCompatActivity implements IjkPlayerListener, View.OnClickListener {
2028

2129
private final static String TAG = RtspLiveActivity.class.getSimpleName();
@@ -26,6 +34,7 @@ public class RtspLiveActivity extends AppCompatActivity implements IjkPlayerList
2634
private ImageButton btnSound;
2735
private boolean isPause;
2836
private boolean isSilence;
37+
private String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
2938

3039
// private final static String url = "rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov";
3140
private final static String url = "rtmp://58.200.131.2:1935/livetv/hunantv";
@@ -35,6 +44,7 @@ protected void onCreate(Bundle savedInstanceState) {
3544
super.onCreate(savedInstanceState);
3645
setContentView(R.layout.activity_live);
3746

47+
requestPermission();
3848
init();
3949

4050
}
@@ -54,7 +64,14 @@ private void init() {
5464
btnPlay.setOnClickListener(this);
5565
btnSound = findViewById(R.id.btn_sound);
5666
btnSound.setOnClickListener(this);
67+
ImageButton btnScreenShot = findViewById(R.id.btn_screenshot);
68+
btnScreenShot.setOnClickListener(this);
69+
}
5770

71+
private void requestPermission() {
72+
if (ActivityCompat.checkSelfPermission(this, permissions[0]) != PackageManager.PERMISSION_GRANTED) {
73+
ActivityCompat.requestPermissions(this, permissions, 1234);
74+
}
5875
}
5976

6077
private void initOptions() {
@@ -111,6 +128,16 @@ public void onClick(View v) {
111128
btnSound.setBackgroundResource(R.drawable.ic_silence);
112129
}
113130
break;
131+
case R.id.btn_screenshot:
132+
if (mVideoView != null) {
133+
Bitmap currentFrame = mVideoView.getCurrentFrame();
134+
if (currentFrame != null) {
135+
String photoName = "img_" + System.currentTimeMillis() + ".jpg";
136+
String photoPath = Environment.getExternalStorageDirectory().getPath() + File.separator + photoName;
137+
PhotoUtil.savePhoto(currentFrame, photoPath, this);
138+
}
139+
}
140+
break;
114141
default:
115142
break;
116143
}

OnLive/src/main/java/com/frank/living/config/Settings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public boolean getEnableSurfaceView() {
8383

8484
public boolean getEnableTextureView() {
8585
String key = mAppContext.getString(R.string.pref_key_enable_texture_view);
86-
return mSharedPreferences.getBoolean(key, false);
86+
return mSharedPreferences.getBoolean(key, true);
8787
}
8888

8989
public boolean getEnableDetachedSurfaceTextureView() {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.frank.living.util;
2+
3+
import android.content.Context;
4+
import android.graphics.Bitmap;
5+
import android.os.Looper;
6+
import android.text.TextUtils;
7+
import android.widget.Toast;
8+
9+
import java.io.FileNotFoundException;
10+
import java.io.FileOutputStream;
11+
import java.io.IOException;
12+
13+
/**
14+
* 图片工具类
15+
* Created by frank on 2019/12/31
16+
*/
17+
18+
public class PhotoUtil {
19+
20+
public static void savePhoto(Bitmap bitmap, String path, Context context) {
21+
savePhoto(bitmap, path, context, 100);
22+
}
23+
24+
public static void savePhoto(Bitmap bitmap, String path, Context context, int quality) {
25+
if (bitmap == null || TextUtils.isEmpty(path) || context == null) {
26+
return;
27+
}
28+
if (quality <= 0 || quality > 100) {
29+
quality = 100;
30+
}
31+
FileOutputStream fileOutputStream = null;
32+
try {
33+
fileOutputStream = new FileOutputStream(path);
34+
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);
35+
fileOutputStream.flush();
36+
if (Looper.myLooper() == Looper.getMainLooper()) {
37+
Toast.makeText(context.getApplicationContext(), "save success:" + path, Toast.LENGTH_SHORT).show();
38+
}
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
} finally {
42+
if (fileOutputStream != null) {
43+
try {
44+
fileOutputStream.close();
45+
} catch (IOException e) {
46+
e.printStackTrace();
47+
}
48+
}
49+
}
50+
}
51+
52+
}

OnLive/src/main/java/com/frank/living/widget/IjkVideoView.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.content.Context;
2222
import android.content.DialogInterface;
2323
import android.content.res.Resources;
24+
import android.graphics.Bitmap;
2425
import android.media.AudioManager;
2526
import android.media.MediaPlayer;
2627
import android.net.Uri;
@@ -335,12 +336,7 @@ private void openVideo() {
335336
// target state that was there before.
336337
mCurrentState = STATE_PREPARING;
337338
attachMediaController();
338-
} catch (IOException ex) {
339-
Log.w(TAG, "Unable to open content: " + mUri, ex);
340-
mCurrentState = STATE_ERROR;
341-
mTargetState = STATE_ERROR;
342-
mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
343-
} catch (IllegalArgumentException ex) {
339+
} catch (IOException | IllegalArgumentException ex) {
344340
Log.w(TAG, "Unable to open content: " + mUri, ex);
345341
mCurrentState = STATE_ERROR;
346342
mTargetState = STATE_ERROR;
@@ -905,7 +901,7 @@ public int toggleAspectRatio() {
905901
public static final int RENDER_SURFACE_VIEW = 1;
906902
public static final int RENDER_TEXTURE_VIEW = 2;
907903

908-
private List<Integer> mAllRenders = new ArrayList<Integer>();
904+
private List<Integer> mAllRenders = new ArrayList<>();
909905
private int mCurrentRenderIndex = 0;
910906
private int mCurrentRender = RENDER_NONE;
911907

@@ -1199,4 +1195,11 @@ public void setRenderViewGone(){
11991195
}
12001196
}
12011197

1198+
public Bitmap getCurrentFrame() {
1199+
if (mRenderView instanceof TextureRenderView) {
1200+
return ((TextureRenderView) mRenderView).getBitmap();
1201+
}
1202+
return null;
1203+
}
1204+
12021205
}
3.58 KB
Loading
-8.57 KB
Binary file not shown.
-9.24 KB
Binary file not shown.
-3.7 KB
Binary file not shown.
-4.33 KB
Binary file not shown.

OnLive/src/main/res/layout/activity_live.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,21 @@
3232
android:layout_marginEnd="20dp"
3333
android:layout_marginBottom="10dp"/>
3434

35+
<ImageButton
36+
android:id="@+id/btn_screenshot"
37+
android:layout_width="30dp"
38+
android:layout_height="30dp"
39+
android:background="@drawable/ic_screen_shot"
40+
android:layout_alignParentBottom="true"
41+
android:layout_toStartOf="@+id/btn_sound"
42+
android:layout_marginEnd="30dp"
43+
android:layout_marginBottom="10dp"/>
44+
3545
<TableLayout
3646
android:id="@+id/hud_view"
3747
android:layout_width="wrap_content"
3848
android:layout_height="wrap_content"
39-
android:layout_gravity="right|center_vertical"
49+
android:layout_gravity="end|center_vertical"
4050
android:background="@color/ijk_transparent_dark"
4151
android:padding="8dp"
4252
android:visibility="gone"/>

0 commit comments

Comments
 (0)