multiple videos play one after the another - android

can anyone teach me how to make videos play one after the other
I need the full code
I have 4 videos
Video1, Video2, Video3, Video4
I want Video1 to play first then followed Video2, then followed by Video3, then followed by Video4
String path="android.resource://" + getPackageName() +"/" + R.raw.Video1;
videoView1.setVideoURI(Uri.parse(path));
videoView1.start();

Short & Simple
Let's say you have four videos in array list
ArrayList<String> urlList = new ArrayList<>();
Create one counter variable to manage current video playing.
int video_counter = 0;
Now following code make your work easy to play dynamic range of video to play in loop (like one after another).
VideoView vv_video = findViewById(R.id.vv_video);
vv_video.setVideoURI(Uri.parse(urlList.get(counter_video_loop)));
vv_video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
vv_video.start();
}
});
vv_video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if ((video_counter + 1) <= urlList.size()) {
video_counter++;
vv_video.setVideoURI(Uri.parse(urlList.get(video_counter + 1)));
vv_video.start();
}
}
});

Assume you have a Videoview, you could for example put the paths to the videos into an array, and detect the end of playback like this:
videoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//start next video here
//for example, set video path to next array item
}
});

Maybe you should create a new VideoView to play next video at OnCompletionListener, and remove the old VideoView ,add the new VideoView.
It works for me.

use exoPlayer for concatenate multi video

Related

How to remove the first 3 seconds from a mp4 file on android?

I want to remove the first 3 seconds from a mp4 video file on android, how can I do it with Android API?
If you are using VideoView for showing the video, try using below code.
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
videoView.seekTo(3000);
}
});

How do I play videos sequentially; one after the other?

I am developing an Android application in which I am reading video links from web services and displaying them in a ListView. I want it so that when I click on any video link it starts playing and at end of that video the next video starts playing automatically (just like a playlist).
myVideoView.setVideoPath(vidPath);
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
myVideoView.setOnCompletionListener(completionListener);
OnCompletionListener completionListener=new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.stop();
//list1 is your list.
i = (i + 1) % list1.size();
//lstplay is your listview.
lstPlay.setItemChecked(i, true);
setup(i);
}
};
Hope this will help you.For your reference, reference1,reference2

How to play preroll?

I want to play preroll ad (Just like you tube) before playing any video live streaming in my app. Right now i am able to play video using URL through streaming but not able to find any way to play preroll please some one help me to achieve this goal.
Just provide a ArrayList of videoUrls for your media player. Use the OnCompletionListener interface and play the next (none-preroll) video when the preroll has been completed. From what Ive understood you can't feed the Android media player a list of urls, you have to setDataSource for each video.
//Starting the mediaplayer
private void playVideo() {
mediaPlayer.setDataSource(videoUrlList.get(playlistPosition));
mediaPlayer.prepareAsync();
}
#Override
public void onCompletion(MediaPlayer mp) {
//Look if the list has more videos
if (playlistPosition < videoUrlList.size() - 1) {
playlistPosition++;
if(mediaPlayer != null)
mediaPlayer.reset();
playVideo();
}else{
//If no more videos, quit MoviePlayer Activity
finish();
}
}

Play video one after another

I need to play two video one after another(as a pair), the first video is as intro video and the second video is as main video, So what I actually need is that after finishing intro video the main video will start...say intro-1 & main-1, intro-2&main-2, intro-3& main3...so on.
the problem that I am getting is that i cnt move to intro video again after completing the main video.Only the main video is played again and again
Here is that code:
videoView.setVideoPath(introPath);
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(final MediaPlayer mp) {
videoView.setVideoPath(mainPath);
MediaController mc = new MediaController(DisplayVideo.this);
videoView.requestFocus();
videoView.start();
}
}
any help will really be appreciated Thanks
Create a list of the video paths, something like:
List<String> videoPathes = new ArrayList<String>();
videoPathes.add(path1);
videoPathes.add(path2);
// etc..
and some index, for example:
int i = 0;
In the onCompletionListener, set the next path this way:
public void onCompletion(final MediaPlayer mp) {
i = (i + 1) % videoPathes.size();
videoView.setVideoPath(videoPathes.get(i));
// the rest ...
}

Seamless video Loop with VideoView

I have the following code to take a video as a raw resource, start the video and loop it but I need the video to loop seamlessly as of now when it comes to an end of the clip and starts the clip again the transition between causes a flicker for a split second, which I really can't have for my app.
public class Example extends Activity {
VideoView vv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vv = (VideoView)findViewById(R.id.VideoView01);
//Video Loop
vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
vv.start(); //need to make transition seamless.
}
});
Uri uri = Uri.parse("android.resource://com.example/"
+ R.raw.video);
vv.setVideoURI(uri);
vv.requestFocus();
vv.start();
}
}
The clip is only 22 seconds long but was created to be seamless so it is possible to work without the delay.
Try this it will work 100%
VideoView videoView;<---write this in outside of method or else declare it as final variable.
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
In Kotlin simply use
videoView.setOnPreparedListener { it.isLooping = true }
Not sure if this helps years later, but I used
vv.start();
vv.setOnCompletionListener ( new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
vv.start();
}
});
and it has a seamless loop
The pause is for the underlying MediaPlayer to refresh its buffers. How long that will take will depend on a number of factors, many of which are outside your control (e.g., speed of CPU, speed of on-board flash storage).
One you can control is to get your video out of the resource and into the filesystem. Resources are stored in the APK, which is a ZIP file, so extracting the video this way probably takes extra time.
You may need to switch away from VideoView and use a SurfaceView with two MediaPlayers, alternating between them -- one is playing while the next is preparing, so when the playing one ends you can switch to the new player. I have not tried this, and so I do not know what the ramifications might be. However, I know that this technique is frequently used for audio playback to transition from one clip to another.
Little late, but any reason that you can't use the following?
MediaPlayer.setLooping(true);
If you are using Kotlin
videoView.setOnPreparedListener(object : MediaPlayer.OnPreparedListener {
override fun onPrepared(mp: MediaPlayer?) {
//Start Playback
videoView.start()
//Loop Video
mp!!.isLooping = true;
Log.i(TAG, "Video Started");
}
});
Using Arrow Expression short form
videoView.setOnPreparedListener { mp ->
//Start Playback
videoView.start()
//Loop Video
mp!!.isLooping = true;
Log.i(TAG, "Video Started");
};
Answer to this is to remove the audio from the video and convert that to a .ogg file which can be looped seamlessly and then use the video without audio to loop round and this works.
Here is answer friends, you must use vv.resume in setOnCompletionListener class
[https://stackoverflow.com/a/27606389/3414469][1]

Categories

Resources