Android - changing/replaying video in videoView - android

App I'm developing contains many short (1-2 sec) videos.
The videos are displayed in one activity. User can either replay video (possibly while video is beeing played) or change actual video.
Part of code changing video:
String videoPath = getVideoPath();
videoView.setVideoPath(videoPath);
videoView.start();
Those 3 lines already causes app to load new video and play it.
Problem starts after video is completed. From this point loading new video causes many problems (Like sometimes for half a movie only sound is played while screen is black blank). There are similar problems with replaying video (which I end up with calling 3 lanes from above).
It seems like android after completing movie releases resources or something like this (and that's why I am settings same path, when I want to replay video).
Ideally I would want video to simply pause and seekTo to beggining of movie after finished playing (but I cannot do this in OnCompletedListener, since it already changed state to stopped...).
Can I somehow achieve this? (By this I mean -> after completed video pauses and seekTo to beginning)
I already tried all combinations of pausing vidoes, suspending them, setting OnPreparedListener, setting OnCompletedListener.
Thx!

Try something like
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
mVideoView.start();
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.reset();
mVideoView.setVideoPath(file.getAbsolutePath());
mVideoView.start();
}
});

stop the playback, change video path, start video
videoView.stopPlayback();
videoView.setVideoPath(newVideoPath);
videoView.start();

I refactored my code in a following way: Everytime I need new video I reload whole activity. It works most of times, but now instead of video blackness at the beginning of playing I sometimes get "Cannot open media file" error.
Has it something to do with android resource managment? I release mediaPlayer in onCompletionListener.
Has anyone had such problem with playing many videos from external storage?

You can do something like this:
videoView.setOnCompletionListener(MediaPlayer.OnCompletionListener { mp ->
mp.seekTo(0);//go to second 0
mp.start()// start again
})
I discover in this link:
https://developer.android.com/reference/android/media/MediaPlayer

Do this.
videoView.setOnPreparedListener { mp ->
mp.isLooping = true
)

Related

Android MediaPlayer- play two videos simultaneously with sync

My problem is similar to this one, but there was no correct answer, so I thought I give it a go.
In my application I've got two video players (TextureView + MediaPlayer- based on VideoView) playing from URL side by side on one screen. I got it working quite right. As I need then them to play simultaneously I synced their controllers, to when I start one of them the second starts too (the same with pausing, seeking, buffering, etc).
videoView1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.setVolume(0.0f, 0.0f);
firstVideoPrepared = true;
tryStart();
}
});
videoView2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
secondVideoPrepared = true;
tryStart();
}
});
private void tryStart() {
// here is also buffered data level check
if (firstVideoPrepared && secondVideoPrepared) {
videoView1.start();
videoView2.start();
}
}
The problem is that despite pausing both videos for buffering, when I start() them they sometimes get out of sync. Let's say that there is the same video in both players- I need them to be sync almost ideally as I can see any difference. Is there any way to keep them synced so precisely?
I've read about libraries like ExoPlayer, FFMPEGMediaPlayer or MediaPalyer-Extended but none of them seems to have what I need.
Is it even possible to keep that accuracy of videos sync?

Android-fullscreen seamless video loop in background

I am created an Android app thta needs to have a fullscreen seemless video loop playing in the background. By 'in the background' I mean that there will be buttons on top of the video.
I've read these threadw already
playback video full screen
Integrating video file in android app as app background
but I'm still confused about the following
1 Is the mediaplayer needed for video playback?
2 Will using OnCompletionListener create a 'seamless' loop or will there be a 'hiccup' as the video loops?
Use the setOnPreparedListener to tell the MediaPlayer to loop and start
videoview.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
mp.start();
}
});
This is seemless on some devices, but can cause a frame or two of stutter on others :/

VideoView seekto() function extremely inconsistent

