97 lines
3.5 KiB
Java
97 lines
3.5 KiB
Java
package com.xixun.xixunplayer;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.content.Context;
|
|
import android.graphics.SurfaceTexture;
|
|
import android.view.Surface;
|
|
import android.view.TextureView;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import tv.danmaku.ijk.media.player.IMediaPlayer;
|
|
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
|
|
|
|
@SuppressLint("ViewConstructor")
|
|
public class SrcVideo extends TextureView implements TextureView.SurfaceTextureListener {
|
|
|
|
IjkMediaPlayer ijkPlayer;
|
|
Surface surface;
|
|
long bitRate;
|
|
|
|
public SrcVideo(Context context, String path, float vol, int dur, long seek, boolean useSW) {
|
|
super(context);
|
|
setSurfaceTextureListener(this);
|
|
Util.println(" video new");
|
|
ijkPlayer = new IjkMediaPlayer();
|
|
if(! useSW) {
|
|
ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-avc", 1);
|
|
ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-hevc", 1);
|
|
}
|
|
ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 0);
|
|
ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "analyzeduration", 1);
|
|
ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_CODEC, "skip_loop_filter", 12);
|
|
try {
|
|
ijkPlayer.setDataSource(path);
|
|
ijkPlayer.setLooping(true);
|
|
ijkPlayer.setVolume(vol, vol);
|
|
ijkPlayer.setOnPreparedListener((IMediaPlayer var1)->{
|
|
ijkPlayer.setOnPreparedListener(null);
|
|
bitRate = ijkPlayer.getBitRate();
|
|
var diff = dur - ijkPlayer.getDuration();
|
|
if(diff>0 && diff<=1000) ijkPlayer.setLooping(false);
|
|
if(seek>=1000) {
|
|
ijkPlayer.seekTo(seek);
|
|
Util.println(" Seek "+seek);
|
|
}
|
|
if(isShown() && getAlpha()>=0.2) ijkPlayer.start();
|
|
});
|
|
ijkPlayer.setOnErrorListener((IMediaPlayer var1, int var2, int var3)->{
|
|
Util.println(" Video Error: "+var1+" "+var2+" "+var3);
|
|
return true;
|
|
});
|
|
ijkPlayer.prepareAsync();
|
|
} catch (Throwable e) {
|
|
Util.makeText(getContext(), Util.toStr(e)).show();
|
|
Util.printStackTrace(e);
|
|
ijkPlayer = null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
|
|
Util.println(" SurfaceTexture Available "+(ijkPlayer==null?"ijkPlayer==null":""));
|
|
this.surface = new Surface(surface);
|
|
ijkPlayer.setSurface(this.surface);
|
|
}
|
|
@Override
|
|
public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {}
|
|
@Override
|
|
public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
|
|
Util.println(" SurfaceTexture Destroyed");
|
|
return false;
|
|
}
|
|
@Override
|
|
public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {}
|
|
|
|
void release() {
|
|
Util.println(" video release");
|
|
if(ijkPlayer!=null) {
|
|
ijkPlayer.release();
|
|
ijkPlayer = null;
|
|
}
|
|
if(surface!=null) {
|
|
surface.release();
|
|
surface = null;
|
|
}
|
|
}
|
|
|
|
// @Override
|
|
// public void onVisibilityAggregated(boolean isVisible) {
|
|
// super.onVisibilityAggregated(isVisible);
|
|
// if(isVisible) ijkPlayer.start();
|
|
// else {
|
|
// ijkPlayer.pause();
|
|
// ijkPlayer.seekTo(0);
|
|
// }
|
|
// }
|
|
} |