0

Streaming video from server using native media player.

            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType("URL TO VIDEO", "video/mp4");
            startActivity(i);

It will lunch view intent successfully where it shows me installed players. but the problem is when i play video. it won't let me change orientation to landscape by default. Is it somehow i can do. Thanks in advance.

Mohsin
  • 1,586
  • 1
  • 22
  • 44

1 Answers1

0

I am not sure you can do this because you are opening third party app to play your video and may be that player not providing you the control to handle orientation by your code.

You can achieve your result by below code

Create a layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayoutRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

   <VideoView
        android:id="@+id/surfaceViewFrame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">
   </VideoView>

</RelativeLayout>

now Create a new activity/playeractivity and set the above layout in setcontent view

//In OnCreate
VideoView videoView = (VideoView)findViewById(R.id.surfaceViewFrame);

videoView.setVideoURI(Uri.parse(yoururl/local/server));
        videoView.start();
        progressBarWait.setVisibility(View.VISIBLE);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(final MediaPlayer mp) {
                mp.start();
                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mediaPlayer, int i, int i2) {
                       // Log.e(TAG, "Changed");
                        progressBarWait.setVisibility(View.GONE);
                        mp.start();
                    }
                });

            }
        });

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        public void onCompletion(MediaPlayer mp) {
            finish();

        }
    });

And in Menifest.xml declear your activity with orientation

 <activity android:name=".YourPlayerActivity" 
android:screenOrientation="portrait/landscape"/>
Shadik Khan
  • 1,217
  • 1
  • 8
  • 18