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

77 lines
2.6 KiB
Java
Raw Normal View History

2023-11-09 08:37:59 +08:00
package com.xixun.xixunplayer;
import android.content.Context;
import android.media.MediaPlayer;
2023-11-10 09:47:38 +08:00
import android.view.Choreographer;
2023-11-09 08:37:59 +08:00
import android.widget.VideoView;
2023-11-10 09:47:38 +08:00
public class EleVideo extends VideoView implements Choreographer.FrameCallback {
2023-11-09 08:37:59 +08:00
float vol = 1;
public EleVideo(String path, Context context) {
super(context);
setVideoPath(path);
setOnPreparedListener((MediaPlayer player)->{
2023-11-10 09:47:38 +08:00
player.setLooping(true);
2023-11-09 08:37:59 +08:00
if(! isShown()) {
player.seekTo(0);
2023-11-10 09:47:38 +08:00
player.pause();
2023-11-09 08:37:59 +08:00
}
if(vol!=1) player.setVolume(vol, vol);
setOnPreparedListener(null);
});
setOnErrorListener((MediaPlayer mp, int what, int extra)->{
Util.makeText(getContext(), "Media Error: "+getErrorName(what)+". "+getErrorName(extra)).show();
return true;
});
start();
2023-11-10 09:47:38 +08:00
choreographer.postFrameCallback(this);
2023-11-09 08:37:59 +08:00
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
}
@Override
public void onVisibilityAggregated(boolean isVisible) {
super.onVisibilityAggregated(isVisible);
if(isVisible) {
if(! isPlaying()) start();
} else {
2023-11-10 09:47:38 +08:00
seekTo(0);
2023-11-09 08:37:59 +08:00
pause();
2023-11-10 20:44:11 +08:00
showCnt = 5;
2023-11-10 09:47:38 +08:00
choreographer.postFrameCallback(this);
}
}
Choreographer choreographer = Choreographer.getInstance();
2023-11-10 20:44:11 +08:00
int showCnt;
2023-11-10 09:47:38 +08:00
@Override
public void doFrame(long frameTimeNanos) {
2023-11-10 20:44:11 +08:00
if(isShown()) {
seekTo(0);
if(showCnt>0) showCnt--;
else return;
} else if(isPlaying()) {
2023-11-09 08:37:59 +08:00
seekTo(0);
2023-11-10 09:47:38 +08:00
pause();
System.out.println("pause in doFrame()");
2023-11-09 08:37:59 +08:00
}
2023-11-10 09:47:38 +08:00
choreographer.postFrameCallback(this);
2023-11-09 08:37:59 +08:00
}
static String getErrorName(int code) {
if(code==MediaPlayer.MEDIA_ERROR_UNKNOWN) return "UNKNOWN";
if(code==MediaPlayer.MEDIA_ERROR_SERVER_DIED) return "SERVER_DIED";
if(code==MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) return "NOT_VALID_FOR_PROGRESSIVE_PLAYBACK";
if(code==MediaPlayer.MEDIA_ERROR_IO) return "IO";
if(code==MediaPlayer.MEDIA_ERROR_MALFORMED) return "MALFORMED";
if(code==MediaPlayer.MEDIA_ERROR_UNSUPPORTED) return "UNSUPPORTED";
if(code==MediaPlayer.MEDIA_ERROR_TIMED_OUT) return "TIMED_OUT";
if(code==-2147483648) return "SYSTEM";
return "Unknown ("+code+")";
}
}