Android如何更改默认的VideoView / MediaPlayer全屏?

时间:2011-10-20 14:34:07

标签: android android-widget media-player surfaceview android-videoview

我目前正致力于在VideoView中播放视频(.mp4文件在两个安卓平板电脑中运行良好)。首先我使用了VideoView,但设备(EKEN M003S)全屏播放视频,而不是VideoView,我将宽度和高度设置为272 x 153 dp。所以我尝试制作一个扩展的VideoView类来覆盖onMeasure()和changeVideoSize()。像这样:

    public class EkenVideoView extends VideoView {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //Log.i("@@@@", "onMeasure");
    int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
    int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
    if (mVideoWidth > 0 && mVideoHeight > 0) {
        if ( mVideoWidth * height  > width * mVideoHeight ) {
            //Log.i("@@@", "image too tall, correcting");
            height = width * mVideoHeight / mVideoWidth;
        } else if ( mVideoWidth * height  < width * mVideoHeight ) {
            //Log.i("@@@", "image too wide, correcting");
            width = height * mVideoWidth / mVideoHeight;
        } else {
            //Log.i("@@@", "aspect ratio is correct: " +
                    //width+"/"+height+"="+
                    //mVideoWidth+"/"+mVideoHeight);
        }
    }
    if (screenMode == DisplayMode.ORIGINAL) {
       if (mVideoWidth > 0 && mVideoHeight > 0) {
           if ( mVideoWidth * height  > width * mVideoHeight ) {
               // video height exceeds screen, shrink it
               height = width * mVideoHeight / mVideoWidth;
           } else if ( mVideoWidth * height  < width * mVideoHeight ) {
               // video width exceeds screen, shrink it
               width = height * mVideoWidth / mVideoHeight;
           } else {
               // aspect ratio is correct
           }
       }
    }
    else if (screenMode == DisplayMode.FULL_SCREEN) {
       // just use the default screen width and screen height 
    }
    else if (screenMode == DisplayMode.ZOOM) {
       // zoom video
       if (mVideoWidth > 0 && mVideoHeight > 0 && mVideoWidth < width) {
           height = mVideoHeight * width / mVideoWidth;
       }
    }
    //Log.i("@@@@@@@@@@", "setting size: " + width + 'x' + height);
    setMeasuredDimension(272, 153);
}

    public void changeVideoSize(int width, int height)
{
    mVideoWidth = width;       
    mVideoHeight = height;

    // not sure whether it is useful or not but safe to do so
    getHolder().setFixedSize(272, 153); 

    requestLayout();
    invalidate();     // very important, so that onMeasure will be triggered
}

我输入宽度272和高度153以强制.mp4视频的宽度和高度为该大小,但EKEN M003S继续以全屏模式播放视频。因此,当我运行应用程序时,一切正常,视频在所有其他视图下方的图层上以全屏模式播放,使活动半透明,其下方的视频。

除了EKEN M003S,我确信某些设备还具有强制视频在全屏播放的功能,并且还有一种方法可以覆盖默认设置。如果有办法,请教我如何做。谢谢。

1 个答案:

答案 0 :(得分:1)

http://clseto.mysinablog.com/index.php?op=ViewArticle&articleId=2992625

我在Asus TF 101(1280x800 res)上测试了这段代码,视频以640x480运行。尝试在XML文件和changeVideoSize()方法中设置维度,尝试设置screenMode = DisplayMode.ORIGINAL