Android Exoplayer : Get currentTime for a live stream - android

How do you get the current time for a live video(rtmp) when using Exoplayer?
I tried
player.getCurrentPosition()
but the values start from 0 and not the actual time of the live stream. Is it possible to get the actual video time?

In this case, I implement with subtraction between date now with user start date playing like this. dateStartPlaying is initialized when we want to play some content / change the content.
private long getCurrentTimeLiveStreaming(){
return new Date().getTime() - dateStartPlaying.getTime();
}

Related

How do I set duration of a video in ExoPlayer?

I need to set duration of a video for a thumbnail to 5 seconds and loop it. I got the loop part, but I can't find documentation for setting specific duration
I tried exoPlayer.seek(duration) but that didn't work
You may use ClippingMediaSource:
ClippingMediaSource​(MediaSource mediaSource, long startPositionUs, long endPositionUs)
Creates a new clipping source that wraps the specified source and provides samples between the specified start and end position.
You can convert to have a new media source and set this new media source for your ExoPlayer:
// Create a new media source with your specified period
val newMediaSource = ClippingMediaSource(mediaSource, 0, 5_000_000)

Showing video play progress while selecting the video range for trimming

I am designing an android video editor app and one of the feature is to trim video, selected from gallery. I can give an option to select the range using the RangeSlider, displayed at the bottom of the VideoView, to the user and then use FFMPEG library to trim the video.
But i am not able to show the progress of the video being played, within the selected range, on the RangeSlider.
Not sure if i am approaching properly, hence please provide me a solution to achieve this.
When you change the bounds of the RangeSlider, you need to calculate the startTime and endTime of the video. Once you are able to calculate the startTime and endTime, you need to create a ClippingMediaSource instance.
public ClippingMediaSource(MediaSource mediaSource, long startPositionUs, long endPositionUs)
ClippingMediaSource takes three paramters:
MediaSource
startPositionUs
endPositionUs
You can create media source by following the below snippet:
fun getMediaSource(file: String): MediaSource {
return ProgressiveMediaSource.Factory(DefaultDataSourceFactory(context, userAgent))
.createMediaSource(MediaItem.fromUri(Uri.parse(file)))
}
After creating the MediaSource you can pass on the values for start and end time you had calculated.
Note: startPositionUs and endPositionUs are in micro-seconds.
Once done, you can pass this media source to your ExoPlayer and it will play only the selected/trimmed part of your video.

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

Android Mediarecorder recorded video duration different from expected duration

I recorded a video using android Mediarecorder.
(Main Problem: I need to know the exact startTime[System time] and endTime[System time] of the video and the [endTime - startTime] must match the duration of the actual video)
MediaRecorder.start
startTime = System.currentTimeMillis()
then on stop
MediaRecorder.stop
endTime = System.currentTimeMillis()
I am expecting the video to have this duration
expected_duration = (endTime - startTime)
However, the
expected_duration is always more than the actual duration of the
video.
My suspicion is that MediaRecorder.start is slow, it took some time before it actually started writing the frames into a video.
So now, is there anyway to get notified when the MediaRecorder started writing the first frame into a video? or is there any way I can figure out the exact System startTime of when video actually started recording.
thanks for reading, and appreciate any comments, opinions or suggestions. ^^
The best way I found to get real start time (and still I'm not sure it's accurate enough) is to find the duration and then subtract it from the endTime like that:
MediaRecorder.stop
endTime = System.currentTimeMillis()
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//use one of overloaded setDataSource() functions to set your data source
retriever.setDataSource(this, Uri.fromFile(file));
String time =
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long movieDurationInMillis = Long.parseLong(time );
long startCaptureTimeMillis = endTime - movieDurationInMillis;

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

Categories

Resources