How to implement progressDialog in videoview when buffering video [like youtube] - android

I have implemented this code.
http://www.androidbegin.com/tutorial/android-video-streaming-videoview-tutorial/
but it showing progressdialog to whole activity, but my requirement is to show progressDialog only in videoview, like wise showing in youtube when buffering the video.

With following code you can detect while video is buffering :
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START)
bufferingDialog.show();
if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END)
bufferingDialog.dismiss();
return false;
}
});
}
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
bufferingDialog.dismiss();
return false;
}
});
Also recommend to see this library.

This may help you try this way
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<VideoView
android:id="#+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
ProgressBar progressBar = null;
VideoView videoView = null;
String videoUrl = "video path here";
Context context = null;
#Override
public void onCreate(Bundle iclic) {
super.onCreate(iclic);
context = null;
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.videoview);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
Uri videoUri = Uri.parse(videoUrl);
videoView.setVideoURI(videoUri);
videoView.start();
progressBar.setVisibility(View.VISIBLE);
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1,
int arg2) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.GONE);
mp.start();
}
});
}
});
}
}

Related

How to play video from URL in videoview in Android?

I am trying to implement video player in Android using VideoView. I have video uploaded on server I am fetching video from the server and trying to play in VideoView. I have fetched video successfully but unable to play in player.
Below is my code:
MediaController mediacontroller = new MediaController(getActivity());
mediacontroller.setAnchorView(videoView);
videoView.setMediaController(mediacontroller);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
videoView.setVideoURI(Uri.parse(video_url));
videoView.start();
}
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(getActivity(),"Error",Toast.LENGTH_SHORT).show();
return false;
}
});
What am I doing wrong?
You need to set video path url also before setting media controller to videoview like this
Uri uri = Uri.parse(path);
videoView.setVideoURI(uri);
videoView.start();
Example
videoView.setVideoURI(Uri.parse(video_url));
videoView.start();
MediaController mediacontroller = new MediaController(getActivity());
mediacontroller.setAnchorView(videoView);
videoView.setMediaController(mediacontroller);
ProgressBar progressBar =findViewById(R.id.progressbar);
progressBar.setVisibility(View.VISIBLE);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
}
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(getActivity(),"Error",Toast.LENGTH_SHORT).show();
return false;
}
});
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1,
int arg2) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.GONE);
mp.start();
}
});
}
});
Add this progressbar widget in xml like this
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
Update:- Added progress bar while playing video

Playing video in videoview android

I am trying to play a video in a video view. The xml code contains the video view as:
<VideoView
android:id="#+id/vvIntroVideo"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="38" />
and in the Java part of code I have:
public class LoginPage extends Activity {
private VideoView introVideoView;
private static String DEBUG = LoginPage.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
//initializing part
introVideoView = (VideoView) findViewById(R.id.vvIntroVideo);
try {
introPlayer();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v(DEBUG, e.getMessage().toString());
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login_page, menu);
return true;
}
public void introPlayer(){
Uri video = Uri.parse("android:resource://"+getPackageName()+"/"+R.raw.documentariesandyou);
introVideoView.setVideoURI(video);
introVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.setLooping(true);
}
});
introVideoView.start();
}
}
The application shows the error that the video cannot be played. The video is in MP4 so there might not be problem in that. Can anyone help me in this case.
First define ProgressDialog mProgressDialog;
Then try this code,it worked for me:
video = (VideoView) findViewById(R.id.VideoView1);
final MediaController mc = new MediaController(this);
mc.setAnchorView(video);
mc.setMediaPlayer(video);
Uri uri = Uri.parse(path1);
video.setMediaController(mc);
System.out.println("!-- uri: "+uri.toString());
video.setVideoURI(uri);
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mProgressDialog.dismiss();
}
});
video.start();
video.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
}
});
}
public void showLoaderProgress(){
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading Video\nPlease wait...");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
where path1 is your video path.
As you are a beginer i am giving you code to play video in videoview,this will play both videos from raw folder as well from server url with live streaming. to play local just load URI to vieoview rest all thing are same.its very easy to understand.i hope it will help you.
globals in activity
private MediaController controller;
ProgressBar progressBar = null;
private String _url;
Oncreate
VideoView video = (VideoView) findViewById(R.id.videoView1);
controller = new MediaController(VideoScreen.this);
video.setMediaController(controller);
video.setVideoPath(_url);
video.requestFocus();
video.start();
progressBar.setVisibility(View.VISIBLE);
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
progressBar.setVisibility(View.GONE);
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1,
int arg2) {
progressBar.setVisibility(View.GONE);
mp.start();
}
});
}
});
I found the solution. Actually the problem was in encoding of video. I was using gingerbread emulator to test application and the video was in mp4 h.264. But it seems gingerbread is capable in playing mp4 or 3gpp h.263 only in default.

