98 lines
3.3 KiB
Java
98 lines
3.3 KiB
Java
![]() |
package com.xixun.xixunplayer;
|
||
|
|
||
|
import android.content.Context;
|
||
|
import android.media.MediaPlayer;
|
||
|
import android.view.Choreographer;
|
||
|
import android.widget.VideoView;
|
||
|
|
||
|
public class EleVideo2 extends VideoView implements Choreographer.FrameCallback {
|
||
|
|
||
|
float vol = 1;
|
||
|
|
||
|
public EleVideo2(String path, Context context) {
|
||
|
super(context);
|
||
|
setVideoPath(path);
|
||
|
setOnPreparedListener((MediaPlayer player)->{
|
||
|
player.setLooping(true);
|
||
|
if(! isShown()) {
|
||
|
player.seekTo(0);
|
||
|
player.pause();
|
||
|
}
|
||
|
if(vol!=1) player.setVolume(vol, vol);
|
||
|
setOnPreparedListener(null);
|
||
|
});
|
||
|
setOnErrorListener((MediaPlayer mp, int what, int extra)->{
|
||
|
var err = "Media Error: "+getErrorName(what)+". "+getErrorName(extra);
|
||
|
Util.makeText(getContext(), err).show();
|
||
|
MainActivity.ins.buf.append(err+'\n');
|
||
|
return true;
|
||
|
});
|
||
|
start();
|
||
|
}
|
||
|
|
||
|
@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) {
|
||
|
MainActivity.ins.buf.append("Video Show\n");
|
||
|
if(! isPlaying()) {
|
||
|
seekTo(0);
|
||
|
start();
|
||
|
seekTo(0);
|
||
|
if(! isPlaying()) MainActivity.ins.buf.append("Video isn't Playing on Show\n");
|
||
|
}
|
||
|
} else {
|
||
|
MainActivity.ins.buf.append("Video Hide\n");
|
||
|
seekTo(0);
|
||
|
pause();
|
||
|
seekTo(0);
|
||
|
showCnt = 3;
|
||
|
choreographer.postFrameCallback(this);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Choreographer choreographer = Choreographer.getInstance();
|
||
|
int showCnt;
|
||
|
@Override
|
||
|
public void doFrame(long frameTimeNanos) {
|
||
|
if(isShown()) {
|
||
|
seekTo(0);
|
||
|
if(! isPlaying()) {
|
||
|
start();
|
||
|
seekTo(0);
|
||
|
if(! isPlaying()) {
|
||
|
MainActivity.ins.buf.append("Video isn't Playing on doFrame ").append(showCnt).append('\n');
|
||
|
if(showCnt==0) {
|
||
|
suspend();
|
||
|
resume();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if(showCnt>0) showCnt--;
|
||
|
else return;
|
||
|
} else if(isPlaying()) {
|
||
|
seekTo(0);
|
||
|
pause();
|
||
|
seekTo(0);
|
||
|
System.out.println("pause in doFrame()");
|
||
|
}
|
||
|
choreographer.postFrameCallback(this);
|
||
|
}
|
||
|
|
||
|
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+")";
|
||
|
}
|
||
|
}
|