I am trying to Seek to a particular location in a video in Android and I am completely stuck because of the inconsistencies it is showing in its behaviour. Here's a list of things I ve done
VideoView.Seekto goes to 5:19 for one video, 5:17 for one video and 5:32 for another for the same milliseconds(326000 ms)!
Since VideoView does not support onSeekListener, I've modified the
source VideoView and added support for it. Yet it does not pause and
start seeking from where I want it to - there is always a lag! The
onSeekListener is called immediately some 6 s before where I want it
to stop. In many cases the seek bar shows the right time and suddenly jumps back a few seconds even though I am calling video.start
from onSeekCompleteListener
Why is this so inconsistent ? Is there a definite way of seeking to where you want to go in milliseconds in videoview? I know I should use MediaPLayer + Surface but since VideoView is just a wrapper of the two I am modifying it at source but to no avail.
I am aware of this : http://code.google.com/p/android/issues/detail?id=9135
But is there any way to get around this and have a definite way of
1.) Seeking to the exact time in milliseconds
2.) Pausing and resuming at the exact time?
You have to wait for the seeking to complete.
VideoView does not have a OnSeekCompleteListener() but you can access the MediaPlayer from the onPrepared method of the VideoView and then set the OnSeekCompleteListener, like this :
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
//Seek completed. Move seekbar
}
});
}
});
10 years later and I came across the same question exactly.
Any way, for me, the solution was to use the mediaPlayer from inside VideoView (Android Oreo 8.0+):
Explanation:
videoView default seekTo function use mediaPlayer default seekTo function (source)
mediaPlayer default seekTo overload is the same as seekTo(long, int) with SEEK_PREVIOUS_SYNC mode, you should use SEEK_PREVIOUS_SYNC only if you want to seek to a sync frame that has a timestamp earlier than or the same as milliseconds given,
However, SEEK_CLOSEST will seek to a frame that may or may not be a sync frame but is closest to or the same as milliseconds.
I know this is not the proper solution to your question but take a look into this library that is being made over the Google's default video view
https://github.com/brianwernick/ExoMedia
It has all the functionality and more that is being supported by default video view.
You can use this video view and emVideoView.seekTo(1000); to jump to 1000 millisecond in the video. You can also have setOnSeekCompletionListener to do process when seek complete.
I solved this problem like this
the seekTo() function doesn't work in VideoView

Android Media Player - Getting the number of times a video played in SetLooping

I am Playing the video in loop using set Looping(true) option and i will stop the media player after a particular event happened. It is working fine. But i want to know the number of times that my video got played in the loop.
MediaPlayer doesn't provide that information, you need to make it loop yourself and count how many times it has restarted. To do this, something in your app will need to extend OnCompletionListener and do something like
int count = 0;
public void onCompletion(MediaPlayer mediaPlayer) {
count++;
mediaplayer.seekTo(0);
mediaplayer.start();
}
And you need to set mediaPlayer.setLooping(false)

Android: MediaPlayer gapless or seamless Video Playing

