增加rtsp/rtmp直播流截图

增加rtsp/rtmp直播流截图
pull/107/head
xufulong 5 years ago
parent 81b7ee58d0
commit 63f78f2c58
  1. 27
      OnLive/src/main/java/com/frank/living/activity/RtspLiveActivity.java
  2. 2
      OnLive/src/main/java/com/frank/living/config/Settings.java
  3. 52
      OnLive/src/main/java/com/frank/living/util/PhotoUtil.java
  4. 17
      OnLive/src/main/java/com/frank/living/widget/IjkVideoView.java
  5. BIN
      OnLive/src/main/res/drawable-xhdpi/ic_screen_shot.png
  6. BIN
      OnLive/src/main/res/drawable-xxhdpi/ic_pause.png
  7. BIN
      OnLive/src/main/res/drawable-xxhdpi/ic_play.png
  8. BIN
      OnLive/src/main/res/drawable-xxhdpi/ic_silence.png
  9. BIN
      OnLive/src/main/res/drawable-xxhdpi/ic_sound.png
  10. 12
      OnLive/src/main/res/layout/activity_live.xml

@ -1,9 +1,13 @@
package com.frank.living.activity; package com.frank.living.activity;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle; import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.os.Environment;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.ImageButton; import android.widget.ImageButton;
@ -12,10 +16,14 @@ import android.widget.TableLayout;
import com.frank.living.R; import com.frank.living.R;
import com.frank.living.listener.IjkPlayerListener; import com.frank.living.listener.IjkPlayerListener;
import androidx.core.app.ActivityCompat;
import tv.danmaku.ijk.media.player.IjkMediaPlayer; import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import com.frank.living.util.PhotoUtil;
import com.frank.living.widget.IjkVideoView; import com.frank.living.widget.IjkVideoView;
import java.io.File;
public class RtspLiveActivity extends AppCompatActivity implements IjkPlayerListener, View.OnClickListener { public class RtspLiveActivity extends AppCompatActivity implements IjkPlayerListener, View.OnClickListener {
private final static String TAG = RtspLiveActivity.class.getSimpleName(); private final static String TAG = RtspLiveActivity.class.getSimpleName();
@ -26,6 +34,7 @@ public class RtspLiveActivity extends AppCompatActivity implements IjkPlayerList
private ImageButton btnSound; private ImageButton btnSound;
private boolean isPause; private boolean isPause;
private boolean isSilence; private boolean isSilence;
private String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
// private final static String url = "rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov"; // private final static String url = "rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov";
private final static String url = "rtmp://58.200.131.2:1935/livetv/hunantv"; private final static String url = "rtmp://58.200.131.2:1935/livetv/hunantv";
@ -35,6 +44,7 @@ public class RtspLiveActivity extends AppCompatActivity implements IjkPlayerList
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live); setContentView(R.layout.activity_live);
requestPermission();
init(); init();
} }
@ -54,7 +64,14 @@ public class RtspLiveActivity extends AppCompatActivity implements IjkPlayerList
btnPlay.setOnClickListener(this); btnPlay.setOnClickListener(this);
btnSound = findViewById(R.id.btn_sound); btnSound = findViewById(R.id.btn_sound);
btnSound.setOnClickListener(this); btnSound.setOnClickListener(this);
ImageButton btnScreenShot = findViewById(R.id.btn_screenshot);
btnScreenShot.setOnClickListener(this);
}
private void requestPermission() {
if (ActivityCompat.checkSelfPermission(this, permissions[0]) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, permissions, 1234);
}
} }
private void initOptions() { private void initOptions() {
@ -111,6 +128,16 @@ public class RtspLiveActivity extends AppCompatActivity implements IjkPlayerList
btnSound.setBackgroundResource(R.drawable.ic_silence); btnSound.setBackgroundResource(R.drawable.ic_silence);
} }
break; break;
case R.id.btn_screenshot:
if (mVideoView != null) {
Bitmap currentFrame = mVideoView.getCurrentFrame();
if (currentFrame != null) {
String photoName = "img_" + System.currentTimeMillis() + ".jpg";
String photoPath = Environment.getExternalStorageDirectory().getPath() + File.separator + photoName;
PhotoUtil.savePhoto(currentFrame, photoPath, this);
}
}
break;
default: default:
break; break;
} }

