I am trying to getDuration() of a stream but its always returning -1. I googled many links but none is having the issue solved. I am using videoview for streaming content in my app. However calling getDuration() in onPreparedListener is always returning -1.
Here is my code:
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mSeekbar.setMax(mp.getDuration());
Utils.dismissProgressDialog();
mVideoView.start();
}
});
And here is what i am getting upon hitting my m3u8 link:
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1900000,RESOLUTION=1280x720
720/720.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=514000,RESOLUTION=854x480
480/480.m3u8
I need to have duration because of DVR implementation so need to figure out a way of doing it.
I want to remove the first 3 seconds from a mp4 video file on android, how can I do it with Android API?
If you are using VideoView for showing the video, try using below code.
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
videoView.seekTo(3000);
}
});
I have a problem with seekTo() method from MediaPlayer. It seems it doesn't work at all! I followed other threads regarding this issue and tried different workarounds but still seekTo() does nothing! Here is my code:
private MediaPlayer mMediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
final VideoView videoView = (VideoView)findViewById(R.id.videoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
final Uri video = Uri.parse("android.resource://"+getPackageName()+"/"+R.drawable.video_myopia);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer = mp;
System.out.println("CURRENT POSITION 0: " + mp.getCurrentPosition());
mp.seekTo(3000);
System.out.println("CURRENT POSITION 1: " + mp.getCurrentPosition());
mp.start();
System.out.println("CURRENT POSITION 2: " + mp.getCurrentPosition());
}
});
The System.out.println() shows the following output:
POSITION 0: 0
POSITION 1: 3000
POSITION 2: 3000
(note: I also tried to start() before seekTo() and then start() again after the seekTo() method)
so after the mp.start() line the mp.getCurrentPosition() says that it is at 3000 but the problem is that it's not! The video starts from the beginning. In each situation that I tried seek, after seekTo(ms) and start() the video starts from the beginning no matter what.
I must mention that I also used mp.setOnSeekCompleteListener() to know when the seek finishes. In this callback I tried to start() the video again, but it always starts from the beginning.
Can someone help me please? I will be grateful for any suggestion :). Thanx!
I discovered that my videos were not seekable, and that's why every time I used seekTo it made my video start from the beginning. In order to solve the problem I converted them with H.264
I just want to ask how to get the time of the video while it plays. I am using videoview. Let us say for example, when you are playing a video with a length of 10 minutes and you are already in 4th minute, how can I get the value of 4 minute? Any help would be appreciated. Thank you.
Below is my code in playing video.
private void PlayVideo(){
MediaController mc = new MediaController(getActivity());
mc.setAnchorView(videoMovie);
Uri video = Uri.parse(url);
videoMovie.setMediaController(mc);
videoMovie.setVideoURI(video);
videoMovie.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
videoMovie.start();
}
});
}
Look this tutorial
and if you want to get the time of the video while it plays, this code is work well
videoView.getDuration()
I have the following code to take a video as a raw resource, start the video and loop it but I need the video to loop seamlessly as of now when it comes to an end of the clip and starts the clip again the transition between causes a flicker for a split second, which I really can't have for my app.
public class Example extends Activity {
VideoView vv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vv = (VideoView)findViewById(R.id.VideoView01);
//Video Loop
vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
vv.start(); //need to make transition seamless.
}
});
Uri uri = Uri.parse("android.resource://com.example/"
+ R.raw.video);
vv.setVideoURI(uri);
vv.requestFocus();
vv.start();
}
}
The clip is only 22 seconds long but was created to be seamless so it is possible to work without the delay.
Try this it will work 100%
VideoView videoView;<---write this in outside of method or else declare it as final variable.
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
In Kotlin simply use
videoView.setOnPreparedListener { it.isLooping = true }
Not sure if this helps years later, but I used
vv.start();
vv.setOnCompletionListener ( new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
vv.start();
}
});
and it has a seamless loop
The pause is for the underlying MediaPlayer to refresh its buffers. How long that will take will depend on a number of factors, many of which are outside your control (e.g., speed of CPU, speed of on-board flash storage).
One you can control is to get your video out of the resource and into the filesystem. Resources are stored in the APK, which is a ZIP file, so extracting the video this way probably takes extra time.
You may need to switch away from VideoView and use a SurfaceView with two MediaPlayers, alternating between them -- one is playing while the next is preparing, so when the playing one ends you can switch to the new player. I have not tried this, and so I do not know what the ramifications might be. However, I know that this technique is frequently used for audio playback to transition from one clip to another.
Little late, but any reason that you can't use the following?
MediaPlayer.setLooping(true);
If you are using Kotlin
videoView.setOnPreparedListener(object : MediaPlayer.OnPreparedListener {
override fun onPrepared(mp: MediaPlayer?) {
//Start Playback
videoView.start()
//Loop Video
mp!!.isLooping = true;
Log.i(TAG, "Video Started");
}
});
Using Arrow Expression short form
videoView.setOnPreparedListener { mp ->
//Start Playback
videoView.start()
//Loop Video
mp!!.isLooping = true;
Log.i(TAG, "Video Started");
};
Answer to this is to remove the audio from the video and convert that to a .ogg file which can be looped seamlessly and then use the video without audio to loop round and this works.
Here is answer friends, you must use vv.resume in setOnCompletionListener class
[https://stackoverflow.com/a/27606389/3414469][1]