vendredi 29 mai 2015

How to make a VideoView full screen by clicking a button in Android?

I have a VideoView in my fragment class. There is also a button that allows you to play and pause the video. When I click the button to play the video, I want to video to go full screen. How can I do this programatically and not through XML?

Here is my source for my fragment:

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;
                    }
                });
            }
        });
    }
}

Aucun commentaire:

Enregistrer un commentaire