Soumen Pramanik
Soumen Pramanik

Reputation: 23

Video Player App Rotation error in android studio

package com.livevideoshare.videosharingapp;

import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.Random;

public class LiveVideoActivity extends AppCompatActivity {

    private VideoView videoView;
    private TextView floatingTextView;
    private Handler handler;
    private final int INTERVAL = 10000;
     int currentPosition ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_video);

        //if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //}

        // Get the VideoView reference from XML layout
        videoView = findViewById(R.id.videoView);
        floatingTextView = findViewById(R.id.floatingTextView);

        // Get the video URL from the server (you should replace this with your actual server call)
        String videoUrl = "http://kolkatacoders.com/videos/vid1.mp4";

        // Set the video URI using the URL obtained from the server
        Uri videoUri = Uri.parse(videoUrl);

        // Create a MediaController
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);

        // Set MediaController for VideoView
        videoView.setMediaController(mediaController);

        // Set video URI
        videoView.setVideoURI(videoUri);
        videoView.requestFocus();
        if (savedInstanceState != null) {
            currentPosition = savedInstanceState.getInt("currentPosition", 0);
        }
        videoView.seekTo(currentPosition);
        // Start playing the video
        videoView.start();

        ///-------------

        // Show text at regular intervals
        handler = new Handler();
        //showFloatingText();

        //handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                floatingTextView.setVisibility(View.VISIBLE);
                setRandomPosition();

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       // floatingTextView.setVisibility(View.INVISIBLE);
                        showFloatingText();
                    }
                }, 2000); // Hide text after 2 seconds
            }
        }, 10000); // Show text after 30 seconds

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d("hi soumen","done");
        outState.putInt("currentPosition", videoView.getCurrentPosition());
        Log.d("hi soumen","videoView.getCurrentPosition()"+videoView.getCurrentPosition());
    }



    // Set the VideoView to full-screen mode
        //setVideoViewFullScreen();
        private void showFloatingText() {
            floatingTextView.setVisibility(View.VISIBLE);
            //setRandomPosition();

            // Hide text after 2 seconds
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    floatingTextView.setVisibility(View.INVISIBLE);
                    // Show text again after 10 seconds
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            setRandomPosition();
                            showFloatingText();
                        }
                    }, INTERVAL);
                }
            }, 2000); // Hide text after 2 seconds
        }
        private void setRandomPosition() {
            // Get the dimensions of the FrameLayout
            int parentWidth = findViewById(R.id.container).getWidth();
            int parentHeight = findViewById(R.id.container).getHeight();

            // Generate random x and y coordinates
            Random random = new Random();
            int x = random.nextInt(parentWidth - floatingTextView.getWidth());
            int y = random.nextInt(parentHeight - floatingTextView.getHeight());

            // Set the TextView's position
            floatingTextView.setX(x);
            floatingTextView.setY(y);
        }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }
}

this is my code. two thing i want to add here one is rotation option and second is after rotation start video from the same position from here it runs. Basically i want to run a video using a url. but like youtube there is no rotation icon. It has only three icon in the VideoView app. One is pause/play one is forward and one is backword.

Upvotes: 0

Views: 29

Answers (0)

Related Questions