Jump to a specific time on youtube video (Android) - 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

Related

How to get current frame number from video in 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

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

MediaPlayer.seekTo() not seeking to position on Android

I'm working on an app in which the video is paused at 3 different intervals. After the second pause, if a button is clicked, it should start back from the previous location.
Eg. if it is currently paused at 1:30, then on click of a button, it goes to the previous bookmark, i.e. 00:45.
I thought using MediaPlayer.seekTo() will help me achieve this. But, seekTo() doesn't seek the position at all. The currentPosition stays the same even after a call to seekTo();
Here's my code.
mediaPlayer.setOnSeekCompleteListener(new OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
Log.d("VID_PLAYER","Seek Complete. Current Position: " + mp.getCurrentPosition());
mp.start();
}
});
and somewhere below, I have this...
mediaPlayer.seekTo(45000);
What is the problem? Why isn't seekTo(); working?
Any help would be appreciated.
I am currently testing it on Android v4.0.3 (ICS)
Try the code snippet given below to achieve more accuracy in seeking to specific positions of the media.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
mediaPlayer.seekTo(seekPosition,MediaPlayer.SEEK_CLOSEST);
else
mediaPlayer.seekTo((int)seekPosition);
Keep in mind that - as the accuracy increases, speed of seeking decreases. So while playing high resolution videos its advised not to use MediaPlayer.SEEK_CLOSESTmore often.
One of the reasons why Android is not able to do seekTo is because of strange encoding of the videos. For example in MP4 format so called "seek points" (i.e. search index) can be specified at the begining and at the end of the file. If they are specified at the begining of the file then seekTo will behave correctly. But if they are specified at the end of the file then seekTo will do nothing and video will start from the begining after seekTo.
This is confirmed bug-or-feature in Android 4.4.2.
P.S. On Youtube all videos are encoded with "seek points" at the begining of the file. So if you have this problem with seekTo in your app first of all check your app with some video files from Youtube. Perhaps you'll need to reencode your videos...
Does your problem have something to do with this bug:
https://code.google.com/p/android/issues/detail?id=4124
I recall encountering this about a year ago. I don't think I found a workaround at the time.
Since API 26, Android added a
seekTo(long msec, int mode);
By specifying a mode, we are able to tell to our MediaPlayer how to seek:
SEEK_PREVIOUS_SYNC: Has the same behavior as seekTo(int msec). It looks for the nearest 'seek point' (i.e. sync frame) backwards.
SEEK_NEXT_SYNC: Same as PREVIOUS, but looks forwards.
SEEK_CLOSEST_SYNC: This looks for the nearest sync frame given a msec time.
SEEK_CLOSEST: This seeks for the nearest frame given a msec time.
I faced a similar problem a couple days ago and by using SEEK_CLOSEST mode, this problem was solved.
For reference, check out these links: https://developer.android.com/reference/android/media/MediaPlayer.html#seekTo(long,%20int)
https://en.wikipedia.org/wiki/Video_compression_picture_types

Binding MediaPlayer to be played at a specific time

When using MediaPlayer, I noticed that whenever my phone stucks, the MediaPlayer glitches and then continues playing from the position in the audio it glitched.
This is bad for my implementation since I want the audio to be played at a specific time.
If I have a song of 1000 millisecond length, I want is the ability to set MediaPlayer to start playing at some specific time t, and then exactly stop at at time t+1000.
This means that I actually need two things:
1) Start MediaPlayer at a specific time with a very small delay.
2) Making MediaPlayer glitches ignore the audio they glitched on and continue playing in order to finish the song on time.
The delay of the functions is very important to me and I need the audio to be played exactly(~) at the time it was supposed to be played.
Thanks!
You will need to use possibly mp.getDuration(); and/or mp.getCurrentPosition(); although it's impossible to know exactly what you mean by "I need the audio to be played exactly(~) at the time it was supposed to be played."
Something like this should get you started:
int a = (mp.getCurrentPosition() + b);
Thanks for the answer Mike. but unfortunately this won't help me. Let's say that I asked MediaPlayer to start playing a song of length 3:45 at 00:00. At 01:00 I started using the phone's resources, due to the heavy usage my phone glitched making MediaPlayer pause for 2 seconds.
Time:
00:00-01:00 - I heard the audio at 00:00-01:00
01:00-01:02 - I heard silence because the phone glitched
01:02-03:47 - I heard the audio at 01:00-03:45 with 2 second time skew
Now from what I understood MediaPlayer is a bad choice of usage on this problem domain, since MediaPlayer provides a high level API.I am currently experimenting with the
AudioTrack class which should provide me with what I need:
//Creating a new audio track
AudioTrack audioTrack = new AudioTrack(...)
//Get start time
long start = System.currentTimeMillis();
// loop until finished
for (...) {
// Get time in song
long now = System.currentTimeMillis();
long nowInSong = now - start;
// get a buffer from the song at time nowInSong with a length of 1 second
byte[] b = getAudioBuffer(nowInSong);
// play 1 second of music
audioTrack.write(b, 0, b.length);
// remove any unplayed data
audioTrack.flush();
}
Now if I glitch I only glitch for 1 second and then I correct myself by playing the right audio at the right time!
NOTE
I haven't tested this code but it seems like the right way to do it. If it will actually work I will update this post again.
P.S. seeking in MediaPlayer is:
1. A heavy operation that will surely delay my music (every millisecond counts here)
2. Is not thread safe and cannot be used from multiple threads (seeks, starts etc...)

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