VideoView not supporting video in Android - 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 :)

Related

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

android- how to play video in VideoView?

I want to play video in a VideoView. which one is correct?
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
video.start();
}
});
or simply:
video.start();
Both method is right to play video but setOnPreparedListener is useful when you want to show ProgressBar when media is loading for play from SDCARD,webserver or streaming urls.
and if video.start(); called without setOnPreparedListener some delay occur during loading file then only black screen appear to user until video start.

How to play preroll?

I want to play preroll ad (Just like you tube) before playing any video live streaming in my app. Right now i am able to play video using URL through streaming but not able to find any way to play preroll please some one help me to achieve this goal.
Just provide a ArrayList of videoUrls for your media player. Use the OnCompletionListener interface and play the next (none-preroll) video when the preroll has been completed. From what Ive understood you can't feed the Android media player a list of urls, you have to setDataSource for each video.
//Starting the mediaplayer
private void playVideo() {
mediaPlayer.setDataSource(videoUrlList.get(playlistPosition));
mediaPlayer.prepareAsync();
}
#Override
public void onCompletion(MediaPlayer mp) {
//Look if the list has more videos
if (playlistPosition < videoUrlList.size() - 1) {
playlistPosition++;
if(mediaPlayer != null)
mediaPlayer.reset();
playVideo();
}else{
//If no more videos, quit MoviePlayer Activity
finish();
}
}

VideoView not playing on some devices

I have fragment which contains CameraPreview as background and in one of the corners i have 100x100dp video view which must play mp4 h.264 format video,
but for some reason some of the devices play other won't the problem that no error thrown it just not showing .
This is the function to prepare the video.
private void setupVideoView(){
String path = "android.resource://"+ getActivity().getPackageName()+"/"+R.raw.model_2;
Uri uri = Uri.parse(path);
mVideoView.setVideoURI(uri);
mVideoView.seekTo(1);
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
}
then after couple seconds i need to start the video, then i call
if(mVideoView != null){
mVideoView.start();
}
i had read about which codecs and format android accept and my video is perfect suit this requirements as i said above its mp4 h.264 format , any idea?

Categories

Resources