Auto Play Videos (List View) - android

How to do an autoplay like facebook in a listview, only one video at a time, and pause it when it comes out of focus.
Below is the code for playing video after OnClick, on Video View in Another Activity, I want it like Facebook that user scrolls and Video plays Automatically.
Videos_URL = intent.getExtras().getString("VideoUrl");
// Find your VideoView in your video_main.xml layout
myVideoView = (VideoView) findViewById(R.id.videoview);
if (mediaControls == null) {
mediaControls = new MediaController(this);
}
// Create a progressbar
progressDialog = new ProgressDialog(this);
// Set progressbar title
progressDialog.setTitle("APPNAME Video ");
// Set progressbar message
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
// Show progressbar
// Show progressbar
progressDialog.show();
try {
Uri video = Uri.parse(Videos_URL);
myVideoView.setVideoURI(video);
myVideoView.setMediaController(mediaControls);
myVideoView.start();
Is it possible to get Auto Play in Video View in Android??

On getview method of adapter you do like this,
it will auto play video when it will come on view.
videoView.setVideoPath(videoPath);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mp) {
//seekbar.
mp.start();
} catch (Exception e) {
e.printStackTrace();
}

Related

How start multiple video automatically in Android app (offline)

Good morning, Ive create an activity with a videoview that start automatically and load a video from uri in loop mode.
How can I load 2 or three video in loop mode?
For example load for from Uri(xxx1, xxx2, xxx3)?
Thaks in advance
I think you can either create a RecyclerView or ListView or ScrollView and respective Adapter or Child video views.
Have a list of video Uris in Uri[] or ArrayList
Call these in respective getView or for(Uri uri : mUris) loop
Here is a single instance to play a video
private void playVideo(Uri uri) {
//set the media controller buttons
if (mediaControls == null) {
mediaControls = new MediaController(AndroidVideoViewExample.this);
}
//initialize the VideoView
myVideoView = (VideoView) findViewById(R.id.video_view);
try {
//set the media controller in the VideoView
myVideoView.setMediaController(mediaControls);
//set the uri of the video to be played
myVideoView.setVideoURI(uri);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
//we also set an setOnPreparedListener in order to know when the video file is ready for playback
myVideoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer) {
// close the progress bar and play the video
progressDialog.dismiss();
//if we have a position on savedInstanceState, the video playback should start from here
myVideoView.seekTo(position);
if (position == 0) {
myVideoView.start();
} else {
//if we come from a resumed activity, video playback will be paused
myVideoView.pause();
}
}
});
}

How to Set android video width and height

i have issues in android video playing. am geting videos from server and set to videoview but videos is playing in backgroung am not able to see that videos ,please refer my code what i have tried.
XML FILE
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<VideoView
android:id="#+id/eventDetailsPage_videosView"
android:layout_width="160dp"
android:layout_height="100dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="#drawable/ic_eventvideo_image" />
</LinearLayout>
Java Class
Uri uri=Uri.parse(mEventDetailsUtil1.getAttachmentsUrls().get(0).getAttach_url());
VideoView mVideosView = (VideoView) findViewById(R.id.eventDetailsPage_videosView);
MediaController mc = new MediaController(this);
mc.setAnchorView(mVideosView);
mc.setMediaPlayer(mVideosView);
mVideosView.setMediaController(mc);
mVideosView.setVideoURI(uri);
mVideosView.requestFocus();
mVideosView.start();
the video is playing when opening the activity , but i don't want like that i need when i click on VideoView i need to open video full screen.
Try this :
VideoView mVideoview;
mVideoview = (VideoView) findViewById(R.id.VideoView);
mPlayServer = (Button) findViewById(R.id.mServerbutton);
mPlayServer.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
playVideo(SERVER_VIDEO_URL);
}
});
private void playVideo(SERVER_VIDEO_URL)
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(mContext);
mediacontroller.setAnchorView(mVideoview);
// Get the URL from String VideoURL
mVideo = Uri.parse(SERVER_VIDEO_URL);
mVideoview.setMediaController(mediacontroller);
mVideoview.setVideoURI(mVideo);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
mVideoview.requestFocus();
mVideoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoview.start();
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener() {
// Called when video is completed
public void onCompletion(MediaPlayer mp) {
}
});
}
To show in fullscreen use video view in new xml file with height and width as fill parent and start new activity with Intent.
Hope this helps.
Android does not support the resizing of videos, you may want to look into a Third Party library that you can utilize. By default android does not have this. I got the same problem while applying controls to video player.

how to make a videoview NOT started automatically after prepared

