通过复制Lollipop的android.widget.VideoView的完整源代码并删除你提到的行,我通过一个愚蠢的解决方案解决了这个问题。
制作自己的VideoView课程。 不要使用extends VideoView因为你不能覆盖openVideo()方法。
我不建议这样做,因为我认为这是一个临时解决方案。 VideoView在4.1-5.0之间改变很多,所以这可以在除Lollipop之外的Android版本上进行RuntimeException
编辑
pinxue告诉我们,我接近MediaPlayer + SurfaceView; 它尊重viewWidth和viewHeight纵横比。
final String finalFilePath = filePath; final SurfaceHolder surfaceHolder = sv.getHolder(); final MediaPlayer mediaPlayer = new MediaPlayer(); final LinearLayout.LayoutParams svLayoutParams = new LinearLayout.LayoutParams(viewWidth,viewHeight); surfaceHolder.addCallback(new SurfaceHolder.Callback(){ @Override public void surfaceCreated(SurfaceHolder holder) { try { if(isDebug) { System.out.println("setting VideoPath to VideoView: "+finalFilePath); } mediaPlayer.setDataSource(finalFilePath); }catch (IOException ioe){ if(isDebug){ ioe.printStackTrace(); } //mediaPlayer = null; } mediaPlayer.setDisplay(surfaceHolder); mediaPlayer.prepareAsync(); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { if(isDebug){ System.out.println("Video is starting..."); } // for compatibility, we adjust size based on aspect ratio if ( mp.getVideoWidth() * svLayoutParams.height < svLayoutParams.width * mp.getVideoHeight() ) { //Log.i("@@@", "image too wide, correcting"); svLayoutParams.width = svLayoutParams.height * mp.getVideoWidth() / mp.getVideoHeight(); } else if ( mp.getVideoWidth() * svLayoutParams.height > svLayoutParams.width * mp.getVideoHeight() ) { //Log.i("@@@", "image too tall, correcting"); svLayoutParams.height = svLayoutParams.width * mp.getVideoHeight() / mp.getVideoWidth(); } sv.post(new Runnable(){ @Override public void run() { sv.setLayoutParams(svLayoutParams); } }); mp.start(); } }); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if(isDebug){ System.out.println("surfaceChanged(holder, "+format+", "+width+", "+height+")"); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { try { mediaPlayer.setDataSource(""); }catch (IOException ioe){ if(isDebug){ ioe.printStackTrace(); } } } }); if(sv.post(new Runnable() { @Override public void run() { sv.setLayoutParams(svLayoutParams);/// sv.setVisibility(View.VISIBLE); }})){ if(isDebug) { System.out.println("post Succeded"); } }else{ if(isDebug) { System.out.println("post Failed"); } }