How to get current frame number from video in android? - android

Is there a way to get the current frame number of the video while video is playing, or when the video has paused? using videoview.

With the VideoWiew, you can retrieve the playback time in milliseconds with getCurrentPosition. Then you have to make some calculations based on the frame rate of the video itself, for which I invite you to read here.

You should be able to get it by calling getCurrentPosition() on your videoView. Such as(videoView.getCurrentPosition). and place that inside a pause button or something along those lines.
getCurrentPosition() – Returns an integer value indicating the current position of playback.
Source: https://www.techotopia.com/index.php/Kotlin_Android_Video_Playback_using_the_VideoView_and_MediaController_Classes

Related

Android ExoPlayer PlayList Pause Current Track When It Finishes

My goal is to pause the Current track right after it finishes, but the default behavior of playlist playback will not pause until the whole Playlist is finished.
I've tried using onPositionDiscontinuity() but it is called after the track has changed to the next one.
override fun onPositionDiscontinuity(reason: Int) {
super.onPositionDiscontinuity(reason)
if (reason == SimpleExoPlayer.DISCONTINUITY_REASON_PERIOD_TRANSITION) {
Log.v("xxx", "called!") //not called at the end of current track
}
}
And it seems like not supported natively (by official):
https://github.com/google/ExoPlayer/issues/3773
You can use the setPauseAtEndOfMediaItems method available on SimpleExoplayer.Builder like so:
player = SimpleExoPlayer.Builder(context)
.setPauseAtEndOfMediaItems(true)
.yourOtherOptions()
.build()
Unfortunately, there is no direct callback available to notify the end of the last frame of the current track. The only thing available with the ConcatenatingMediaSource, to know the end of a track is onPositionDiscontinuity(), but as you know that would be dispatched only after the first frame of the next track is already rendered. So in that case I think we can have the below possibilities wrt your use case:
Use VideoFrameMetadataListener interface and override the onVideoFrameAboutToBeRendered(), which would be called on the playback thread when a video frame is about to be rendered. In your case just before the next track rendering. link
Get the track duration [getDuration()] and keep getting the current playback position using getCurrentPosition() in every second(or any other time interval). And pause the playback when it returns the specified time. You can use a CountDownTimer for this and in the timer callback, onTick(), invoke getCurrentPosition() for the current track. 
Use PlayerMessage to fire events at specified playback positions: The playback position at which it should be executed can be set using PlayerMessage.setPosition.link
Use onMediaItemTransition(): Called when playback transitions to another media item. Here generally we update the application’s UI for the new media item. So instead of updating the UI, we can pause the playback. Not sure if this gets called before or after onPositionDiscontinuity(). Feasibility needs to be verified.

Video view seeking inconsistencies

I have a video that I record at 60 frames per second or 30 frames per second. Now I view that video in a video view. I want the video to skip frame by frame on each skip button tap.
The issue is that the seek to method looks for nearest key frame and move to that position. So sometimes when I am skipping frames it shows inconsistent behaviour due to that.
So how can I achieve this?
This is the code I am using to seek the video view:
int currentPosition = videoView.getCurrentPosition();
double increment = 1000/60;
currentPosition += Math.round(increment);
if (videoLoader.canSeekForward()) {
videoLoader.seekTo(currentPosition);
}
So the method videoView.getCurrentPosition() sometimes returns wrong value.
I solved this issue by replacing video view with ExoPlayer. ExoPlayer has a method called setSeekParameters. Just use that method with the below parameter:
setSeekParameters(SeekParameters.EXACT)
and then the seekto method will seek to exact frames and not just the nearest key frames.

Exoplayer change current video seamlessly

I want to change current playing video to another seamlessly. But there is a little bit delay before the next video will be playing. I don't know what the next video will be. How I can do this?
You can use playlists with ExoPlayer. This way the playback transitions at the end of item 1 to 2 without buffering.
DynamicConcatenatingMediaSource mainSource = new DynamicConcatenatingMediaSource();
mainSource.addMediaSource(mediaSource1);
player.prepare(mainSource);
// later...
mainSource.addMediaSource(mediaSource2);
As soon as you know the second media you can add it while the player is playing.
https://medium.com/google-exoplayer/dynamic-playlists-with-exoplayer-6f53e54a56c0

Jump to a specific time on youtube video (Android)

I already loaded a video on a mobile phone using YouTubePlayerView. I was wondering if anyone could help me with the code to jump to a specific time (e.g., 2m4sec) on a video.
You should use the YouTubePlayer's seekTo methods to jump a video to a specific time.
YouTubePlayer.seekToMillis (int milliSeconds)
More about this method
Or:
YouTubePlayer.seekToRelativeMillis (int milliSeconds)
More about this method
If you want to start a video right away from a certain time, you can use:
YouTubePlayer.loadVideo(String videoId, int timeMillis)
More about this method

How can I perform an action at a certain point in time during a video in Android?

I am creating a program which requires me to change a setting in the program when the video reaches specific points (at 1/3 of the video's completion, 2/3's and on completion). Android has a built in callback method for completion so performing an action at that point in time is not difficult. However, I don't know how to go about checking when the video has reached 1/3 and 2/3's of completion.
Using a MediaPlayer control you will get
the total duration of your media file in milliseconds:
myMediaPlayer.getDuration()
you will implement a thread that check every second for the current position at 1/3, 2/3 and 3/3 of the videos completion, with
myMediaPlayer.getCurrentPosition(); //***current Position in milliseconds.

Categories

Resources