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(); Util.println(err); 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) { Util.println("Video Show"); if(! isPlaying()) { seekTo(0); start(); seekTo(0); if(! isPlaying()) Util.println("Video isn't Playing on Show"); } } else { Util.println("Video Hide"); seekTo(0); pause(); seekTo(0); showCnt = 3; if(canAdd) { choreographer.postFrameCallback(this); canAdd = false; } } } Choreographer choreographer = Choreographer.getInstance(); int showCnt; boolean canAdd = true; @Override public void doFrame(long frameTimeNanos) { if(isShown()) { seekTo(0); if(! isPlaying()) { start(); seekTo(0); if(! isPlaying()) { Util.println("Video isn't Playing on doFrame "+showCnt); if(showCnt==0) { suspend(); resume(); } } } if(showCnt>0) showCnt--; else { canAdd = true; return; } } else if(isPlaying()) { seekTo(0); pause(); seekTo(0); Util.println("pause in doFrame()"); } choreographer.postFrameCallback(this); canAdd = false; } 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+")"; } }