如何通过单击Android中的按钮来制作VideoView全屏?

时间:2015-05-28 19:43:40

标签: android android-fragments android-videoview

我的片段类中有一个VideoView。还有一个按钮,可让您播放和暂停视频。当我点击按钮播放视频时,我想要视频全屏显示。我怎么能以编程方式而不是通过XML来做到这一点?

以下是我片段的来源:

public class Test extends Fragment
{
    private VideoView vid;
    private Button    playpause1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
    {
        root = (ViewGroup) inflater.inflate(R.layout.test, null);

        vid = (VideoView) root.findViewById(R.id.videoView1);
        vid.requestFocus();
        vid.setVideoURI(Uri.parse("my_path"));

        playpause1 = (Button) root.findViewById(R.id.playpause1);
        playpause1.setText(R.string.buffering);
        playpause1.setEnabled(false);

        playpause1.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if vid.isPlaying())
                {
                    vid.pause();
                }
                else
                {
                   vid.start();
                }
            }
        });


       vid.setOnPreparedListener(new OnPreparedListener()
        {
            @Override
            public void onPrepared(MediaPlayer mp)
            {
                playpause1.setEnabled(true);

                mp.setOnInfoListener (new MediaPlayer.OnInfoListener()
                {
                    @Override
                    public boolean onInfo (MediaPlayer mp, int what, int extra)
                    {
                        if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START)
                        {
                            playpause1.setEnabled(false);
                        }

                        if(what == MediaPlayer.MEDIA_INFO_BUFFERING_END)
                        {
                            playpause1.setEnabled(true);
                        }
                        return false;
                    }
                });
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

  1. 使用自己的布局(包含完整尺寸的视频)创建一个新片段。

  2. 新片段应该获得当前播放状态:

  3. public static FullVideoFragment newInstance(final long playedDuration) { final FullVideoFragment fragment = new FullVideoFragment(); final Bundle bundle = new Bundle(); bundle.putLong("playedDuration", playedDuration); fragment.setArguments(bundle); return fragment; }

    1. 将playDuration放入相同的片段
    2. @Override public void onActivityCreated(final Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); this.playDuration = getArguments().getLong("playedDuration"); initializeViews(); //get video URI... }

      1. 在片段的clickListener中:使用FragmentManager添加完整大小的视频片段并传递播放持续时间。

      2. 将新的全尺寸片段搜索到给定的playingDuration并播放视频。