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 ..
}
});
Related
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
i am displaying images and video in imageview and videoview but the issue is when video is
playing onpreparedlistener called but when video finish oncompletion listener not called
when videoview complete i increment the i for next video or images
also it gives me error in logcat like this but video is playing
10-29 20:12:47.770: E/MediaPlayer(3975): error (1, -2147483648)
private void nextVideo(String path){
mImageview.setVisibility(View.GONE);
if(mVideoview.getVisibility()==View.GONE){
mVideoview.setVisibility(View.VISIBLE);
}
controller = new MediaController(HomeActivityNewViewPager.this);
mVideoview.setVideoURI(Uri.parse(path));
mVideoview.setMediaController(null);
controller.setMediaPlayer(mVideoview);
mVideoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mVideoview.start();
long duration = mVideoview.getDuration();
second=duration;
//handler.removeCallbacks(runnable);
//handler.postDelayed(runnable,second);
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.v("video view completed","---"+i);
mp.reset();
if(automode){
if(i==myplaylistlocal.size() || i>myplaylistlocal.size())
{
String checkcount=spreferences.getString("roundcount", "");
Log.v("roundcount==Before Integer.parseInt","---->"+roundcount);
if(roundcount>=Integer.parseInt(checkcount))
{
roundcount=0;
Log.v("roundcount==After Integer.parseInt","---->"+roundcount);
updateplaylist();
}
i=0;
indexplus();
imagesautomode();
i++;
}
else if(i==myplaylistlocal.size()-1)
{
imagesautomode();
i++;
}
else{
imagesautomode();
}
}
else{
i++;
images();
}
}
});
mVideoview.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.v("Error in video playing","----->"+i);
return true;
}
});
}
Either way, the error referenced above is MEDIA_ERROR_UNKNOWN. If this video was made for this app, I would make sure that it is properly encoded for Android. Also make sure that is clearly defines its endpoint.
http://developer.android.com/reference/android/media/MediaPlayer.html#MEDIA_ERROR_UNKNOWN
this is a work around but could possbly work in your situation:
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
if(what == MediaPlayer.MEDIA_ERROR_UNKNOWN)
//ERROR UNKNOWN - COULD BE IMPROPERLY FORMATTED VIDEO {
//MOVE ON TO NEXT VIDEO
//DO LOGGING
}
}
I am confused by the MediaController and VideoView APIs in Android.
I am using it to stream videos through HTTP, which is working fine. Only problem is that it stops for buffering from time to time. I now want to get notified whenever the video starts to buffer to display a progress indicator.
I learned that I need to implement public abstract void onBufferingUpdate (MediaPlayer mp, int percent) from android.media.MediaPlayer.OnBufferingUpdateListener, my question is just where do I have to implement it? I followed the advice from another SO question and implemented:
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Log.d(Constants.LOG, "onPrepared called");
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
//do something
Log.d(Constants.LOG, "Buffering update: " + percent + "%");
}
});
}
But it didn't work... Do I have to subclass VideoView and implement it there??
Works:
VideoView myVideoView = (VideoView)this.findViewById(R.id.video_view);
myVideoView.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
myVideoView.start();
if ( dialog != null )
dialog.dismiss();
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener()
{
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
Log.d("URL", "Buffering update: " + percent + "%");
}
});
}
});
Dialog dialog = new ProgressDialog(VideoActivity.this);
dialog.setMessage("Loading ...");
dialog.setCancelable(true);
dialog.setInverseBackgroundForced(false);
myVideoView.setVideoURI( Uri.parse( "http://..." ) );
In my activity I have got a VideoView which should show a MediaController on touch. If the user touches quickly on Controller while VideoView is not following, after that user presses back button, then my app will be stuck.
setContentView(R.layout.activity_play_video_fullscreen);
videoView = (VideoView) findViewById(R.id.video_view);
urlString = getIntent().getStringExtra(EXTRA_URL);
videoView.setVideoURI(Uri.parse(urlString));
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// video ready to play - hide progress bar
ProgressBar pb = (ProgressBar)findViewById(R.id.progress_bar);
pb.setVisibility(ProgressBar.INVISIBLE);
}
});
videoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// video finished - terminate this activity
AxUtils.axLog(AxUtils.eDbgLogError, AxUtils.eDbgLogGroupDialer, String.format("PlayVideoFullsreenActivity.videoView.onCompletion(): Fullscreen video playback completed.\n"));
finish();
}
});
// install our own error handler
videoView.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
AxUtils.axLog(AxUtils.eDbgLogError, AxUtils.eDbgLogGroupDialer, String.format("PlayVideoFullsreenActivity.videoView.onError(): Playback failed. what=%d(%s) extra=%d(%s)\n",
what, what_toString(what), extra, extra_toString(extra)));
String reason;
if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
reason = "Server connection lost.";
}
else {
reason = extra_toString(extra);
}
String message = String.format("Playback Failed. %s", reason);
Toast.makeText(PlayVideoFullscreenActivity.this.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
finish();
return true;
}
});
// add playback controls
mediaController = new MediaController(this);
mediaController.setAnchorView(videoView.getRootView());
videoView.setMediaController(mediaController);
In your preparedListeners, disable and enable your media controllers. It should solve your issue.
Good luck.
I am trying to build an android app that will loop a video!
The problem is that it never loops! It plays the video only once!
During debugging i realized that the "myVideoView.setOnCompletionListener" is being executed but the video doesn't play!
I also try "mp.reset()" inside the CompletionListener.
Maybe i am missing something in a different file, such as the Manifest?
Any thoughts?
Here is my code:
final VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoURI(Uri.parse(SrcPath));
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
myVideoView.requestFocus();
myVideoView.start();
}
});
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
myVideoView.setVideoURI(Uri.parse(SrcPath));
}
});
Try onPreparedListener instead of onCompletionListener:
myVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
Hope this helps.
Try this.
myVideoView = (VideoView) findViewById(R.id.videoView1);
myVideoView.setVideoPath(video_path);
myVideoView.setMediaController(new MediaController(this));
myVideoView.start();
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
myVideoView.start();
}
});
By that after completion of your video start again...
Try this.
videoView.setVideoPath(tempFileMessageUri.toString());
ctlr = new MediaController(context);
ctlr.setMediaPlayer(videoView);
videoView.setMediaController(ctlr);
videoView.requestFocus();
videoView.start();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
videoView.start();
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
videoView.start();
}
});