Video not going Fullscreen on Android - android

I want my video activity to go fullscreen when the video starts, but it's not happening. Here are some screenshots and the code that runs this activity.:
public class InfoLentes extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.infolentes);
}
public void sendMessage(View view) {
setContentView(R.layout.fullscreen);
VideoView VideoView = new VideoView(this);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
VideoView.setMediaController(new MediaController(this));
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.eyeprotect);
VideoView.setVideoURI(video);
setContentView(VideoView);
VideoView.start();
}
}
And this is my fullscreem.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="#+id/VideoViewfull"
android:layout_width="match_parent"
android:layout_height="match_parent">
</VideoView>
</FrameLayout>
And Screenshots of whats happening.:
What am I doing wrong?
EDIT:
So Using #Sheetal Kumar Maurya answer, I created another activity and used the android:theme="#style/AppTheme.NoActionBar" on the manifest, the result is better, but still not really fullscreen.:

Create a VideoPlayer activity with NoActionbar theme.
<activity
android:name=".VideoPlayerActivity"
android:theme="#style/AppTheme.NoActionBar" />
In onCreate section add following:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Now use this activity as a player for full screen and enjoy.

So I finally, after a long time of searching, found the perfect solution for my problem.
Using the answer here on this post from #Hamiseixas, I did the following.:
Edited Gradle, and synced:
compile 'com.github.rtoshiro.fullscreenvideoview:fullscreenvideoview:1.1.2'
repositories {
mavenCentral()
}
Instead of a videoview, I used the new compiled package on my fullscreen.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent">
<com.github.rtoshiro.view.video.FullscreenVideoLayout
android:id="#+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.github.rtoshiro.view.video.FullscreenVideoLayout>
</RelativeLayout>
And when I press the intent button to take me to the video file I want to play, this is the activity that playes it.:
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.github.rtoshiro.view.video.FullscreenVideoLayout;
import java.io.IOException;
public class VideoTest extends AppCompatActivity {
FullscreenVideoLayout videoLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullscreen);
videoLayout = (FullscreenVideoLayout) findViewById(R.id.videoview);
videoLayout.setActivity(this);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.eyeprotect);
try {
videoLayout.setVideoURI(video);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}
It was that simple =D
It's all better documented here, a huge thanks to that post, and to the creator of FullscreenVideoView, Toshiro

Related

Play multiple videos at the same time using TextureView in Android programmatically

I would like to crop and play multiple videos in one activity using texture view in android programmatically. The code below throws an exception " Caused by: java.lang.IllegalArgumentException: surfaceTexture must not be null".
Mainactivity.java class
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Surface;
import android.view.TextureView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private TextureView videoView1;
private TextureView videoView2;
private MediaPlayer mediaPlayer1;
private MediaPlayer mediaPlayer2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView1 = findViewById(R.id.video_view_1);
videoView2 = findViewById(R.id.video_view_2);
String path = "android.resource://" + getPackageName() + "/" + R.raw.patila;
mediaPlayer1 = new MediaPlayer();
try {
mediaPlayer1.setDataSource(path);
mediaPlayer1.setSurface(new Surface(videoView1.getSurfaceTexture()));
mediaPlayer1.setOnPreparedListener(mp -> mp.start());
mediaPlayer1.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer2 = new MediaPlayer();
try {
mediaPlayer2.setDataSource(path);
mediaPlayer2.setSurface(new Surface(videoView2.getSurfaceTexture()));
mediaPlayer2.setOnPreparedListener(mp -> mp.start());
mediaPlayer2.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer1.release();
mediaPlayer2.release();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextureView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/video_view_1"/>
<TextureView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/video_view_2"/>
</LinearLayout>
I have done multiple research about the same and how can achieve multiple video cropping and playing on one activity.
I'm really confused on the best methodology to achieve the same because I'm new to TextureView. Kindly assist me on how to go about it.

Stopping Android video after certain amount of time

I would like to play a video with a VideoView in fullscreen mode in Android 5.1. Moreover, I would like to stop the video (and changing the activity) after a certain amount of time (let's say 5min). How can I achieve that? My layout is the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_height="fill_parent"
android:paddingLeft="2px"
android:paddingRight="2px"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="2px"
android:paddingBottom="2px"
android:layout_width="fill_parent"
android:orientation="vertical">
<VideoView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/VideoView" />
</LinearLayout>
And the following code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.VideoView;
public class VideoViewDemo extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView = (VideoView)findViewById(R.id.VideoView);
videoView.setVideoPath("/sdcard/test.mp4");
videoView.start();
}
}
You should use a "MediaPlayer" and then make a "Runnable" to stop the video at 5m. Then with a handler you call your Runnable with ".postDelayed) method like this:
public class VideoViewDemo extends Activity {
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, "video");
mp.start();
Handler handler = new Handler();
handler.postDelayed(stopPlayerTask, 500000);
}
Runnable stopPlayerTask = new Runnable(){
#Override
public void run() {
mp.pause();
}};
}
You need to adapt this example ;)

