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;
|
2025-09-19 19:13:20 +08:00
|
|
|
float vol;
|
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-09-19 19:13:20 +08:00
|
|
|
this.vol = vol;
|
2025-07-28 19:14:00 +08:00
|
|
|
setSurfaceTextureListener(this);
|
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);
|
|
|
|
|
}
|
2025-08-22 23:08:48 +08:00
|
|
|
//ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 0);
|
2023-12-01 16:17:06 +08:00
|
|
|
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);
|
2026-01-23 15:14:23 +08:00
|
|
|
if(Util.isAudioLoss) ijkPlayer.setVolume(0, 0);
|
|
|
|
|
else 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);
|
2025-08-22 23:08:48 +08:00
|
|
|
if(getAlpha() < 0.25) {
|
|
|
|
|
ijkPlayer.pause();
|
2025-12-23 16:40:05 +08:00
|
|
|
var aaa = seek<1000 ? 0 : seek;
|
|
|
|
|
ijkPlayer.seekTo(aaa);
|
|
|
|
|
ijkPlayer.pause();
|
|
|
|
|
ijkPlayer.seekTo(aaa);
|
2025-08-22 23:08:48 +08:00
|
|
|
} else if(seek>=1000) ijkPlayer.seekTo(seek);
|
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);
|
2025-08-22 23:08:48 +08:00
|
|
|
Util.println(" Seek "+seek);
|
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() {
|
|
|
|
|
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
|
|
|
}
|
2023-11-09 08:37:59 +08:00
|
|
|
}
|