Stopping Android video after certain amount of time - android

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 ;)

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.

Video not going Fullscreen on 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

Android Application Not Showing Anything

I'm trying to create a basic audio player with Play/Pause and a slider based on this tutorial: http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243
Everything compiles properly however nothing shows up in the activity on either emulator or device. Device api level 18 and AVD api level 19
MainActivity.java
package com.ex.highline;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
SeekBar seek_bar;
Button play_button, pause_button;
MediaPlayer player;
TextView text_shown;
Handler seekHandler = new Handler();
public void onClick(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekUpdate();
/* Initialize all views */
seek_bar = (SeekBar) findViewById(R.id.seek_bar);
play_button = (Button) findViewById(R.id.play_button);
pause_button = (Button) findViewById(R.id.pause_button);
text_shown = (TextView) findViewById(R.id.text_shown);
play_button.setOnClickListener((android.view.View.OnClickListener) this);
pause_button.setOnClickListener((android.view.View.OnClickListener) this);
player = MediaPlayer.create(this, R.raw.money);
seek_bar.setMax(player.getDuration());
}
Runnable run = new Runnable() {
#Override
public void run(){
seekUpdate();
}
};
public void seekUpdate() {
seek_bar.setProgress(player.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
public void onClick(View view){
switch (view.getId()){
case R.id.play_button:
text_shown.setText("Playing...");
player.start();
break;
case R.id.pause_button:
player.pause();
text_shown.setText("Paused...");
}
}
}
activity_main.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_height="fill_parent" >
<TextView
android:id="#+id/text_shown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp"
android:text=""
android:textSize="42sp" />
<SeekBar
android:id="#+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button
android:id="#+id/pause_button"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_alignBaseline="#+id/play_button"
android:layout_alignBottom="#+id/play_button"
android:layout_toRightOf="#+id/text_shown"
android:text="#string/pause" />
<Button
android:id="#+id/play_button"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_alignRight="#+id/text_shown"
android:layout_below="#+id/seek_bar"
android:layout_marginTop="44dp"
android:gravity="center"
android:text="#string/play" />
</RelativeLayout>
there is two changes:
1) OncLick ==> onCreate
2) put seekUpdate(); at the end of onCreate method (cauz you have the var play)**
public class MainActivity extends Activity implements OnClickListener {
SeekBar seek_bar;
Button play_button, pause_button;
MediaPlayer player;
TextView text_shown;
Handler seekHandler = new Handler();
public void onCreate(Bundle savedInstanceState) { // <== change here
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Initialize all views */
seek_bar = (SeekBar) findViewById(R.id.seek_bar);
play_button = (Button) findViewById(R.id.play_button);
pause_button = (Button) findViewById(R.id.pause_button);
text_shown = (TextView) findViewById(R.id.text_shown);
play_button.setOnClickListener((android.view.View.OnClickListener) this);
pause_button.setOnClickListener((android.view.View.OnClickListener) this);
player = MediaPlayer.create(this, R.raw.test);
seek_bar.setMax(player.getDuration());
seekUpdate(); // <==== change here
}
// the rest of code
In Android the activities are shown responding to intents.
When you click on an Android App icon in your mobile you are launching the action android.intent.action.MAIN with the category android.intent.category.LAUNCHER so... if you want to see the activity you should make sure that you have something like this in your AndroidManifest.xml:
<activity
android:name=".MainActivity">
<!-- intent filters -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Where .MainActivity is the class name of your activity with the app package as prefix.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ex.highline"
android:versionCode="1"
android:versionName="1.0" >
So... the app package + the activity main are the activity complete class name that you want to start (when you click on the app icon).

How to play video which is at some URL location in android's video view?

I am trying to play youtube video within video view but getting error(can't play this video) all the time(on device as well as on emulator too). Please help me with this problem.
Thanks in advance.
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.VideoView;
public class AndroidVideoView extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView myVideoView = (VideoView) findViewById(R.id.videoview);
String viewSource = "rtsp://v2.cache8.c.youtube.com/CjYLENy73wIaLQndUnGVjs340xMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYJP8mrqSz625UAw=/0/0/0/video.3gp";
myVideoView.setVideoURI(Uri.parse(viewSource));
myVideoView.requestFocus();
myVideoView.start();
}
}
1) Videos will not buffer on emulator because it is virtual device. In-order stream video you should use real device
2) you must provide this permission because with out internet connection we will not able to connect with YouTube server
<uses-permission android:name="android.permission.INTERNET" />
3)Please validate your RTSP link. Given below is my rtsp link for my YouTube video
"rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQnCikhfRzTDsBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";
To generate rtsp link Use this
http://gdata.youtube.com/feeds/mobile/videos/"+Video Id+"
In my case Video Id is sMM0R19IisI
My Code
public class MainActivity extends Activity {
String SrcPath = "rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQnCikhfRzTDsBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";//"rtsp://v5.cache1.c.youtube.com/CjYLENy73wIaLQnhycnrJQ8qmRMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYPj_hYjnq6uUTQw=/0/0/0/video.3gp";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoURI(Uri.parse(SrcPath));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
}
}
LAYOUT XML
<?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/myvideoview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

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