i have an mp4 i am trying to view on my android in a video view but for some reason when i use seekto i can only seek to every 10 seconds. if i seek to 34 seconds it seeks to 30 and if i seek to 36 it seeks to 40. and so on for every thing i try to seek to. i have heard something about seek points in an mp4 file. is this whats causing my seek to fail?
videoView = (VideoView)findViewById(R.id.watchMp4View);
videoView.setVideoURI(Uri.parse(uri));
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
videoView.seekTo(timeToSkip); //
mediaPlayer.seekTo(timeToSkip);
mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mediaPlayer) {
videoView.start();
}
});
this issue is related to the creation of the .mp4 file, your file need to be prepared to allow seeking, in the encoding process check the Keyframe property, try with a shorter keyframe.
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 am using VideoView to play video.
If the video is not supported by the phone (such as my phone supporting only 480p video, not 720p which is the video I'm trying to play), it shows the dialog
Sorry,This video cannot be played
I want to do something after I click the dialog's "OK" button.
Where can I add the code to do this?
My code as below:
vv = (VideoView)this.findViewById(R.id.screen_video);
Uri uri = Uri.parse(FlePath);
vv.setVideoURI(uri);
vv.start();
After your code paste this.If you get this error then oncmpletion listner will call.
vv.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//release your MediaPlayer Resource
//do whatever you want
}
});
Hope this help you :)
is it possible to stop a video on its last frame? I am using the code below:
VideoView video = (VideoView) findViewById(R.id.menu_video);
final String uriPath = "android.resource://com.my.project/raw/myvideo"
Uri uri = Uri.parse(uriPath);
video.setVideoURI(uri);
I've tried to load the video from the sd card, to pause it in a completion listener and to find out something in the web... Now I am thinking about hiding it and using an ImageView as "fake" last Frame. Does anyone know a better solution?
To get the last frame of a video you can skip to its end by using:
video.seekTo(video.getDuration());
firstly you must register listner like this:
mMediaPlayer.setOnCompletionListener(this);
second:
public void onCompletion(MediaPlayer arg0) {
Log.d(TAG, "onCompletion called");
this.finish();
}
and you should implement OnCompletionListener in your class
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]