How Can we Set Video Length for Some time Span like 60 seconds if its length is more than 5 minute

**In Android Application Please Suggest how can we setDuration of video... My video length is currently more than 2 minutes.
*1.videoview doesnot giving setDuration ...
2. how to setDuration in videoview***
public class MainActivity extends Activity {
VideoView video;
MediaPlayer mp;
Button play;
int playtime=10000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video=(VideoView)findViewById(R.id.videoView1);
video.setLongClickable(true);
play=(Button)findViewById(R.id.btn1);
Uri uri=Uri.parse("android.resource://" + getPackageName() +"/"+R.drawable.b);
video.setVideoURI(uri);
video.setMediaController(new MediaController(this));
//video.setDuration(playtime);
MediaController mp=new MediaController(this);
play.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v)
{
if(video.getDuration()<=playtime)
{
video.setVisibility(v.VISIBLE);
video.requestFocus();
video.start();
play.setEnabled(false);
}
else
{
video.stopPlayback();
}
return true;
}
});
video.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
play.setEnabled(true);
video.setVisibility(View.GONE);
play.setFocusableInTouchMode(true);
play.requestFocus();
}
});
}
2. Xml and here is the XML Snippet which is ok..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<VideoView
android:id="#+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:visibility="gone"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="play"/>
</RelativeLayout>
You need to implement MediaPlayerControl to MediaController Like this.
mp.setMediaPlayer(new MediaPlayerControl() {
#Override
public void start() {
}
#Override
public void seekTo(int pos) {
}
#Override
public void pause() {
}
#Override
public boolean isPlaying() {
return false;
}
#Override
public int getDuration() {
return 0; // Pass your duration here
}
#Override
public int getCurrentPosition() {
return 0;
}
#Override
public int getBufferPercentage() {
return 0;
}
#Override
public boolean canSeekForward() {
return false;
}
#Override
public boolean canSeekBackward() {
return false;
}
#Override
public boolean canPause() {
return false;
}
});
Replace this lines
video.setMediaController(new MediaController(this));
//video.setDuration(playtime);
MediaController mp=new MediaController(this);
With
MediaController mp=new MediaController(this);
video.setMediaController(mp);

VideoView onPrepared is not called if the VideoView is invisible

