How to play video from URL in videoview in Android? - 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

Related

How to raise a toast when a seekbar is used in a videoview?

I am using video view to play files I need to raise a toast when the seekbar is clicked or used to seek video showing the current video duration. Any help?
MediaController mediaController = new MediaController(this.context);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
try {
videoView.setVideoURI(Uri.parse(url));
}
catch (Exception e){
Log.i("Error","In making connection");
}
videoView.requestFocus();
try{
videoView.setOnPreparedListener(new android.media.MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(android.media.MediaPlayer mediaPlayer) {
textViewPlayback.setVisibility(View.INVISIBLE);
Log.i("Inside","On prepare");
progressBar.setVisibility(View.INVISIBLE);
videoView.start();
videoView.seekTo(getSeekTime()*1000);
textViewPlayback.postDelayed(onEverySecond,10);
}
});
}
catch (Exception e){
Toast.makeText(this.context,"can not play this file",Toast.LENGTH_SHORT).show();
}
videoView.setOnErrorListener(new android.media.MediaPlayer.OnErrorListener() {
#Override
public boolean onError(android.media.MediaPlayer mediaPlayer, int i, int i1) {
Log.i("Error in playing","error");
return false;
}
});
videoView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Toast.makeText(getContext(),textViewPlayback.getText(),Toast.LENGTH_SHORT).show();
return false;
}
});
}
I am using video-view player to play and control media. I need to know is there any way that I can find out when the seekbar is touched in the video view controls.
You can use like this -
Add seekbar below your video view.
seekBar = (SeekBar) findViewById(R.id.seek_bar);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
seekBar.setProgress(0);
seekBar.setMax(recVideo.getDuration());
}
});
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Show toast here
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

E/MediaPlayer: Error (-38,0)

String fileName = "android.resource://" + getPackageName() + "/raw/oryx1001";
MediaController videoMediaController = new MediaController(this);
//mVideoView.setVideoPath( Uri.parse());
mVideoView.setVideoURI(Uri.parse(fileName));
mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return true;
}
});
videoMediaController.setVisibility(View.GONE);
videoMediaController.setMediaPlayer(mVideoView);
mVideoView.setMediaController(videoMediaController);
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
//jump();
//System.out.println("dksadalkdakldsadlkadklsad");
mp.reset();
String fileName = "android.resource://" + getPackageName() + "/raw/oryx1001";
MediaController videoMediaController = new MediaController(MainActivity.this);
mVideoView.setVideoURI(Uri.parse(fileName));
mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return true;
}
});
videoMediaController.setVisibility(View.GONE);
videoMediaController.setMediaPlayer(mVideoView);
mVideoView.setMediaController(videoMediaController);
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
}
});
}
});
simply my code 3gp file not working. it works on android oreo but not on lolipop device or samsung s6. is it api thing, or device thing for codecs supported? i tried everything mp4, 3gp nothin working.
E/MediaPlayer: Error (-38,0)
D/VideoView: Error: -38,0
/MediaPlayer: Error (1,-38)
You need to call mediaPlayer.start() in the onPrepared method by using a listener. You are getting this error because you are calling mediaPlayer.start() before it has reached the prepared state.
You have set setOnPreparedListener
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
}
});
but you start wrong object mVideoView.start(); you should call
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mp.start();//Change is hare ..
}
});

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

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

VideoView throws IllegalStateException

I have a videoview in my app which plays videos from my expansion file. Here is the code:
private VideoView _vv;
private static final Uri CONTENT_URI = Uri.parse("content://--PACKAGENAMEHIDDENFORSECURITY--.EHZipUriProvider");
private static final String VIDEO_NAME = "b1.mp4";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
_vv = (VideoView) findViewById(R.id.vv);
startVideoPlayback();
}
private void startVideoPlayback()
{
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(_vv);
_vv.setMediaController(mediaController);
_vv.setVideoURI(Uri.parse(CONTENT_URI + "/" + VIDEO_NAME));
_vv.start();
}
And the stacktrace:
java.lang.IllegalStateException
at android.media.MediaPlayer.prepareAsync(Native Method)
at android.widget.VideoView.openVideo(VideoView.java:335)
at android.widget.VideoView.access$2100(VideoView.java:71)
at android.widget.VideoView$7.surfaceCreated(VideoView.java:607)
at android.view.SurfaceView.updateWindow(SurfaceView.java:572)
at android.view.SurfaceView.access$000(SurfaceView.java:86)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:175)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1897)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
...
I have no idea why it does this. Can anyone help?
My layout is just a videoview.
Try this
try {
mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(url);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
});
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
videoView.start();
}
});
videoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
}
});
} catch (Exception e) {
}

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.

Categories

Resources