How to play a video from Content Provider - android

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.

Related

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 loses back stack

I have to refactor some old code and am currently up against a wall. Apparently this code used to load a different kind of video format but now it pulls in an .MP4, which means the WebView it used to call can no longer play the video. Given the extremely finite amount of time I have, I tried to kludge in a VideoView to play the video. The video loads and plays, but the activity stack is lost when the VideoView is started.
I have overwritten the MediaController class to at least be able to capture the back key event. My question is: how can I load this VideoView and retain the activity stack?
I appreciate your time and patience.
Here is the code calling the VideoView:
public void onListItemClick(ListView l, View v, int position, long id) {
Video video = (Video) this.getListAdapter().getItem(position);
track("Video", "VOD", video.title);
String url = video.link.substring(29);
if(DEBUG_MODE)
Log.d("TDF", url);
url = Kriptonic.getEncryptedURL(url);
videoView = new VideoView(_myActivity);
_myActivity.setContentView(videoView);
Uri uri = Uri.parse(url);
mc = new CustomMediaController(_myActivity);
videoView.setMediaController(mc);
videoView.setVideoURI(uri);
videoView.start();
}
EDIT:
I solved this by not being lazy. Just creating a video like that without attaching it to its own activity was just bad coding. I created a simple activity, started an intent in the above fragment to call that activity and all is fine.
...For everything but ICS. It won't run in ICS. But that will be a different question.
Cache the previous view in a temporary variable and restore the contentView to it when back is pressed.
mainView = _myActivity.getContentView();
videoView = new VideoView(_myActivity);
_myActivity.setContentView(videoView);
When back button is pressed
#Override
public void onBackPressed() {
if (mainView != null) {
_myActivity.setContentView(mainView);
mainView = null;
} else {
super.onBackPressed();
}
}

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

VideoView/MediaPlayer stop on last frame

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

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