how to play videos one by one sequentually? - android

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

Related

VideoView and MediaPlayer seekTo always starts from beginning

I have some problem with seeking video played in VideoView and resuming it.
Here's some part of my code:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
videoView.start();
Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
}
});
}
});
videoView.setVideoURI('some local uri');
At some point I'm pausing video using:
videoView.pause();
After button click I'm seeking video to certain time, eg. 10 seconds (10 000 ms).
#OnClick(R2.id.ivMove)
void clickMove() {
videoView.seekTo(10000);
}
After that in log I see:
onSeekComplete1 10000
onSeekComplete2 0
So after calling videoView.start() it is starting video from beginning!
But when I call start() without seeking video (eg. one button calls pause() and second calls start() then video is playing correctly - from the moment when it was paused, not from beginning.
Could you tell me what I'm doing wrong?
The problem was caused by MediaPlayer - it is seeking to closest keyframe of the video and it turns out that my testing video has keyframes every ~30 seconds.
I changed to ExoPlayer https://github.com/google/ExoPlayer - it can seek to exact time.
There is a new overload for the MediaPlayer seekTo method. It has a second int parameter, in which you can pass a SEEK_CLOSEST constant. According to Google, "This mode is used with seekTo(long, int) to move media position to a frame (not necessarily a key frame) associated with a data source that is located closest to or at the given time." In testing my own app, it solves the problem for API level >= 26. I'm not sure if there are any good options prior to API level 26.
Try videoView.resume() instead of start()
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
videoView.resume();
Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
}
});
}
});
videoView.setVideoURI('some local uri');
Try this solution. It works like charm....
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
mp.start();
}
});
}
});

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

MediaPlayer.start() sound execution delayed

I've a strange problem with my app.
Every time a button is clicked, I call a method to execute sound.
The code:
public static void executeSound(Context context) {
if (isSoundEnabled(context)) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(context, R.raw.button_click);
}
mediaPlayer.start();
}
}
Where isSoundEnabled function verified if user has sound enabled inside app.
Sound is not executed each time I tap button, but after I tap other buttons, and sounds are executed all together.
So happen that I press three different buttons, and three sounds are executed only when I push a button for third time. How can I execute immediately the sound?
I have this problem on lollipop devices (the same code run perfectly on the same device but with kitkat)
Change your execute method to this
public static void executeSound(Context context) {
if (isSoundEnabled(context)) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(mContext, audioUriPath);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
}

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

android MediaPlayer Looping

I am trying to play a looping Ogg file, I tried enabling setLooping(true) but that had no effect so I tried onCompletionListener and that's not working either, could someone clarify what I am doing wrong?
musicPlayer = MediaPlayer.create(mContext, R.raw.overworld);
musicPlayer.setVolume(musicVolume, musicVolume);
// musicPlayer.setLooping(true);
musicPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
musicPlayer.stop();
musicPlayer.seekTo(0);
musicPlayer.start();
Log.d("Sound Manager", "Song Completed");
}
});
Following is my play function,
public void playSong(int id) {
try {
stopSong();
musicPlayer = MediaPlayer.create(mContext, id);
musicPlayer.start();
} catch(Exception e) {
// Ignored
}
}
It's known that MediaPlayer is having problems with ogg files.
You could preferrably switch to another file format.
The other thing is, I would go on trying with setLooping(boolean) as it's most likely using the same scheme and its much more clearly.
Calling seekTo() if the MediaPlayer Object is stopped causes the MediaPlayer to be in an invalid state. You can call pause() instead but I wouldn't call any of these method, why not just seeking? I would guess if you remove the musicPlayer.stop() it will work.
remove ....
musicPlayer.stop();
from
onCompletion(MediaPlayer mp)
onCompletionListener() is not called if your MediaPlayer is set to looping, BUT if you don't have it set to looping, you can always just use a completion listener like so
#Override
public void onCompletion(MediaPlayer mp) {
if(!mp.isPlaying()) {
mp.start();
}
mp.seekTo(0);
}
You also shouldn't call stop() because that stops playback completely, and it doesn't make sense to seek in a video/song that you are not playing.

Categories

Resources