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);
}
}
}
这是我的代码。我想在此处添加两件事,一是旋转选项,二是旋转后从其运行的相同位置开始视频。 基本上我想使用网址运行视频。但像 YouTube 一样,没有旋转图标。 VideoView 应用程序中只有三个图标。一种是暂停/播放,一种是前进,一种是后退。
要添加旋转选项并确保视频旋转后从同一位置恢复,您可以按照以下步骤操作:
添加旋转选项:您可以在布局中添加旋转按钮/图标,单击时,它将在横向和纵向之间旋转屏幕。 处理旋转更改:当屏幕旋转时,活动将被销毁并重新创建。您可以在旋转前保存当前视频位置并在旋转后恢复它。 以下是实现这些目标的方法:
首先,让我们在布局中添加一个旋转按钮:
<!-- activity_live_video.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Your existing VideoView and TextView -->
<ImageButton
android:id="@+id/rotateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:src="@drawable/ic_rotate"
android:background="@android:color/transparent"/>
</RelativeLayout>
接下来,让我们修改 LiveVideoActivity 以处理旋转并添加旋转功能:
public class LiveVideoActivity extends AppCompatActivity {
// Your existing code...
private boolean isFullscreen = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_video);
// Your existing code...
// Find and set click listener for rotation button
ImageButton rotateButton = findViewById(R.id.rotateButton);
rotateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rotateScreen();
}
});
}
private void rotateScreen() {
if (isFullscreen) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
isFullscreen = true;
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
isFullscreen = false;
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
// Other methods...
}
现在,为了确保视频旋转后从同一位置恢复,您需要分别在 onSaveInstanceState 和 onRestoreInstanceState 方法中保存和恢复视频位置:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("currentPosition", videoView.getCurrentPosition());
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
currentPosition = savedInstanceState.getInt("currentPosition");
videoView.seekTo(currentPosition);
}
通过这些更改,您将拥有一个旋转按钮,并且视频将在旋转后从同一位置恢复。确保根据您的要求和设计调整布局和代码。