0

I'm working on a Video player application where i use MediaPlayer and SurfaceView to control Video playing. Everything is ok, But my problem is i have to implement a full screen feature where user click on a full screen button to full screen the player with controllers(Like : play,pause,seekbar e.t.c). I want the same as Youtube's full screen feature. How can i do that ?

Layout File ->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="200dp">

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

        <com.ssl.testvideo.FontAwesome
            android:id="@+id/btnPayPause"
            android:textColor="#fff"
            android:layout_centerInParent="true"
            android:text="&#xf04b;"
            android:textSize="45sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="40dp">

            <TextView
                android:id="@+id/timeForMediaPlayer"
                android:textStyle="bold"
                android:textColor="#fff"
                android:gravity="center"
                android:layout_gravity="center_vertical"
                android:text="00:00:00"
                android:layout_width="60dp"
                android:layout_height="wrap_content" />

            <SeekBar
                android:id="@+id/progressBar"
                android:layout_marginRight="10dp"
                android:layout_gravity="center_vertical"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content" />

            <com.ssl.testvideo.FontAwesome
                android:id="@+id/fullScreen"
                android:layout_marginLeft="10dp"
                android:layout_gravity="center_vertical"
                android:textSize="20sp"
                android:textColor="#fff"
                android:text="&#xf0b2;"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

which is looks like this ->

enter image description here

Also ,the Activity ->

public class PlayerActivity extends Activity 
    implements SurfaceHolder.Callback {

    Uri targetUri;

    MediaPlayer mediaPlayer;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean pausing = false;
    boolean isFirstTime = true;
    private SeekBar progressBar;
    private FontAwesome fullScreen;
    private FontAwesome btnPayPause;
    private TextView timeForMediaPlayer;
    Timer mTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playerlayout);

        initialize();

        targetUri = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");

        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setFixedSize(176, 144);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setScreenOnWhilePlaying(true);

        btnPayPause.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (pausing) {
                    pausing = false;
                    mediaPlayer.start();
                } else {
                    if (isFirstTime) {
                        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mediaPlayer.setDisplay(surfaceHolder);

                        try {
                            mediaPlayer.setDataSource(getApplicationContext(), targetUri);
                            mediaPlayer.prepare();
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        mediaPlayer.start();
                        isFirstTime = false;
                    } else {
                        pausing = true;
                        mediaPlayer.pause();
                    }
                }
            }
        });

        mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                progressBar.setSecondaryProgress(percent);
            }
        });

        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                new Thread(runnable).start();
            }
        });

        progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (mediaPlayer != null && fromUser) {
                    int timeToSet = (mediaPlayer.getDuration() * progress) / 100;
                    mediaPlayer.seekTo(timeToSet);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        fullScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }


    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {}
                int currentTime = mediaPlayer.getCurrentPosition();
                int percent = ( 100 * currentTime)  / mediaPlayer.getDuration();
                progressBar.setProgress(percent);
                final String timeToShow = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(currentTime),
                        TimeUnit.MILLISECONDS.toMinutes(currentTime) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(currentTime)),
                        TimeUnit.MILLISECONDS.toSeconds(currentTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(currentTime)));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        timeForMediaPlayer.setText(timeToShow);
                    }
                });
            }
        }
    };

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        mediaPlayer.release();
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub

    }

    private void initialize() {
        timeForMediaPlayer = (TextView) findViewById(R.id.timeForMediaPlayer);
        progressBar = (SeekBar) findViewById(R.id.progressBar);
        fullScreen = (FontAwesome) findViewById(R.id.fullScreen);
        btnPayPause = (FontAwesome) findViewById(R.id.btnPayPause);
    }
}

i'm flowing this tutorial to implement the video player. i only need to implement the full screen feature.

PLEASE DON'T SUGGEST ME ANY THIRD PARTY LIBRARY

Zahidul Islam
  • 3,180
  • 1
  • 25
  • 35
  • Have you tried MediaController: http://stackoverflow.com/a/6869615/295004 https://developer.android.com/reference/android/widget/MediaController.html – Morrison Chang Jun 05 '16 at 06:26
  • I have no issue with play/pause or steaming related issue. That part has done. My only issue is to make full screen . – Zahidul Islam Jun 05 '16 at 06:28
  • Have you seen: https://developer.android.com/training/system-ui/immersive.html – Morrison Chang Jun 05 '16 at 06:32
  • Yes, but the problem is i have to rotate full screen , not a participle view. Let's say, i have the player layout top of the layout and a `ListView` bottom, then i don't want to rotate the listview . I need only rotate the `SurfaceView` which is playing the view . – Zahidul Islam Jun 05 '16 at 06:35

0 Answers0