VideoView not looping video only on Galaxy s4 - android

In my application I use a VideoView to stream video files,and I make the VideoView loop videos using the MediaPlayer.setLooping(true).
Here is some of the code -
videoView.setVideoPath(url);
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (shouldLoopVideo) {
mp.setLooping(true);
}
videoView.start();
}
});
This code is working perfectly for all devices but the Galaxy s4 android 4.2.2 I tested on many devices, and many android versions, but the only ones that fail are the galaxy s4.
The device will play the video once, and stop (After calling the onCompletion listener).

It is a bug of Android 4.4.2.
My workaround:
private String mVideoPath;
private VideoView mVideoView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mVideoPath = "android.resource://" + getActivity().getPackageName() + "/" + R.raw.backvideo;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mVideoView = (VideoView) view.findViewById(R.id.videoBackground);
mVideoView.setVideoURI(Uri.parse(mVideoPath));
mVideoView.setKeepScreenOn(true);
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mp) {
mVideoView.setVideoURI(Uri.parse(mVideoPath));
mVideoView.start();
}
});
mVideoView.start();
}
#Override
public void onPause() {
super.onPause();
mVideoView.pause();
}
#Override
public void onResume() {
super.onResume();
mVideoView.setVideoURI(Uri.parse(mVideoPath));
mVideoView.start();
}

I am having the same problem. Works on other devices, but not on Galaxy S4.
Is there some known solution for this problem.
My current workaround is to call setVideoPath() in onCompletion() which doesn't seams to be right for me.

here is my code which work from android J to android O device
VideoView vv = (VideoView)findViewById(R.id.vv);
vv.setZOrderOnTop(true);
try {
MediaController mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(vv);
Uri uri = Uri.parse(_video_link);
vv.setMediaController(mediacontroller);
vv.setVideoURI(uri);
vv.seekTo(1);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
vv.requestFocus();
vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
vv.start();
}
});
vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp)
{
vv.resume();
}
});

Related

android media player on prepared listener on a null object reference

i am making a phone dialer app that play the sound when you press on button the app is working great but after a while i get a random crash because on prepared listener i do not know what is happening and this is the error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setOnPreparedListener(android.media.MediaPlayer$OnPreparedListener)' on a null object reference
at com.alper.pola.andoid.phonedailer.MainActivity$1.onTouch(MainActivity.java:258)
and this is how i cast the media player
if (mLastButton == one) {
MediaPlayer mediaPlayer ;
mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.one);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp1.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp1.release();
}
});
}
if (mLastButton == two) {
MediaPlayer mediaPlayer ;
mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.two);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp2.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp2.release();
}
});
}
if (mLastButton == three) {
MediaPlayer mediaPlayer ;
mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.three);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp3.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp3.release();
}
});
}
if (mLastButton == four) {
MediaPlayer mediaPlayer ;
mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.four);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp4.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp4.release();
}
});
}
the app is working fine and the same button when i click on it is working but after while the button stop working and i get this crash with every button it start with working fine but after i type a few numbers the app crash
You are using a very inefficient way while dealing with MediaPlayer. Still I'll help you a workaround with this code. Use mediaPlayer.prepare() to initiate the call. Checkout this code:
if (mLastButton == one) {
mp1 = MediaPlayer.create(MainActivity.this,R.raw.one);
mp1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp1.start();
});
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp1.release();
}
});
mp1.prepare();
}
Do similarly for others. There are much better way to do this but as with your code, this is shortest.

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.

Android: VideoView can't play video

im trying to get a basic video player working for an android application im working on but whenever i try to play the video i get an error saying "cant play video". Im certain the video is in supported formats and ive tried converting it between several of the supported formats on top of that so i assume its an issue with my code:
public class VideoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_layout);
VideoView videoView = (VideoView)findViewById(R.id.VideoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoPath("TravelAppTopic4/res/raw/canadavid.mp4");
videoView.start();
}
thanks in advance for any help
edit:im testing on both a virtual device and an S3 to the same result.
public class VideoScreen extends Activity{
private MediaController controller;
Context _ctx = VideoScreen.this;
private Thread thread;
ProgressBar progressBar = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.phone_video_screen);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
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();
}
});
}
});
}
}
try
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw. canadavid));

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

Video doesn't loop Android VideoView

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

Categories

Resources