如何使用surfaceview播放全屏视频?

时间:2012-04-30 05:40:41

标签: android video surfaceview

我正在使用surfaceview播放本地mp4视频,这里是布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<SurfaceView
    android:id="@+id/surface"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</SurfaceView>

</FrameLayout>

从代码我将全屏设置为surfaceview

    int height = getWindowManager().getDefaultDisplay().getHeight(); //metrics.heightPixels;
    int width = getWindowManager().getDefaultDisplay().getHeight(); //metrics.widthPixels;

    mPreview = (SurfaceView) findViewById(R.id.surface);
    LayoutParams lp = new FrameLayout.LayoutParams(width, height);

    mPreview.setLayoutParams(lp);
    holder = mPreview.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    //holder.setFixedSize(width, height);
    holder.setSizeFromLayout();

但是,现在问题是视频没有全屏播放,视频上方有黑屏。

屏幕截图http://i.imgur.com/WNFDM.png 我怎样才能播放全屏视频?

3 个答案:

答案 0 :(得分:5)

不知道这多大了...... 试试这个

requestWindowFeature(Window.FEATURE_NO_TITLE);  
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

在您的活动的onCreate方法上执行setContentView之前

答案 1 :(得分:2)

尝试

  mPreview = (SurfaceView) findViewById(R.id.surface);
  LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

    mPreview.setLayoutParams(lp);
    holder = mPreview.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    holder.setSizeFromLayout();

答案 2 :(得分:0)

你能试试吗?

            mPreview = (SurfaceView) findViewById(R.id.surface);
            holder = mPreview.getHolder();
            holder.addCallback(this);

不要在你的oncreate中设置宽度和高度......