Android/XixunPlayer/app/src/main/java/com/xixun/xixunplayer/SrcVideo.java

97 lines
3.5 KiB
Java
Raw Normal View History

2023-11-09 08:37:59 +08:00
package com.xixun.xixunplayer;
2024-05-09 20:30:22 +08:00
import android.annotation.SuppressLint;
2023-11-09 08:37:59 +08:00
import android.content.Context;
2024-01-24 20:17:59 +08:00
import android.graphics.SurfaceTexture;
import android.view.Surface;
import android.view.TextureView;
2023-11-09 08:37:59 +08:00
2024-01-24 20:17:59 +08:00
import androidx.annotation.NonNull;
2023-12-11 15:17:48 +08:00
import tv.danmaku.ijk.media.player.IMediaPlayer;
2023-12-01 16:17:06 +08:00
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
2023-11-09 08:37:59 +08:00
2024-05-09 20:30:22 +08:00
@SuppressLint("ViewConstructor")
2024-02-02 19:34:33 +08:00
public class SrcVideo extends TextureView implements TextureView.SurfaceTextureListener {
2023-11-09 08:37:59 +08:00
2023-12-01 16:17:06 +08:00
IjkMediaPlayer ijkPlayer;
2025-07-28 19:14:00 +08:00
Surface surface;
2024-01-24 20:17:59 +08:00
long bitRate;
2023-12-01 16:17:06 +08:00
2025-07-28 19:14:00 +08:00
public SrcVideo(Context context, String path, float vol, int dur, long seek, boolean useSW) {
2023-11-09 08:37:59 +08:00
super(context);
2025-07-28 19:14:00 +08:00
setSurfaceTextureListener(this);
Util.println(" video new");
2023-12-01 16:17:06 +08:00
ijkPlayer = new IjkMediaPlayer();
2025-07-28 19:14:00 +08:00
if(! useSW) {
ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-avc", 1);
ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-hevc", 1);
}
2023-12-01 16:17:06 +08:00
ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 0);
ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "analyzeduration", 1);
2025-07-28 19:14:00 +08:00
ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_CODEC, "skip_loop_filter", 12);
2023-12-01 16:17:06 +08:00
try {
ijkPlayer.setDataSource(path);
ijkPlayer.setLooping(true);
ijkPlayer.setVolume(vol, vol);
2023-12-11 15:17:48 +08:00
ijkPlayer.setOnPreparedListener((IMediaPlayer var1)->{
2024-05-09 20:30:22 +08:00
ijkPlayer.setOnPreparedListener(null);
2024-01-24 20:17:59 +08:00
bitRate = ijkPlayer.getBitRate();
2025-07-28 19:14:00 +08:00
var diff = dur - ijkPlayer.getDuration();
if(diff>0 && diff<=1000) ijkPlayer.setLooping(false);
if(seek>=1000) {
ijkPlayer.seekTo(seek);
Util.println(" Seek "+seek);
2023-12-11 15:17:48 +08:00
}
2025-07-28 19:14:00 +08:00
if(isShown()) ijkPlayer.start();
2023-12-11 15:17:48 +08:00
});
2024-05-09 20:30:22 +08:00
ijkPlayer.setOnErrorListener((IMediaPlayer var1, int var2, int var3)->{
Util.println(" Video Error: "+var1+" "+var2+" "+var3);
return true;
});
2023-12-01 16:17:06 +08:00
ijkPlayer.prepareAsync();
} catch (Throwable e) {
2024-01-24 20:17:59 +08:00
Util.makeText(getContext(), Util.toStr(e)).show();
Util.printStackTrace(e);
2023-12-01 16:17:06 +08:00
ijkPlayer = null;
}
2023-11-09 08:37:59 +08:00
}
2025-07-28 19:14:00 +08:00
2023-12-11 15:17:48 +08:00
@Override
2024-01-24 20:17:59 +08:00
public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
2025-07-28 19:14:00 +08:00
Util.println(" SurfaceTexture Available "+(ijkPlayer==null?"ijkPlayer==null":""));
this.surface = new Surface(surface);
ijkPlayer.setSurface(this.surface);
2023-12-11 15:17:48 +08:00
}
@Override
2024-03-26 15:15:57 +08:00
public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {}
2023-12-11 15:17:48 +08:00
@Override
2024-01-24 20:17:59 +08:00
public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
2025-07-28 19:14:00 +08:00
Util.println(" SurfaceTexture Destroyed");
2024-01-24 20:17:59 +08:00
return false;
}
@Override
2024-03-26 15:15:57 +08:00
public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {}
2023-11-09 08:37:59 +08:00
2023-12-01 16:17:06 +08:00
void release() {
2025-07-28 19:14:00 +08:00
Util.println(" video release");
2023-12-01 16:17:06 +08:00
if(ijkPlayer!=null) {
ijkPlayer.release();
ijkPlayer = null;
2023-11-09 08:37:59 +08:00
}
2025-07-28 19:14:00 +08:00
if(surface!=null) {
surface.release();
surface = null;
2023-12-11 15:17:48 +08:00
}
2023-12-01 16:17:06 +08:00
}
@Override
public void onVisibilityAggregated(boolean isVisible) {
super.onVisibilityAggregated(isVisible);
2025-07-28 19:14:00 +08:00
if(isVisible) ijkPlayer.start();
2024-11-11 18:22:45 +08:00
else {
2025-07-28 19:14:00 +08:00
ijkPlayer.pause();
ijkPlayer.seekTo(0);
2024-11-11 18:22:45 +08:00
}
2023-11-09 08:37:59 +08:00
}
}