I can play the videos fine back to back by implementing the OnCompletionListener to set the data source to a different file. No problems there. I call reset() and prepare() just fine.
What I haven't been able to figure out, is how to get rid of the 1-2 second gap screen flicker between the data source change and the new video starting. The gap shows a black screen, and I haven't found any way to get around it.
I've tried setting the background of the parent view to an image, but it manages to bypass that. Even if the SurfaceView is transparent (which it is by default.) I've also tried to have the multiple video files played at the same time, and switching mediaplayer's display when one ends and the other is supposed to start.
The last thing I tried, was to have a second view in the background that I show temporarily while the video is "preparing" and removing it when the video is ready to start. That also wasn't very seamless.
Is there any way to get rid of that gap. Running a video in a loop works wonderfully and does exactly what I want with the exception that it's looking through the same video instead of playing a different one that I pick.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#drawable/background"
android:layout_height="fill_parent">
<SurfaceView
android:id="#+id/surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
</SurfaceView>
</FrameLayout>
Player.java
public class Player extends Activity implements
OnCompletionListener, MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
private MediaPlayer player;
private SurfaceView surface;
private SurfaceHolder holder;
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.main);
surface = (SurfaceView)findViewById(R.id.surface);
holder = surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void onCompletion(MediaPlayer arg0) {
File clip = new File(Environment.getExternalStorageDirectory(),"file2.mp4");
playVideo(clip.getAbsolutePath());
}
public void onPrepared(MediaPlayer mediaplayer) {
holder.setFixedSize(player.getVideoWidth(), player.getVideoHeight());
player.start();
}
private void playVideo(String url) {
try {
File clip = new File(Environment.getExternalStorageDirectory(),"file1.mp4");
if (player == null) {
player = new MediaPlayer();
player.setScreenOnWhilePlaying(true);
}
else {
player.stop();
player.reset();
}
player.setDataSource(url);
player.setDisplay(holder);
player.setOnPreparedListener(this);
player.prepare();
player.setOnCompletionListener(this);
}
catch (Throwable t) {
Log.e("ERROR", "Exception Error", t);
}
}
I too have the same problem as outlined by below link
VideoView Disappears for a second when video is changed
But this issue wont occur if you try using Android 4.0+ (ICS). I started to port VideoView.java and MediaPlayer.java from 4.0 to my app , but thats seems complex and no luck till now. Basically it seems a bug in the native code of the older versions.
after too much wasted time trying to figure out how to play consecutive videos without the "gap", i'm leaning towards impossible. unless of course you're able to dig down to the native level and implement your own player, Android's media player simply doesn't support seamless playback as of the moment.
I've not done this with video playback on a MediaPlayer but I've done something similar with audio files when a stream gets interrupted because a user has switched from 3G to Wifi.
I think the delay that you're seeing is whilst the media player is buffering the input file.
So maybe you can define both players at the start? You should do define the datasource and prepare both players but only start one of them.
Then in your onCompletionListener you can flip them over instead of resetting the existing one and setting a new datasource.
player.release();
player = flipPlayer;
flipPlayer = null;
player.start();
Obviously you'd need to either use a different onPreparedListener for flipPlayer or take the player.start() out of the onPrepare. (Since you're calling it synchronously I wouldn't have thought this was an issue).
I don't think it's possible.
Reason: Mortplayer was also available for windows mobile and one of its strengths was that it supported gapless play. However it doesn't in the android version of the app, and the developer itself writes that the SDK does not allow it on xda:
http://forum.xda-developers.com/showpost.php?p=5364530&postcount=5
HTH, Daniele
Did you try to have ready (opened/prepared) 2 VideoView's, with one being visible, other invisible and stopped, and ass soon you get OnCompletionListener callback, make 2nd one visible, start it, and hide 1st one.
In meantime, as 2nd one plays, you can call open/prepare on 1st VideoView to open/prepare another file.
#user1263019 - were you able to port Android 4.0 MediaPlayer to your app? I'm facing the same issue and I'm looking for a nice solution. My case is having an image over the SurfaceView which should be hidden in order to show the video playing, but there is a gap between calling start() and the actual start of the video. The only solution so far is to start a Thread that checks if getCurrentPosition() is > 0.
On the topic - I think it is not possible to have gapless playback, though Winamp claim to have such abilities. A dirty hack is to prepare the second player several seconds before the end of the playback of the first player and then call start() of the second player 100-200ms before end of playback.
In your exoplayer implementation (or mediaplayer) use a handler to repeatedly post a runnable, while playing, that gets the current tracking time and passes it to a callback:
public interface MediaAction {
public void onTrackingChanged(long currentPosition);
}
public class ExoPlayerImplementation implements Exoplayer {
..
private MediaAction mediaActionCallback;
public void setMediaActionCallback(MediaAction ma) {
mediaActionCallback = ma;
}
public void releaseMediaAction() { mediaActionCallback = null; }
private Handler trackingCallback = new Handler(Looper.getMainLooper());
Runnable trackingTask;
private Runnable getTrackingTask() {
Runnable r = new Runnable {
#Override public void run() {
long currentPosition = getCurrentPosition();
if (mediaActionCallback != null) {
mediaActionCallback.onTrackingChanged(currentPosition);
}
// 60hz is 16 ms frame delay...
trackingCallback.postDelayed(getTrackingTask(), 15);
}
};
trackingTask = r;
return r;
}
public void play() {
// exoplayer start code
trackingCallback.post(getTrackingTask());
}
public void pause/stop() {
trackingCallback.removeCallbacks(trackingTask);
}
..
}
And now you can track when the current position actually has changed--if it has changed, then you can show your video output view / swap views / remove preview (ie, use MediaExtractor to grab initial frames, overlay as preview in an ImageView then hide once current position is increasing). You can make this code more complete by combining it with the state listener: then you know if you are buffering (check buffering position versus current position), actually playing, paused or stopped with proper callbacks (in MediaAction). You can apply this method to both MediaPlayer and ExoPlayer classes (since it is just an interface and handler).
Hope that helps!

Categories

Resources