如何在视频上设置媒体控制器?

时间:2013-08-27 09:10:26

标签: android view android-videoview mediacontroller

我创建了简单的视频视图应用程序,它工作正常。我已将layoutparams添加到视频视图中,并将videoview添加到一个布局位置,那时我的媒体控制器显示在该区域之外,但我希望在视频上显示媒体控制器。请给我你有用的建议。

屏幕截图: enter image description here

代码:

public class MainActivity extends Activity {
    private VideoView view;
    RelativeLayout layoutmain, layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        layoutmain = (RelativeLayout) findViewById(R.id.id1);
        MediaController controller = new MediaController(this);
        view = new VideoView(this);
        view.setVideoPath("/mnt/sdcard/jj.avi");
        RelativeLayout.LayoutParams viewparam = new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        viewparam.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        viewparam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        viewparam.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        viewparam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        view.setLayoutParams(viewparam);
        FrameLayout.LayoutParams viewparam1 = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        controller.setLayoutParams(viewparam1);
        view.setMediaController(controller);
        controller.setAnchorView(controller);
        view.requestFocus();
        view.start();
        try {
            layout = new RelativeLayout(this);
            RelativeLayout.LayoutParams viewparam2 = new RelativeLayout.LayoutParams(500, 500);
            viewparam2.setMargins(20, 20, 0, 0);
            layout.setLayoutParams(viewparam2);
            layout.setBackgroundColor(Color.BLACK);
            layout.addView(view);
            layout.addView(controller);
        } catch (Exception e) {
            e.printStackTrace();
        }
        layoutmain.addView(layout);
    }
}

1 个答案:

答案 0 :(得分:5)

在xml中创建一个要放置控制器的视图,然后在这样的代码中写入

View v = findViewById(R.Id.yourView); 

controller.setAnchorView(v);