From f83743d53e0cbae721b603f8ea0ec6d415ac88ea Mon Sep 17 00:00:00 2001 From: FongMi Date: Sun, 1 Jan 2023 15:05:46 +0800 Subject: [PATCH] Fix ijk subtitle show --- .../ijk/media/player/ui/IjkVideoView.java | 27 ++++++------------- .../ijk/media/player/ui/SubtitleView.java | 6 ----- .../src/main/res/layout/ijk_player_view.xml | 22 +++++++++++++++ 3 files changed, 30 insertions(+), 25 deletions(-) create mode 100644 ijkplayer/src/main/res/layout/ijk_player_view.xml diff --git a/ijkplayer/src/main/java/tv/danmaku/ijk/media/player/ui/IjkVideoView.java b/ijkplayer/src/main/java/tv/danmaku/ijk/media/player/ui/IjkVideoView.java index 87b3efe21..5390f80bf 100644 --- a/ijkplayer/src/main/java/tv/danmaku/ijk/media/player/ui/IjkVideoView.java +++ b/ijkplayer/src/main/java/tv/danmaku/ijk/media/player/ui/IjkVideoView.java @@ -9,6 +9,7 @@ import android.os.Build; import android.util.AttributeSet; import android.util.Log; import android.view.Gravity; +import android.view.LayoutInflater; import android.widget.FrameLayout; import android.widget.MediaController; @@ -20,6 +21,7 @@ import java.util.Map; import tv.danmaku.ijk.media.player.IMediaPlayer; import tv.danmaku.ijk.media.player.IjkMediaPlayer; import tv.danmaku.ijk.media.player.IjkTimedText; +import tv.danmaku.ijk.media.player.R; public class IjkVideoView extends FrameLayout implements MediaController.MediaPlayerControl { @@ -73,60 +75,47 @@ public class IjkVideoView extends FrameLayout implements MediaController.MediaPl private int mVideoSarNum; private int mVideoSarDen; + private FrameLayout contentFrame; private SubtitleView subtitleView; public IjkVideoView(Context context) { super(context); initVideoView(context); - initSubtitleView(context); } public IjkVideoView(Context context, AttributeSet attrs) { super(context, attrs); initVideoView(context); - initSubtitleView(context); } public IjkVideoView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initVideoView(context); - initSubtitleView(context); - } - - public IjkVideoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { - super(context, attrs, defStyleAttr, defStyleRes); - initVideoView(context); - initSubtitleView(context); } private void initVideoView(Context context) { + LayoutInflater.from(context).inflate(R.layout.ijk_player_view, this); mAppContext = context.getApplicationContext(); + contentFrame = findViewById(R.id.ijk_content_frame); + subtitleView = findViewById(R.id.ijk_subtitle); mVideoWidth = 0; mVideoHeight = 0; mCurrentState = STATE_IDLE; mTargetState = STATE_IDLE; } - private void initSubtitleView(Context context) { - subtitleView = new SubtitleView(context); - LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM); - int space = Math.round(Utils.dp2px(context, 16)); - params.setMargins(space, 0, space, space); - addView(subtitleView, params); - } - private void setRenderView(IRenderView renderView) { clearRender(); mRenderView = renderView; setResizeMode(mCurrentAspectRatio); - addView(mRenderView.getView(), new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER)); + contentFrame.addView(mRenderView.getView(), 0, new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER)); mRenderView.addRenderCallback(mSHCallback); mRenderView.setVideoRotation(mVideoRotationDegree); } private void clearRender() { if (mRenderView != null) { - removeView(mRenderView.getView()); + contentFrame.removeView(mRenderView.getView()); mRenderView.removeRenderCallback(mSHCallback); mRenderView = null; } diff --git a/ijkplayer/src/main/java/tv/danmaku/ijk/media/player/ui/SubtitleView.java b/ijkplayer/src/main/java/tv/danmaku/ijk/media/player/ui/SubtitleView.java index e51ba96a8..afab69f27 100644 --- a/ijkplayer/src/main/java/tv/danmaku/ijk/media/player/ui/SubtitleView.java +++ b/ijkplayer/src/main/java/tv/danmaku/ijk/media/player/ui/SubtitleView.java @@ -4,9 +4,7 @@ import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; -import android.graphics.Typeface; import android.util.AttributeSet; -import android.view.Gravity; import android.widget.TextView; import androidx.annotation.Nullable; @@ -33,10 +31,6 @@ public class SubtitleView extends TextView { private void init() { strokeWidth = Utils.dp2px(getContext(), 0.6f); - setTypeface(Typeface.DEFAULT_BOLD); - setGravity(Gravity.CENTER); - setTextSize(16); - setZ(99); } @Override diff --git a/ijkplayer/src/main/res/layout/ijk_player_view.xml b/ijkplayer/src/main/res/layout/ijk_player_view.xml new file mode 100644 index 000000000..de84acd22 --- /dev/null +++ b/ijkplayer/src/main/res/layout/ijk_player_view.xml @@ -0,0 +1,22 @@ + + + + + + + + +