I am hiding the VideoView initially and when the video is loaded I am showing the VideoView. But onPrepared is never called if the VideoView is invisible initially. However onPrepared is called properly if VideoView is visible. Is there any way to hide the videoView until video is loaded. Any help would be appreciated. Thanks!
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<VideoView
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:visibility="invisible" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView) findViewById(R.id.video);
Uri videoUri = Uri.parse(url);
videoView.setVideoURI(videoUri);
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Toast.makeText(mActivity, "on prepared", Toast.LENGTH_SHORT).show();
videoView.setVisibility(View.VISIBLE);
}
});
}
You could try setting alpha channel of video view to 0 or close to 0.
Solved it making its layout invisible instead of videoView itself.
Thanks to #J.Kowalski .
Layout:
<FrameLayout
android:id="#+id/layout_video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<VideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:scrollbars="none"/>
</FrameLayout>
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
/>
Set OnPreparedListener:
videoView.setOnPreparedListener(this);
Show progress bar before ready:
#Override
public void showProgress() {
progress.setVisibility(View.VISIBLE);
layoutVideoView.setVisibility(View.INVISIBLE);
}
Load video URI:
#Override
public void loadVideo(Uri uri) {
videoView.setVideoURI(uri);
}
When its ready, onPrepared is called:
#Override
public void onPrepared(MediaPlayer mp) {
Log.d("debug", "onPrepared");
iStepPreviewPresenter.onVideoViewReady();
}
Finally show layout and start video:
#Override
public void hideProgress() {
progress.setVisibility(View.INVISIBLE);
layoutVideoView.setVisibility(View.VISIBLE);
}
#Override
public void startVideo() {
videoView.start();
}
Nailed it!
It can be achieved by one of these tricks
You can set the height and width of the VideoView to 1px until the video is prepared and then switch to full screen by changing the properties to match parent
As #FDIM said in his answer, we can set the alpha value of the view to 0 until video is prepared. But it works only if we use TextureView to load the video. It doesn't work with normal VideoView. Here is a custom VideoView extending TextureView which mimics the default VideoVideo implementation. This class can be directly used in xml file and can set the alpha to 0 to hide the video.
public class TextureVideoView extends TextureView implements SurfaceTextureListener {
private MediaPlayer mMediaPlayer;
public TextureVideoView(Context context) {
super(context);
init();
}
public TextureVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mMediaPlayer = new MediaPlayer();
this.setSurfaceTextureListener(this);
}
public void seekTo(int msec) {
mMediaPlayer.seekTo(msec);
}
public void setVideoPath(final String path) throws IOException {
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
}
public void setVideoPath(FileDescriptor fd, long offset, long length) throws IOException {
mMediaPlayer.reset();
mMediaPlayer.setDataSource(fd, offset, length);
mMediaPlayer.prepare();
}
public void setVideoURI(Context context, Uri uri) throws IOException {
mMediaPlayer.reset();
mMediaPlayer.setDataSource(context, uri);
}
public void setOnPreparedListener(OnPreparedListener onPreparedListener) {
mMediaPlayer.setOnPreparedListener(onPreparedListener);
}
public void setOnCompletionListener(OnCompletionListener onCompletionListener) {
mMediaPlayer.setOnCompletionListener(onCompletionListener);
}
public void setOnErrorListener(OnErrorListener onErrorListener) {
mMediaPlayer.setOnErrorListener(onErrorListener);
}
public void start() {
mMediaPlayer.start();
}
public void pause() {
mMediaPlayer.pause();
}
public void setVolume(float leftVolume, float rightVolume ) {
mMediaPlayer.setVolume(leftVolume, rightVolume);
}
public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}
public void stopPlayback() {
mMediaPlayer.stop();
}
public void reset() {
mMediaPlayer.reset();
}
public void release() {
mMediaPlayer.release();
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
int height) {
mMediaPlayer.setSurface(new Surface(surface));
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
int height) {
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
An easier workaround:
Just add a dummy parentview for the videoview and set the visiblity for that parentview instead of changing the visibility of the videoview itself.

Android loading animation before VideoView start

I don't find any solution to show a loading animation (or just an imageView) before a VideoView start to play : during the video is buffering.
Does anyone have a clue ?
Thanks.
Or you can just use Progress Dialog;
public class VideoEkrani extends Activity {
VideoView ekran;
String kaynakYolu = "http://commonsware.com/misc/test2.3gp";
ProgressDialog progDailog;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sayfa_video_goruntule);
ekran = (VideoView) findViewById(R.id.videoViewESPU);
ekran.setMediaController(new MediaController(this));
ekran.setVideoURI(Uri.parse(kaynakYolu));
ekran.requestFocus();
ekran.start();
progDailog = ProgressDialog.show(this, "Please wait ...", "Retrieving data ...", true);
ekran.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
progDailog.dismiss();
}
});
}
}
Finally I found the method setOnPreparedListener() on VideoView.
I added a ProgressBar inside my layout, and hide it with the OnPreparedListener
You cant detect the video buffer using OnBufferingUpdateListener on MediaPlayer. If you use VideView set that listener inside OnPreparedListener.
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
if(percent == 100){
//video have completed buffering
}
}
});
}
});

Categories

Resources