VideoView/MediaPlayer stop on last frame - android

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

Related

How to play a video from Content Provider

I might have been using the wrong search tags, but here it goes.
In the App I'm developing the user is able to upload pictures and videos.
This through recording or gallery. I'm currently trying to show a video which the user just recorded. I'm taking the Uri from Intent.getData() after an intent with MediaStore.ACTION_VIDEO_CAPTURE.
public
void getCameraVideo() {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(ctx.getPackageManager()) != null) {
ctx.startActivityForResult(takeVideoIntent, FETCHER_VIDEOCAMERA);
} else {
Toast.makeText(ctx, Language.translate("error_phone_has_no_camera_app"), Toast.LENGTH_LONG).show();
ErrorClass.log("Tried to take video, but phone has no camera.");
}
}
So I have this content Uri to my video, now how should I get about into playing this video in an VideoView? I can't seem to find any decent solution to this issue.
I simply tried this in the activity where I'm showing the video, but it just results in a black VideoView.
view = new VideoView(activity);
((VideoView) view).setVideoURI(uri);
((VideoView) view).start();
I'm sure someone solved this already, I just can't seem to find it.
Once try as follows
view.setVideoURI(Uri.parse(path));
view.requestFocus();
view.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
//start playing here
mp.start();
}
}
Hope this will helps you.
Providing a code snippet would help us figure out your problem but here goes.
Since you are implementing this in your activity, you should be able to access the VideoView from the layout. Hence I am assuming you should be able to do something like this...
String video_url = "some_url";
VideoView videoView = (VideoView)findViewById(R.id.video_view_id);
MediaController controls = new MediaController(YourActivityClassName.this);
videoView.setMediaController(controls);
videoView.setVideoURI(Uri.parse(video_url));
videoView.start();
Hopefully, this should help you. If not, let us know and we can help you further.
P.S. Check out the android docs for more information about the videoview:
http://developer.android.com/reference/android/widget/VideoView.html
You may need to call videoView.play() to see if your uri is correct.

Android videoview causes blocking UI elements

Hi in my android application I am using videoview for streaming my videos. I face very unwanted behaviour from my videoview. The scenario is like this. I have activity A and activity B. Activity A has one simple button and on click of that button I am starting activity B which contains videoview and starts playing video as soon as it start my activity. So my observation is like this: Once I start Activity B it will call setVideoURI and start(). there are few callback methods one of them is setOnPreparedListener. when I call start() after some time It is executing setOnPreparedListener and after that it will start playing video. But in between before executing setOnPreparedListener if I come back to Activity A it will block that activity UI for some time. But if I wait till setOnPreparedListener get executes and then come back to Activity A then its working properly. This is not happening with all devices only with google devices like moto g and nexus. But I tried with htc or intel device it is working properly. My code looks like :
VideoView mVideoView =(VideoView)findViewById(R.id.myVideo);
//Creating MediaController
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(mVideoView);
//specify the location of media file
Uri uri=Uri.parse("http://abcExample.com/playlist.m3u8");
//Setting MediaController and URI, then starting the videoView
mVideoView.setMediaController(mediaController);
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
mVideoView.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.i("this is video view sample ... ", "this is video view sample ... this is on error listener ");
return true;
}
});
mVideoView.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
Log.i("this is video view sample ... ", "this is video view sample ... this is on prepared listener ");
}
});
mVideoView.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
Log.i("this is video view sample ... ", "this is video view sample ... this is on complete listener ");
}
});
Am I missing something or doing something wrong? Need some help. Thank you.
I'm having the same behaviour with videoview. I was looking for a solution but i didn't find anything. Maybe, It's possible that making a new task fix that issue. I'll try it

Get the video time while playing android

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

VideoView not supporting video in Android

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

Seamless video Loop with VideoView

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]

Categories

Resources