diff --git a/OnLive/src/main/java/com/frank/living/activity/RtspLiveActivity.java b/OnLive/src/main/java/com/frank/living/activity/RtspLiveActivity.java index 3d83d14..75cfb01 100644 --- a/OnLive/src/main/java/com/frank/living/activity/RtspLiveActivity.java +++ b/OnLive/src/main/java/com/frank/living/activity/RtspLiveActivity.java @@ -1,9 +1,13 @@ package com.frank.living.activity; +import android.Manifest; +import android.content.pm.PackageManager; +import android.graphics.Bitmap; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; +import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.ImageButton; @@ -12,10 +16,14 @@ import android.widget.TableLayout; import com.frank.living.R; import com.frank.living.listener.IjkPlayerListener; +import androidx.core.app.ActivityCompat; import tv.danmaku.ijk.media.player.IjkMediaPlayer; +import com.frank.living.util.PhotoUtil; import com.frank.living.widget.IjkVideoView; +import java.io.File; + public class RtspLiveActivity extends AppCompatActivity implements IjkPlayerListener, View.OnClickListener { private final static String TAG = RtspLiveActivity.class.getSimpleName(); @@ -26,6 +34,7 @@ public class RtspLiveActivity extends AppCompatActivity implements IjkPlayerList private ImageButton btnSound; private boolean isPause; 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 = "rtmp://58.200.131.2:1935/livetv/hunantv"; @@ -35,6 +44,7 @@ public class RtspLiveActivity extends AppCompatActivity implements IjkPlayerList super.onCreate(savedInstanceState); setContentView(R.layout.activity_live); + requestPermission(); init(); } @@ -54,7 +64,14 @@ public class RtspLiveActivity extends AppCompatActivity implements IjkPlayerList btnPlay.setOnClickListener(this); btnSound = findViewById(R.id.btn_sound); 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() { @@ -111,6 +128,16 @@ public class RtspLiveActivity extends AppCompatActivity implements IjkPlayerList btnSound.setBackgroundResource(R.drawable.ic_silence); } 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: break; } diff --git a/OnLive/src/main/java/com/frank/living/config/Settings.java b/OnLive/src/main/java/com/frank/living/config/Settings.java index ba1e3c2..0a8ed0d 100644 --- a/OnLive/src/main/java/com/frank/living/config/Settings.java +++ b/OnLive/src/main/java/com/frank/living/config/Settings.java @@ -83,7 +83,7 @@ public class Settings { public boolean getEnableTextureView() { String key = mAppContext.getString(R.string.pref_key_enable_texture_view); - return mSharedPreferences.getBoolean(key, false); + return mSharedPreferences.getBoolean(key, true); } public boolean getEnableDetachedSurfaceTextureView() { diff --git a/OnLive/src/main/java/com/frank/living/util/PhotoUtil.java b/OnLive/src/main/java/com/frank/living/util/PhotoUtil.java new file mode 100644 index 0000000..69fb799 --- /dev/null +++ b/OnLive/src/main/java/com/frank/living/util/PhotoUtil.java @@ -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(); + } + } + } + } + +} diff --git a/OnLive/src/main/java/com/frank/living/widget/IjkVideoView.java b/OnLive/src/main/java/com/frank/living/widget/IjkVideoView.java index 7c29597..68f1b01 100644 --- a/OnLive/src/main/java/com/frank/living/widget/IjkVideoView.java +++ b/OnLive/src/main/java/com/frank/living/widget/IjkVideoView.java @@ -21,6 +21,7 @@ import android.annotation.TargetApi; import android.content.Context; import android.content.DialogInterface; import android.content.res.Resources; +import android.graphics.Bitmap; import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; @@ -335,12 +336,7 @@ public class IjkVideoView extends FrameLayout implements MediaController.MediaPl // target state that was there before. mCurrentState = STATE_PREPARING; attachMediaController(); - } catch (IOException 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) { + } catch (IOException | IllegalArgumentException ex) { Log.w(TAG, "Unable to open content: " + mUri, ex); mCurrentState = 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_TEXTURE_VIEW = 2; - private List mAllRenders = new ArrayList(); + private List mAllRenders = new ArrayList<>(); private int mCurrentRenderIndex = 0; 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; + } + } diff --git a/OnLive/src/main/res/drawable-xhdpi/ic_screen_shot.png b/OnLive/src/main/res/drawable-xhdpi/ic_screen_shot.png new file mode 100644 index 0000000..3cb3fc3 Binary files /dev/null and b/OnLive/src/main/res/drawable-xhdpi/ic_screen_shot.png differ diff --git a/OnLive/src/main/res/drawable-xxhdpi/ic_pause.png b/OnLive/src/main/res/drawable-xxhdpi/ic_pause.png deleted file mode 100644 index dedce6a..0000000 Binary files a/OnLive/src/main/res/drawable-xxhdpi/ic_pause.png and /dev/null differ diff --git a/OnLive/src/main/res/drawable-xxhdpi/ic_play.png b/OnLive/src/main/res/drawable-xxhdpi/ic_play.png deleted file mode 100644 index 72dc5a4..0000000 Binary files a/OnLive/src/main/res/drawable-xxhdpi/ic_play.png and /dev/null differ diff --git a/OnLive/src/main/res/drawable-xxhdpi/ic_silence.png b/OnLive/src/main/res/drawable-xxhdpi/ic_silence.png deleted file mode 100644 index 3ecb74f..0000000 Binary files a/OnLive/src/main/res/drawable-xxhdpi/ic_silence.png and /dev/null differ diff --git a/OnLive/src/main/res/drawable-xxhdpi/ic_sound.png b/OnLive/src/main/res/drawable-xxhdpi/ic_sound.png deleted file mode 100644 index 8a4742a..0000000 Binary files a/OnLive/src/main/res/drawable-xxhdpi/ic_sound.png and /dev/null differ diff --git a/OnLive/src/main/res/layout/activity_live.xml b/OnLive/src/main/res/layout/activity_live.xml index c62b2b7..b0b9f3c 100644 --- a/OnLive/src/main/res/layout/activity_live.xml +++ b/OnLive/src/main/res/layout/activity_live.xml @@ -32,11 +32,21 @@ android:layout_marginEnd="20dp" android:layout_marginBottom="10dp"/> + +