I would like to trigger an event when my VideoView is done playing? How woudl I do this? can you show me an example or direct my to some resources?
Im thinking something like this.
if(!videoView.isplaying()){
//trigger event
}
or
public void onStop(){
//trigger event
}
If you want to know when play has completed, you would use VideoView.setOnCompletionListener().
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mp){
// do on completion stuff.
}
});
If you want to detect error cases, you would use VideoView.setOnErrorListener() instead.
Related
I'm trying to start video from specific position but VideoView has strange behave. When I run code like that
videoView.seekTo(2000)
int current = videoView.getCurrentPosition()
Log.e("Current Time", String.valueOf(current))
Log gives me value 0. It should give 2000 because this is current position. Even If I implement onPreparedListener it doesn't worked (but in different way). It display proper value (2000) but video still is not seek.
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp)
{
videoView.getDuration();
videoView.seekTo(2000);
Log.e("Current: ", String.valueOf(videoView.getCurrentPosition()));
}
});
How to fight this ? If I will build MediaPlayer + SurfaceView it will helps or will behave same as VideoView ?
Actually, the thing is,VideoView.seekTo() is a wrapper around MediaPlayer.seekTo(). This function returns almost immediately even though the actual seeking is still being performed. Therefore you want to wait for seeking to complete via MediaPlayer.OnSeekCompleteListener.
However, the standard VideoView does not support OnSeekCompleteListener.
But you can copy and locally customize the VideoView class to add this support yourself.
Or, You could try this
mVideoView.setOnPreparedListener(new MediaPlayer .OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
Log.e("Current: ", String.valueOf(videoView.getCurrentPosition()));
}
});
}
});
videoView.seekTo(2000); //or wherever the call is to be made
Hope this should help.
I have a VideoView in my activity, which I use only as a background, with a video on looping mode.
The screen does not go into sleep mode automatically. I tried manually counting the seconds since the last user interactivity, but I was still not able to force the screen to turn off.
I have tried PowerManager, acquiring locks and goToSleep() function, but I still haven't managed to turn my screen off.
I know this is an ancient question, but I thought I'd write an answer anyway since this page shows up in Google searches.
I solved the same problem using this code:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.setScreenOnWhilePlaying(false);
}
});
Try this:
DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mDPM.lockNow();
That will lock your phone (same as pressing the power/lock button) and thus turn of the screen.
I solved the same problem using this code:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.setScreenOnWhilePlaying(true);
}
});
I met the same problem. The following code has solved my problem. Hope it can help you.
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.setScreenOnWhilePlaying(false);
}
});
Solved Add one line of code after setContentView() in onCreate()
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flag);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
In Fragment
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Lets say I have several players I've created.
And Lets say that for each one of them, I want a different set on actions to be activated.
How can I locate which player has been finished (the implementation itself of the case sentence) in order to act differently for each player ?
For example: in order to handle a single player, all we have to do is to implement the onCompletion() method like this:
public void onCompletion(MediaPlayer mp){
//DO SOMETHING HERE THAT FITS FOR ALL KINDS OF PLAYER'S TYPE OBJECTS
}
How do I add a case sentence to expand it to handle several different player objects ?
Thanks !
The MediaPlayer attribute passed to this method is the same MediaPlayer that has just completed, so as long as you are keeping a pointer to each media player (such as through a global variable), then all you need to do is check which media player you have received:
public class MyClass implements OnClompleteListener
{
MediaPlayer player1, player2, player3;
//initialize them
player1.setOnCompleteListener(this);
player2.setOnCompleteListener(this);
player3.setOnCompleteListener(this);
#Override
public void onCompletion(MediaPlayer mp)
{
if (mp == player1)
{
//TODO handle player 1 completion
}
else if (mp == player2)
{
//TODO handle player 2 completion
}
else if (mp == player3)
{
//TODO handle player 3 completion
}
}
}
You can also handle this in-line, without implementing the OnCompleteListener:
player1.setOnCompleteListener(new OnCompleteListener() {
#Override
public void onCompletion(MediaPlayer mp)
{
//mp IS ALWAYS EQUAL TO player1!
}
});
You can either create a class that extends MediaPlayer and give it an attribute like a number to keep track of which player is which. Then you would check the player's number in the onCompletion() method. Otherwise, you could create different onCompletion listeners and set them to the different players.
I have an application with a VideoView, in order to make the video play on a loop I use an onCompletionListner to call setVideoPath() again, like this:
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
mVideoView.start();
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
//I have a log statment here, so I can see that it is making it this far.
mp.reset(); // <--- I added this recently to try to fix the problem
mVideoView.setVideoPath(file.getAbsolutePath());
}
});
This setup works well on all devices I've come across so far, I never had any trouble with it not repeating.
However the Motorola Xoom that I am testing on was recently upgraded to ICS. Now that it is on ICS this will work for a while and loop the video. But eventually (I've added a counter and some Logs, there does not appear to be any pattern to how many times it successfully loops before stopping) it will quit looping and just sit on a freeze frame of the first frame in the movie.
Does anyone know what could cause this not to loop properly any more? OR does anyone know of another way to get a VideoView to loop properly that does work under ICS still?
If you have only one video to play you can setLooping(true) in your on prepared listener.
myVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.setLooping(true);
}
});
and you're done.
So far this:
mp.reset();
inside the onComplete callback seems to fix it. Would be very interested if anyone can explain what is going on with it.
i am using VideoView and seek bar but when i seekTo(..) on desired position through seekBar it play video from starting.
i trying this code:
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
mVideoView.seekTo(progress);
}
As Reno mentioned, you have to wait for the seeking to complete.
VideoView does not have a OnSeekCompleteListener() but you can access the MediaPlayer from the onPrepared method of the VideoView and then set the OnSeekCompleteListener, like this :
mVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
//TODO: Your code here
}
});
}
});
The call to VideoView.seekTo() is a wrapper around MediaPlayer.seekTo(). This function returns almost immediately even though the actual seeking is still being performed. Therefore you want to wait for seeking to complete via MediaPlayer.OnSeekCompleteListener.
However, as Reno mentioned, the standard VideoView does not support OnSeekCompleteListener.
But you can copy and locally customize the VideoView class to add this support yourself.
First, start with a copy of VideoView.java. Or you can clone the entire frameworks/base repo but warning it is over 1 gig of data to download.
Copy VideoView.java into your Eclipse Android project and it will start building but fail. Here's what I did to get it to compile:
Update the package statement to match your project.
Comment out the references to MetaData. The fix for this is on my todo list. These need to be replaced with calls to MediaMetadataRetriever.
Replace mContext with calls to getBaseContext()
Now you are ready to add the code for OnSeekCompleteListener. The implementation is similar to the other listeners, i.e OnCompletionListener.
public class VideoView extends SurfaceView
implements MediaPlayerControl {
// The client's listener which is the notification callback.
private OnSeekCompleteListener mOnSeekCompleteListener;
// Set up MediaPlayer to forward notifications to client.
private MediaPlayer.OnSeekCompleteListener mSeekCompleteListener =
new MediaPlayer.OnSeekCompleteListener() {
public void onSeekComplete(MediaPlayer mp) {
if (mOnCompletionListener != null) {
mOnCompletionListener.onCompletion(mMediaPlayer);
}
}
};
// API for client to set their listener.
public void setOnSeekCompleteListener(OnSeekCompleteListener l)
{
mOnSeekCompleteListener = l;
}
}
Finally, update your own code:
Update references to android.widget.VideoView to use your customized VideoView.
Implement a listener and set it via by calling setOnSeekCompleteListener().
Your code now receives notifications when the seek has really completed and it can then perform subsequent seeks.
You have to wait for the seeking to complete, unfortunately VideoView does not have a OnSeekCompleteListener() (why Google? -_-)
use my method works like charm
Take mediaPlayer
MediaPlayer mediaPlayer;
init mediaPlayer
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer = mp;
}
});
use seekTo method like this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mediaPlayer.seekTo((int) mStartPosition, MediaPlayer.SEEK_CLOSEST);
} else {
mediaPlayer.seekTo((int) mStartPosition);
}
You should use
mVideoView.seekTo(progress, SEEK_CLOSEST)