2023-11-09 08:37:59 +08:00
|
|
|
package com.xixun.xixunplayer;
|
|
|
|
|
|
|
|
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 androidx.annotation.OptIn;
|
|
|
|
import androidx.media3.common.MediaItem;
|
|
|
|
import androidx.media3.common.util.UnstableApi;
|
|
|
|
import androidx.media3.exoplayer.ExoPlayer;
|
|
|
|
import androidx.media3.exoplayer.SeekParameters;
|
|
|
|
|
|
|
|
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-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-11 15:17:48 +08:00
|
|
|
String path;
|
|
|
|
float vol;
|
2023-12-01 16:17:06 +08:00
|
|
|
IjkMediaPlayer ijkPlayer;
|
2023-12-11 15:17:48 +08:00
|
|
|
ExoPlayer exoPlayer;
|
2024-01-24 20:17:59 +08:00
|
|
|
long bitRate;
|
2024-02-02 18:09:54 +08:00
|
|
|
boolean isLive;
|
2023-12-01 16:17:06 +08:00
|
|
|
|
2024-02-02 19:34:33 +08:00
|
|
|
public SrcVideo(Context context, String path, float vol, boolean isLive) {
|
2023-11-09 08:37:59 +08:00
|
|
|
super(context);
|
2023-12-11 15:17:48 +08:00
|
|
|
this.path = path;
|
|
|
|
this.vol = vol;
|
2024-02-02 18:09:54 +08:00
|
|
|
this.isLive = isLive;
|
2023-12-11 15:17:48 +08:00
|
|
|
initIjk();
|
2023-11-09 08:37:59 +08:00
|
|
|
}
|
|
|
|
|
2023-12-11 15:17:48 +08:00
|
|
|
@OptIn(markerClass = UnstableApi.class)
|
|
|
|
void initExo() {
|
|
|
|
exoPlayer = new ExoPlayer.Builder(getContext()).build();
|
|
|
|
exoPlayer.setMediaItem(MediaItem.fromUri(path));
|
|
|
|
exoPlayer.setRepeatMode(ExoPlayer.REPEAT_MODE_ONE);
|
|
|
|
exoPlayer.setSeekParameters(SeekParameters.CLOSEST_SYNC);
|
|
|
|
exoPlayer.setVolume(vol);
|
2024-01-24 20:17:59 +08:00
|
|
|
exoPlayer.setVideoTextureView(this);
|
2023-12-11 15:17:48 +08:00
|
|
|
exoPlayer.prepare();
|
|
|
|
}
|
|
|
|
void initIjk() {
|
2023-12-01 16:17:06 +08:00
|
|
|
ijkPlayer = new IjkMediaPlayer();
|
2023-12-11 15:17:48 +08:00
|
|
|
//ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 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);
|
|
|
|
ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_CODEC, "skip_loop_filter", 48);
|
|
|
|
try {
|
2024-01-24 20:17:59 +08:00
|
|
|
setSurfaceTextureListener(this);
|
2023-12-01 16:17:06 +08:00
|
|
|
ijkPlayer.setDataSource(path);
|
|
|
|
ijkPlayer.setLooping(true);
|
|
|
|
ijkPlayer.setVolume(vol, vol);
|
2023-12-11 15:17:48 +08:00
|
|
|
ijkPlayer.setOnPreparedListener((IMediaPlayer var1)->{
|
2024-01-24 20:17:59 +08:00
|
|
|
bitRate = ijkPlayer.getBitRate();
|
|
|
|
if(bitRate > 12000000) {
|
|
|
|
setSurfaceTextureListener(null);
|
2023-12-11 15:17:48 +08:00
|
|
|
release();
|
|
|
|
initExo();
|
|
|
|
}
|
2024-03-26 15:15:57 +08:00
|
|
|
if(isShown()) start();
|
2023-12-11 15:17:48 +08:00
|
|
|
});
|
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
|
|
|
}
|
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) {
|
2024-03-26 15:15:57 +08:00
|
|
|
setSurfaceTextureListener(null);
|
2024-01-24 20:17:59 +08:00
|
|
|
if(ijkPlayer!=null) ijkPlayer.setSurface(new Surface(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) {
|
|
|
|
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 start() {
|
2024-01-24 20:17:59 +08:00
|
|
|
if(ijkPlayer!=null) {
|
|
|
|
ijkPlayer.seekTo(0);
|
|
|
|
ijkPlayer.start();
|
|
|
|
} else if(exoPlayer!=null) {
|
2023-12-11 15:17:48 +08:00
|
|
|
if(exoPlayer.getPlaybackState()==ExoPlayer.STATE_IDLE) exoPlayer.prepare();
|
|
|
|
exoPlayer.play();
|
|
|
|
}
|
2023-12-01 16:17:06 +08:00
|
|
|
}
|
|
|
|
void pause() {
|
|
|
|
if(ijkPlayer!=null) {
|
|
|
|
ijkPlayer.pause();
|
|
|
|
ijkPlayer.seekTo(0);
|
2023-11-10 09:47:38 +08:00
|
|
|
}
|
2023-12-11 15:17:48 +08:00
|
|
|
if(exoPlayer!=null && exoPlayer.isPlaying()) {
|
|
|
|
exoPlayer.pause();
|
|
|
|
exoPlayer.seekToDefaultPosition();
|
|
|
|
}
|
2023-11-10 09:47:38 +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
|
|
|
}
|
2023-12-11 15:17:48 +08:00
|
|
|
if(exoPlayer!=null) {
|
|
|
|
exoPlayer.release();
|
|
|
|
exoPlayer = null;
|
|
|
|
}
|
2023-12-01 16:17:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
String getState() {
|
2023-12-11 15:17:48 +08:00
|
|
|
if(exoPlayer!=null) {
|
|
|
|
var state = exoPlayer.getPlaybackState();
|
|
|
|
if(state==ExoPlayer.STATE_IDLE) return "STATE_IDLE";
|
|
|
|
if(state==ExoPlayer.STATE_BUFFERING) return "STATE_BUFFERING";
|
|
|
|
if(state==ExoPlayer.STATE_READY) return "STATE_READY";
|
|
|
|
if(state==ExoPlayer.STATE_ENDED) return "STATE_ENDED";
|
|
|
|
}
|
2023-12-01 16:17:06 +08:00
|
|
|
return "null";
|
2023-11-09 08:37:59 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 16:17:06 +08:00
|
|
|
@Override
|
|
|
|
public void onVisibilityAggregated(boolean isVisible) {
|
|
|
|
super.onVisibilityAggregated(isVisible);
|
2024-02-02 18:09:54 +08:00
|
|
|
if(isVisible) {
|
|
|
|
start();
|
|
|
|
if(isLive) ijkPlayer.setVolume(vol, vol);
|
|
|
|
} else if(isLive) ijkPlayer.setVolume(0, 0);
|
2023-12-01 16:17:06 +08:00
|
|
|
else pause();
|
2023-11-09 08:37:59 +08:00
|
|
|
}
|
|
|
|
}
|