VideoView throws IllegalStateException - android

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) {
}

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

Device not display MediaController

I am using the below code to test on emulator and device, the result came out was, the emulator can show the MediaController just below the VideoView(default is at the bottom of the screen). However, when I tested in device, it does not show me the MediaController.
I would like to know why.
final VideoView videoView = (VideoView)findViewById(R.id.videoGuide1);
videoView.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp){
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener(){
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height){
MediaController mediaController = new MediaController(WatchVideo.this);;
videoView.setMediaController(mediaController);
mediaController.setAnchorView(videoView);
}
});
}
});
Try this It worked for me....
VideoView vv;
vv = (VideoView) findViewById(R.id.videoView);
vv.setVideoPath(filename);
vv.setMediaController(new MediaController(this));
vv.requestFocus();
vv.start();
}
}
Try to this way
MediaController controller;
Videoview videoview;
videoview=(VideoView)findViewById(R.id.videoview);
controller = new MediaController(this);
videoview.setVideoURI(Uri.parse(path));
videoview.setMediaController(controller);
controller.setMediaPlayer(videoview);
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
int duration = videoview.getDuration();
videoview.requestFocus();
videoview.start();
controller.show();
Log.d("Video view is prepared","------>");
}
});

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.

VideoView - MediaController not showing up

I can't get it working... Whenever I touch my video, nothing is happing... Although, I think, the MediaController should be visible...
I currently use following function to play my video:
private void playCachedVideo(final VideoView videoView, final Context c, File f)
{
//videoView.setZOrderOnTop(true);
videoView.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener()
{
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height)
{
MediaController mediaController = new MediaController(c);
mediaController.setMediaPlayer(videoView);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
}
});
}
});
videoView.setVideoURI(Uri.fromFile(f));
videoView.start();
}
The video plays fine, but I can't get any MediaController to show up...
PS: I'm showing the player in a DialogFragment
Try this:
mMediaController = new MediaController(getActivity());
replace c by getActivity().
You need to attach the controller to the VideoView and then call mediaController.show(timeInMillisec) in onPrepared() callback.
This show the controller on touch of videoview and will hide the controller after specified timeInMillisec.
public class VideoViewActivity extends Activity implements MediaPlayer.OnPreparedListener{
private VideoView mVideoView = null;
MediaController mediaController = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview_layout);
mVideoView = (VideoView) findViewById(R.id.VideoView);
mVideoView.setOnPreparedListener(this);
mediaController = new MediaController(mVideoView.getContext());
mediaController.setMediaPlayer(mVideoView);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoPath("URL");
}
#Override
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
mediaController.show(2000);
}
}

How can I check if a streaming rtsp on android ends?

I have an application that show a streaming rtsp. How can I check if it ends?
Buffer empty? Anything else?
Socket is closed
Anything on control channel from media server?
private VideoView vidView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vidView = (VideoView) findViewById(R.id.myVideo);
uri = Uri.parse(RTSP_URL);
vidView.setMediaController(new MediaController(this));
vidView.setVideoURI(uri);
vidView.requestFocus();
vidView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Runnable runnable = new Runnable() {
#Override
public void run() {
vidView.start();
}
};
runOnUiThread(runnable);
}
});
vidView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d("Error ", what + " " + extra);
return false;
}
});
vidView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.d(getClass().getName(), " callback COMPLETE HERE ");
}
});
}

Categories

Resources