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

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

Related

Playing sounds in Android in certain order

In an Android app I am using MediaPlayer to play sound files.
This is just for personal use and will not be published.
I have several references to the sound files:
MediaPlayer dooropen = MediaPlayer.create(MainActivity.this, R.raw.dooropen);
MediaPlayer doorclose = MediaPlayer.create(MainActivity.this, R.raw.doorclose);
//...
For example the length of the dooropen sound clip is 2 seconds so after I play it I sleep for 2.5 seconds and then play the doorclose sound clip, like so
dooropen.start();
try{ Thread.sleep(2500); }catch(InterruptedException e){ }
doorclose.start();
The issue I am having is some of my sound files are not playing in the order I have them in.
There does not seem to be any reason why certain sound files do not play, because if I play them at the top of my onCreate() procedure they all play, it is only when I try and play them in a certain order.
You should implement the setOnCompletionListener() of the mediaplayer to get a callback when playback has completed and then load another audio file that needs to start playing.
See MediaPlayer Documentation about the mediaplayer state.
Yes you can use MediaPlayer along with oncompletionListener or you may try reseting the mediaplayer after one audio is completed. example code here. You may also use session id to keep track of which file was playing and which to start now.
mPlayer = new MediaPlayer();
//set other attributes here
mPlayer.setAudioSessionId(1);
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//check which audio session was playing and set new datasource and session
mPlayer.reset();
//set other data source here and different session id
}
});
Hope it solves your problem.
If you want to play sound in order, try this:
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp2.start();
}
});
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp3.start();
}
});
mp3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp1.start();
}
});

multiple videos play one after the another

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

how to play videos one by one sequentually?

how to play videos one by one simultaneously?
Now i have a problem, I have more than 3 videos,Now i have to play first videos, can't move next videos. How to do that...
vidview.setVideoPath(vidname[i].toString()+".3gp||.mp4");
System.out.println("\n\n\n File name is: "+vidname[i].toString());
vidview.requestFocus();
vidview.start();
vidview.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.stop();
run();
}
});

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 ...
}

Categories

Resources