I am new in the world of programming android app. I am trying to make an android app that let me view an RTSP video stream of my Reolink E1 Pro camera. Then I will add more features that are not related to RTSP video so they are not important. I have tried both VLClib and Exoplayer. At the moment I am using Exoplayer as RTSP player and when I test the app with the Android Studio emulator, all the devices that I test, run the app with no problem. When I connect my real smartphone to the PC and try to run the application, everything is fine except for the fact that the stream doesn't start and therefore the result is the black screen in the Exoplayer player. In reality the screen does not remain black but presents a gif which is loaded as long as the video buffers but it will never end to buffer. The gif is only an ImageView placed on the Exoplayer player that is visible only during buffering time. The RTSP stream is a black screen during this period. Very very rarely, after infinite attempts with the same code, the stream starts but it is a set of distorted pixels that cannot even compose a correct frame. it's not a problem related to the RTSP stream because if I use the VLC app on my smartphone, I can see the stream perfectly without any problems always at the first try. Also using the Reolink app I can see perfectly the stream. When I try to use my app with my smartphone, in the Logcat of Android Studio, I can see an infinite succession of lines that say from RtpH264Reader: Received RTP packet with unexpected sequence number. Expected: 61465; received: 61531. Dropping packet.. The number change in all the lines. It's not the same number repeated.
This is the java code I am using:
Home.java
package fexaquarium.sytes.net;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import com.bumptech.glide.Glide;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.rtsp.RtspMediaSource;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.StyledPlayerView;
public class Home extends AppCompatActivity {
private ExoPlayer player;
private StyledPlayerView styledPlayerView;
private MediaSource source;
private ImageView fullscreenButton;
private ImageView playButton;
private ImageView bufferingGif;
private boolean fullscreen = false;
private boolean playing = false;
private LinearLayout linearLayout;
private boolean isLoggedIn;
#SuppressLint("AuthLeak") private final String url = "rtsp://user:password#myRTSPlink:port";
#SuppressLint("SourceLockedOrientationActivity")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (RememberUser.getUserName(Home.this).length() == 0) {
Intent login = new Intent(Home.this, Login.class);
startActivity(login);
} else {
isLoggedIn = true;
}
setContentView(R.layout.home);
//Binding elements
styledPlayerView = findViewById(R.id.styledVideoView);
fullscreenButton = styledPlayerView.findViewById(R.id.exo_fullscreen_icon);
playButton = styledPlayerView.findViewById(R.id.playButton);
bufferingGif = findViewById(R.id.bufferingGif);
linearLayout = findViewById(R.id.linearLayout);
//Setting RTSP video height
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int videoWidth = metrics.widthPixels;
int videoHeight = (int)((float) videoWidth * 1440 / 2560);
styledPlayerView.getLayoutParams().width = videoWidth;
styledPlayerView.getLayoutParams().height = videoHeight;
styledPlayerView.requestLayout();
player = new ExoPlayer.Builder(this).build();
//Toggle fullscreen
fullscreenButton.setOnClickListener(view -> {
if(fullscreen) {
fullscreenButton.setImageDrawable(ContextCompat.getDrawable(Home.this, R.drawable.ic_fullscreen));
styledPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);
styledPlayerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
if(getSupportActionBar() != null){
getSupportActionBar().show();
}
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) styledPlayerView.getLayoutParams();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = videoHeight;
styledPlayerView.setLayoutParams(params);
linearLayout.setVisibility(VISIBLE);
fullscreen = false;
} else {
fullscreenButton.setImageDrawable(ContextCompat.getDrawable(Home.this, R.drawable.ic_fullscreen_exit));
styledPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIXED_WIDTH);
styledPlayerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) styledPlayerView.getLayoutParams();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = ViewGroup.LayoutParams.MATCH_PARENT;
styledPlayerView.setLayoutParams(params);
linearLayout.setVisibility(GONE);
fullscreen = true;
}
});
//Toggle play/pause
playButton.setOnClickListener(view -> {
if(playing) {
playButton.setImageDrawable(ContextCompat.getDrawable(Home.this, R.drawable.ic_play));
player.setPlayWhenReady(false);
playing = false;
} else {
playButton.setImageDrawable(ContextCompat.getDrawable(Home.this, R.drawable.ic_pause));
player.setPlayWhenReady(true);
playing = true;
}
});
//Buffering
player.addListener(new Player.Listener() {
#SuppressLint("SwitchIntDef")
#Override
public void onPlaybackStateChanged(int playbackState) {
Player.Listener.super.onPlaybackStateChanged(playbackState);
switch (playbackState) {
case Player.STATE_BUFFERING:
bufferingGif.setVisibility(View.VISIBLE);
Glide.with(getApplicationContext()).asGif().load(R.drawable.buffering).into(bufferingGif);
break;
case Player.STATE_READY:
bufferingGif.setVisibility(View.INVISIBLE);
Glide.with(getApplicationContext()).clear(bufferingGif);
break;
}
}
});
Uri uri = Uri.parse(url);
source = new RtspMediaSource.Factory().createMediaSource(MediaItem.fromUri(uri));
player.setMediaSource(source);
player.setVolume(0);
styledPlayerView.setPlayer(player);
player.prepare();
}
#Override
public void onPause() {
super.onPause();
player.setPlayWhenReady(false);
}
#Override
public void onDestroy() {
super.onDestroy();
player.release();
}
#Override
public void onBackPressed() {
if (isLoggedIn) {
finishAffinity();
} else {
super.onBackPressed();
}
}
}
This is my layout:
home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<RelativeLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.078">
<com.google.android.exoplayer2.ui.StyledPlayerView
android:id="#+id/styledVideoView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:fitsSystemWindows="true"
app:controller_layout_id="#layout/exo_controls"
app:resize_mode="fill">
<ImageView
android:id="#+id/bufferingGif"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:src="#drawable/buffering"
android:visibility="invisible"
tools:ignore="ContentDescription" />
</com.google.android.exoplayer2.ui.StyledPlayerView>
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.994" >
<ImageView
android:id="#+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_home"
android:layout_gravity="center"
android:layout_weight="0.1"
android:background="#drawable/btn_pressed"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/btnMeasure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_measurements"
android:layout_gravity="center"
android:layout_weight="0.1"
android:background="#drawable/btn_pressed"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/btnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_settings"
android:layout_gravity="center"
android:layout_weight="0.1"
android:background="#drawable/btn_pressed"
tools:ignore="ContentDescription" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
In the AndroidManifest.xml I set <uses-permission android:name="android.permission.INTERNET" />.
In the exo_controls.xml file, I have a simple RelativeLayout with the play/pause button and the fullscreen button.
There are definitely errors in my code since I don't know well neither android nor java. In any case, on the Android studio emulator I can get the app to work without problems but on my mobile I just can't. I discovered that it happens only when I use a remote connection. I wish I could see my RTSP streaming as smoothly as if I was using VLC or the Reolink app from a remote internet connection like my mobile phone connection. In my modem I opened the right port of the camera and I am using a free DDNS (No-Ip). I hope someone can help me because I searched on the internet but I can't find solutions to my problem and I would love to complete this little project.
Related
I am just trying to make an simple android application which will play a video from YouTube which I provide as a link. Here is the code
package com.example.videoplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
import org.videolan.libvlc.IVLCVout;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;
import java.util.ArrayList;
import java.util.logging.StreamHandler;
public class MainActivity extends AppCompatActivity {
private static String TAG = "MainActivity";
private VideoView vv;
private Button bt;
private Context mContext = this;
private LibVLC mLibVLC = null;
private MediaPlayer mMediaPlayer = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vv = findViewById(R.id.videoView);
bt = findViewById(R.id.button);
}
public void setUpMediaStream(View view) {
final ArrayList<String> args = new ArrayList<>();
args.add("-vvv");
mLibVLC = new LibVLC(mContext, args);
Log.d(TAG, "setUpMediaStream() creating media player");
mMediaPlayer = new MediaPlayer(mLibVLC);
Log.d(TAG, "setUpMediaStream() setting video view");
String yt = "https://www.youtube.com/watch?v=tXHoqsnRD-U";
Media media = new Media(mLibVLC, Uri.parse(yt));
mMediaPlayer.setMedia(media);
media.release();
mMediaPlayer.setAspectRatio(null);
mMediaPlayer.setScale(0);
Log.d(TAG, "setUpMediaStream() playing media");
mMediaPlayer.play();
}}
The problem is I am facing the following exception when I press the play button
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.videoplayer-JGI3RJow0fmaB6c-w2WuVQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.videoplayer-JGI3RJow0fmaB6c-w2WuVQ==/lib/arm64, /system/lib64]]] couldn't find "libc++_shared.so"
Any response is helpful.
Here is my XML :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<VideoView
android:id="#+id/videoView"
android:layout_width="370dp"
android:layout_height="390dp"
android:layout_marginTop="104dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.487"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:onClick="setUpMediaStream"
android:text="#string/play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"`enter code here`
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/videoView"
app:layout_constraintVertical_bias="0.153" /> </androidx.constraintlayout.widget.ConstraintLayout>
I was unable to replicate your exception, but...
I don't think you can just pass in a url of a web page, vlc is not a WebView - it needs to be a playable file, such as https://jell.yfish.us/media/jellyfish-3-mbps-hd-h264.mkv - and be sure to add the internet permission in the manifest:
<uses-permission android:name="android.permission.INTERNET" />
I also think you need to replace the VideoView in your layout with org.videolan.libvlc.util.VLCVideoLayout and pass it into mediaPlayer.attachViews() after you instantiate the MediaPlayer.
I was able to play the video with these steps.
** EDIT **
LibVLC also have a sample project, where in their app-level build.gradle file they specify jni directories:
android {
...
sourceSets {
main {
jni.srcDirs = [] // Prevent gradle from building native code with ndk; we have our own Makefile for it.
jniLibs.srcDir 'jni/libs' // Where generated .so files are placed
assets.srcDirs = ['src/main/assets', '../assets/']
}
}
}
https://code.videolan.org/videolan/libvlc-android-samples/-/tree/master/java_sample
What I need is: Change the URL of the video view whenever I change the remote config parameters!
I'm making a tv channel app using video view.
to show the TV channels if the format of the url .m3u8 is used, but this link changes daily so IMPOSSIVEL I stay updating my application every day that change this link .... so I discovered (actually I remembered) that in firebase there is a option "remote config" that it is possible to change these links ...
What do I want anyway? How can I do this by using the remote config
package tk.protvapp.protv;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.MediaController;
import android.widget.VideoView;
public class FoxActivity extends Activity {
VideoView myVideoView;
View v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_fox);
myVideoView = (VideoView)this.findViewById(R.id.videofox1); MediaController mc = new MediaController(this); myVideoView.setMediaController(mc);
final String urlStream = "//Search the link in remote config for paste here";
myVideoView.start();
myVideoView.findFocus();
runOnUiThread(new Runnable() { #Override public void run() { myVideoView.setVideoURI(Uri.parse(urlStream)); } });
}
public void voltarhomefox(View v){
Intent intent = new Intent(FoxActivity.this, HomeActivity.class);
startActivity(intent);
}
}
My layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
tools:context="tk.protvapp.protv.FoxActivity">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="voltarhomefox"
android:text="VOLTAR" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/frameLayout">
<VideoView
android:id="#+id/videofox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</RelativeLayout>
How can I change whenever I need the Videoview URL using the Google Firebase Remote config method?
More details: I have a VideoView that uses URL to show video or stream but I want to be able to change these links whenever I need. Without having to create a new application update!
My activity:
package tk.protvapp.protv;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.MediaController;
import android.widget.VideoView;
public class FoxActivity extends Activity {
VideoView myVideoView;
View v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_fox);
myVideoView = (VideoView)this.findViewById(R.id.videofox1); MediaController mc = new MediaController(this); myVideoView.setMediaController(mc);
final String urlStream = "//Search the link in remote config for paste here";
myVideoView.start();
myVideoView.findFocus();
runOnUiThread(new Runnable() { #Override public void run() { myVideoView.setVideoURI(Uri.parse(urlStream)); } });
}
public void voltarhomefox(View v){
Intent intent = new Intent(FoxActivity.this, HomeActivity.class);
startActivity(intent);
}
}
My Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
tools:context="tk.protvapp.protv.FoxActivity">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="voltarhomefox"
android:text="VOLTAR" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/frameLayout">
<VideoView
android:id="#+id/videofox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</RelativeLayout>
You can define a key in Remote Config Dashboard
video_url = "https://foo.com/video.mp4"
and fetch the value of video_url from your code using the getString("video_url") method.
All I want is My videos to be normally loaded when anyone clicks on the video icon in the video control panel, and the video is afterwards played.
I am stuck. I am able to load one video but I am unable to load multiple videos in one activity sequence.
I want to load the next:
fragment names from an array
videos id from an array
So that I can load many videos in one activity.
Here is my code:
activity_video.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.soleyman.happybirthdayurj.VideoActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/unlisted"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Info"
android:onClick="unlistClick"
/>
<fragment
android:id="#+id/youtube_player_fragment1"
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:layout_width="280dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"/>
<fragment
android:id="#+id/youtube_player_fragment2"
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:layout_width="280dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
VideoActivity.java
package com.example.soleyman.happybirthdayurj;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;
public class VideoActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {
private YouTubePlayer sPlayer;
YouTubePlayerFragment playerFragment;
private String[] ytNames = {"R.id.youtube_player_fragment1", "R.id.youtube_player_fragment2"};
private String[] vdoID = {"PiKxS3P5o2Y", "PwC-fFIGjZM"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show Actionbar
ActionBar actionBar = getSupportActionBar();
actionBar.show();
this.setTitle("My Favorite Panda Videos");
setContentView(R.layout.activity_video);
playerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(ytNames[]); // currently it's showing error
playerFragment.initialize(PlayerConfig.API_KEY, this);
}
private void playVideo(String videoId) {
if ( sPlayer != null ) {
sPlayer.loadVideo(videoId);
sPlayer.cueVideo(videoId);
}
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
sPlayer = youTubePlayer;
playVideo(vdoID[]); // currently it's showing error
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
sPlayer = null;
}
// UnlistedActivity Activity
public void unlistClick(View view) {
Intent intent = new Intent(VideoActivity.this, UnlistedActivity.class);
startActivity(intent);
}
}
Any help is highly appreciated. If you have any suggestions or different methods/ways to do the same thing, please, suggest to me.
P.S.: Already spent 1 day to solve it
Both ytNames[] and vdoID[] are arrays.
All the places you're trying to use them, you need to give an index. Like ytNames[0] for the first one.
This is quite basic programming, you might want to brush up on arrays:
https://code.tutsplus.com/tutorials/learn-java-for-android-development-working-with-arrays--mobile-2894
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);
}
});
}
}