VideoView android is not working on android version 4+

Anyone help me. I am preparing application to integrate video into it. Kept video in res/raw folder. Video format .mp4. Working api-level-21. It is working on android devices for android version 2.2 but not on 4+.
Here is my code:
java:
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.MediaController;
import android.widget.VideoView;
public class Gift extends ActionBarActivity {
MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gift);
VideoView myVideoView = (VideoView) findViewById(R.id.videoView);
Uri uri= Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sidbday);
mediaController = new MediaController(this);
myVideoView.setMediaController(mediaController);
myVideoView.setVideoURI(uri);
myVideoView.start();
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<VideoView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/videoView"/>
</RelativeLayout>
This message is shown when framework is not ready to play video yet. You can start your video after onPrepared method is called:
instead of myVideoView.start(); use
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
myVideoView.start();
}
});
EDIT
I didn't notice that it's an old topic but maybe it hepls someone.

Fullscreen VideoView in landscape mode

I wish to make VideoView into full screen in landscape mode. I tried this. It shows a margin from left and right. I want to know how I can make the VideoView fullscreen.
My .xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<VideoView
android:id="#+id/videoViewN"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
You may add theme to the activity in manifest file:
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
And for VideoView in xml, change
android:layout_height="wrap_content"
to
android:layout_height="fill_parent"
This will help you achieve fullscreen in video view. Hope it helps.
adding below codes before setContentView works for me in onCreate function. I hope it helps.
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
Full Screen Landscape Video
AndroidManifext.xml (Setting the orientation)
<activity
android:name=".Video1"
android:screenOrientation="landscape" />
Video1.xml Code :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Video1">
<VideoView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
</VideoView>
Video1.java Code :
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;
public class Video1 extends AppCompatActivity {
private VideoView videoView;
private MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video1);
videoView = findViewById(R.id.videoView);
String fullScreen = getIntent().getStringExtra("fullScreenInd");
if("y".equals(fullScreen)){
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
}
Uri videoUri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.YOUR_VIDEO_NAME);
videoView.setVideoURI(videoUri);
mediaController = new FullScreenMediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.start();
}
}
FullScreenMediaControler.java Code :
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.MediaController;
public class FullScreenMediaController extends MediaController {
private ImageButton fullScreen;
private String isFullScreen;
public FullScreenMediaController(Context context) {
super(context);
}
#Override
public void setAnchorView(View view) {
super.setAnchorView(view);
//image button for full screen to be added to media controller
fullScreen = new ImageButton (super.getContext());
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
params.rightMargin = 80;
addView(fullScreen, params);
//fullscreen indicator from intent
isFullScreen = ((Activity)getContext()).getIntent().
getStringExtra("fullScreenInd");
if("y".equals(isFullScreen)){
fullScreen.setImageResource(R.drawable.ic_fullscreen_exit);
}else{
fullScreen.setImageResource(R.drawable.ic_fullscreen);
}
//add listener to image button to handle full screen and exit full screen events
fullScreen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(),Video1.class);
if("y".equals(isFullScreen)){
intent.putExtra("fullScreenInd", "");
}else{
intent.putExtra("fullScreenInd", "y");
}
((Activity)getContext()).startActivity(intent);
}
});
}
}

how to write a simple video player on android?

I have code for simple audio player on android. But it is not working for videos. Please guide me to write a simple video player.
The code of audio player is
package com.example.helloplayer;
public class HelloPlayer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mp = MediaPlayer.create(this, R.raw.file);
mp.start();
}
}
For making a video player you will have to make use of video view. a sample is shown below
Layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView android:id="#+id/videoView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
Here i am playing a video stored in "resource/raw" folder , "one" is the name of video file,you can replace it with name of your video file .also make sure that you are going to play an android supported video format
VideoplayerActivitvity
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoplayerActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView =(VideoView)findViewById(R.id.videoView);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.one);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
}
}

Categories

Resources