@ -83,7 +83,7 @@ public class Settings {
public boolean getEnableTextureView() { public boolean getEnableTextureView() {
String key = mAppContext.getString(R.string.pref_key_enable_texture_view); String key = mAppContext.getString(R.string.pref_key_enable_texture_view);
return mSharedPreferences.getBoolean(key, false); return mSharedPreferences.getBoolean(key, true);
} }
public boolean getEnableDetachedSurfaceTextureView() { public boolean getEnableDetachedSurfaceTextureView() {

@ -0,0 +1,52 @@
package com.frank.living.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Looper;
import android.text.TextUtils;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 图片工具类
* Created by frank on 2019/12/31
*/
public class PhotoUtil {
public static void savePhoto(Bitmap bitmap, String path, Context context) {
savePhoto(bitmap, path, context, 100);
}
public static void savePhoto(Bitmap bitmap, String path, Context context, int quality) {
if (bitmap == null || TextUtils.isEmpty(path) || context == null) {
return;
}
if (quality <= 0 || quality > 100) {
quality = 100;
}
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);
fileOutputStream.flush();
if (Looper.myLooper() == Looper.getMainLooper()) {
Toast.makeText(context.getApplicationContext(), "save success:" + path, Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

@ -21,6 +21,7 @@ import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.net.Uri; import android.net.Uri;
@ -335,12 +336,7 @@ public class IjkVideoView extends FrameLayout implements MediaController.MediaPl
// target state that was there before. // target state that was there before.
mCurrentState = STATE_PREPARING; mCurrentState = STATE_PREPARING;
attachMediaController(); attachMediaController();
} catch (IOException ex) { } catch (IOException | IllegalArgumentException ex) {
Log.w(TAG, "Unable to open content: " + mUri, ex);
mCurrentState = STATE_ERROR;
mTargetState = STATE_ERROR;
mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
} catch (IllegalArgumentException ex) {
Log.w(TAG, "Unable to open content: " + mUri, ex); Log.w(TAG, "Unable to open content: " + mUri, ex);
mCurrentState = STATE_ERROR; mCurrentState = STATE_ERROR;
mTargetState = STATE_ERROR; mTargetState = STATE_ERROR;
@ -905,7 +901,7 @@ public class IjkVideoView extends FrameLayout implements MediaController.MediaPl
public static final int RENDER_SURFACE_VIEW = 1; public static final int RENDER_SURFACE_VIEW = 1;
public static final int RENDER_TEXTURE_VIEW = 2; public static final int RENDER_TEXTURE_VIEW = 2;
private List<Integer> mAllRenders = new ArrayList<Integer>(); private List<Integer> mAllRenders = new ArrayList<>();
private int mCurrentRenderIndex = 0; private int mCurrentRenderIndex = 0;
private int mCurrentRender = RENDER_NONE; private int mCurrentRender = RENDER_NONE;
@ -1199,4 +1195,11 @@ public class IjkVideoView extends FrameLayout implements MediaController.MediaPl
} }
} }
public Bitmap getCurrentFrame() {
if (mRenderView instanceof TextureRenderView) {
return ((TextureRenderView) mRenderView).getBitmap();
}
return null;
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 KiB

@ -32,11 +32,21 @@
android:layout_marginEnd="20dp" android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"/> android:layout_marginBottom="10dp"/>
<ImageButton
android:id="@+id/btn_screenshot"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ic_screen_shot"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/btn_sound"
android:layout_marginEnd="30dp"
android:layout_marginBottom="10dp"/>
<TableLayout <TableLayout
android:id="@+id/hud_view" android:id="@+id/hud_view"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical" android:layout_gravity="end|center_vertical"
android:background="@color/ijk_transparent_dark" android:background="@color/ijk_transparent_dark"
android:padding="8dp" android:padding="8dp"
android:visibility="gone"/> android:visibility="gone"/>

Loading…
Cancel
Save