I have created a videoview with a standard media controller on it. I was able to play the video by calling start() method in setOnPreparedListener, so the video will play automatically when it finished preparing it self.
However, what i want to do is to make the video stand-by (NOT playing automatically), so the user need to tap/click/touch the videoview to start the video.
I've done some googling, and i also tried to setOnTouchListener on my videoview and calling the start() method there. But the result is unexpected (and confusing as well), a pop up dialog appears and said "The video cant be played".
This is the complete code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
txtTitle = (TextView)findViewById(R.id.txtTitle);
player = (VideoView)findViewById(R.id.player);
Bundle video = getIntent().getExtras();
if(video != null)
{
id = video.getString("id");
title = video.getString("title");
rtsp = video.getString("rtsp");
}
txtTitle.setText(title);
pDialog = new ProgressDialog(this);
pDialog.setTitle("Please Wait...");
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(player);
// Get the URL from String VideoURL
Uri uri = Uri.parse(rtsp);
player.setMediaController(mediacontroller);
player.setVideoURI(uri);
player.setBackgroundColor(Color.WHITE);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
player.requestFocus();
player.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
// TODO Auto-generated method stub
pDialog.dismiss();
player.setBackgroundColor(Color.TRANSPARENT);
//the video will be played if i call the start() method here
}
});
player.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
//"The video cant be played" pop up dialog appeared, video wont start
player.start();
return false;
}
});
}
I got confuse because it can be played when i put the start() method in setOnPreparedListener but it couldn't when i put it in setOnTouchListener.
I don't know if this is related to the problem or not, but im buffering a youtube video (RTSP link) on my videoview.
Any help is appreciated, Thanks.
This should be a comment but my reputation is not high enough. I do not see you calling prepare() method so probably you are trying to invoke start() when player is in initialized state. You might try to set the onTouchListener of player object in the onPrepared() callback.

play 2 videoview overlap in one layout

I want to make 2 videoviews overlap in one layout like this
This my code:
private void PlayVideo() {
try {
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(
MainActivity.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videourl);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
// videoView.requestFocus();
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
videoView.start();
}
});
} catch (Exception e) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
.create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Video Play Error :" + e.toString());
progressDialog.dismiss();
System.out.println("Video Play Error :" + e.toString());
finish();
}
My problem is: video 1 can play, but video 2 not play. If I minimize my application and I maximize again, 2 videos can run at the same time.
Please help , thanks before!
VideView is a surface view that should not overlay each other. The order of the components in the layout is not considered during rendering. The solution is to use the setZOrderMediaOverlay() method to inform the system that video2 is above any other surface. In your code I can't see video2 but you should include the following line in its initialization.
video2View.setZOrderMediaOverlay(true);

Streaming video with videoview

My code below to streaming video:
VideoView vv = (VideoView)this.findViewById(R.id.screen_video);
Uri uri = Uri.parse(URL);
vv.setVideoURI(uri);
vv.start();
It works.
But if the URL video's format not support by android phone or pad.
It show a dialog, and not show the screen.
But it still streaming with black screen.
I want to get the error message, and access as an exception.
But I don't know how to get it?
Another problem is that the streaming may crash cause by low speed wifi.
How to check it to wait while low speed wifi?
try this code,it work,
public class PlayVideo extends Activity
{
private String videoPath ="url";
private static ProgressDialog progressDialog;
String videourl;
VideoView videoView ;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play_video);
videoView = (VideoView) findViewById(R.id.videoView);
progressDialog = ProgressDialog.show(PlayVideo.this, "", "Buffering video...", true);
progressDialog.setCancelable(true);
PlayVideo();
}
private void PlayVideo()
{
try
{
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(PlayVideo.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videoPath );
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.setOnPreparedListener(new OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
progressDialog.dismiss();
videoView.start();
}
});
}
catch(Exception e)
{
progressDialog.dismiss();
System.out.println("Video Play Error :"+e.toString());
finish();
}
}
}
It depends on which Android Version you are developing your application on. There are certain devices which do not support running .mp4 file. Go through Android Media support for more information. Check if you can play any .3gp files or not.
You shouldn't play the video instantly. Add OnPrepared listener to the video view and start the video playing after it. With MediaPlayer you could keep track of the buffering state and stop the video for a while when its not yet downloaded. Please have a look at this guide.
Try Intent to avoid that error. or put try catch in your block.
VideoView can only Stream 3gp videos but i recommend this code to stream your video
public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.main);
String videourl = "http://something.com/blah.mp4";
Uri uri = Uri.parse(videourl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setDataAndType(uri, "video/mp4");
startActivity(intent);
}
Or Click here to watch Android Video Streaming Tutorial.

Categories

Resources