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);
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'm using VideoView to loop a small video, all works fine on the emulators, but when I deploy that to TV, after the first loop video turns black, but sound keeps going. This is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
VideoView videoview = (VideoView) findViewById(R.id.videoview1);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video);
videoview.setVideoURI(uri);
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
videoview.start();
}
});
}
The Emulator is using Android 6.0, the TV is Sony Bravia with Android 6.0.1.
I tested using SurfaceView, instead of VideoView - the same thing happens.
Any idea how to get rid of that black screen?
PS: There is a workaround that works - make OnCompletionListener and do videoview.start() there - this way it loops, but there's an ugly gap between the loops.
Just use this mVideoView.setZOrderOnTop(true); It will not show the black screen as the view appears.
Try to set your VideoView using handler like this.
videoview.setBackgroundColor(Color.WHITE); //color what you want as background
videoview.postDelayed(new Runnable() {
#Override
public void run() {
videoview.setVideoURI(videoUri);
}
}, 100);
videoview.postDelayed(new Runnable() {
#Override
public void run() {
vv.setBackgroundColor(Color.TRANSPARENT);
}
}, 300);
videoview.requestFocus();
videoview.start();
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { #Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
I have looked into the source code of VideoView and the first thing that I would like to point out is that inside the setVideoURI() method the MediaPlayer object will be created, and the OnPreparedListener will be set.
Your mistake is that you set the OnPreparedListener after the MediaPlayer may already have prepared the video, thus never call the onPrepared in the listener you have set after that, which means the setLooping(true) was maybe never set on the MediaPlayer.
TL;DR:
Put videoview.setVideoURI(uri); after videoview.setOnPreparedListener().
videoview.start() should be after setVideoURI(). On finish it should start again without additional input because this time mp.setLooping(true); was correctly set and will be executed. No videoview.start() is necessary after the initial one.
I actually ended up using ExoPlayer instead of the default one.
It's a bit harder to set up, but this problem didn't show up there.
m_MediaPlayer = MediaPlayer.create(context, soundFromResource);
m_MediaPlayer.setVolume(0.99f, 0.99f);
m_MediaPlayer.setLooping(true);
m_MediaPlayer.setOnCompletionListener(USoundPlayback.this);
m_MediaPlayer.start();
I've tested it like above, I've also tested it by calling setLooping(true) after start() but with no luck.
I have two Nexus 5 phones, both with Android 5 on them. On one, the looping works, on the other the sound stops after one go, it won't loop.
Any ideas ?!
Apparently there's an issue with Android 5 devices which use the NuPlayer instead of the AwesomePlayer.
You can check it by going in the Developer Options, under the Media section there should be Use NuPlayer (experimental). I've unchecked that and it appears it's alright now.
I haven't been able to figure out how to fix this issue, so I've hacked it a bit. I've set some flags in the code and when it enters onCompletion, if the user hasn't specifically stopped the sound, i restart it there. If there's anyone with a better fix, let me know and i'll update this answer.
Here's the issue: https://code.google.com/p/android-developer-preview/issues/detail?id=1695
After a lot of experimentation with media player, I have found solution to this :
call
m_MediaPlayer.setLoopoing(true)
after
m_MediaPlayer.start()
It is an issue in media player.
I used the next code to fix that:
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (looping) {
mMediaPlayer.seekTo(0);
}
}
});
I solve this using this workaround:
public static void setMpLooping(final MediaPlayer mp, final boolean isLooping) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP
|| Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) {
if (isLooping) {
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.start();
}
});
} else {
mp.setOnCompletionListener(null);
}
} else {
mp.setLooping(isLooping);
}
}
But remember, sometimes there is a delay during the transition from end to start of the track on Lollipop
I have used MediaController in my activity its working fine but when I play video for first time then there should b pause button visible but instead there is play and when I press that button then the video is paused correctly and state remains the same and after that its working properly. And same thing happens when Video Completed.
Is this a bug or I am doing any thing wrong?
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaController = new MediaController(VideoPlayerActivity.this){
public void hide(){
}
public void show(){
if(isPlayingAd){
super.hide();
}else{
super.show();
}
}
};
videoView.setMediaController(mediaController);
mediaController.setMediaPlayer(videoView);
mediaController.show();
}
});
I've been having the same issue. I was not calling MediaController.setVideoView as you were, as I thought VideoView.setMediaController was sufficient for wiring things up. I tried adding that, then moving the call to show within onPrepared, and now it is working.
I wish I had a better understanding; my best guess is that perhaps everything needs to be wired up properly before the media is prepared, and before calling show. In any case, here is what I have:
mMediaController = new MediaController(VideoPlayerActivity.this, false);
mVideoView.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer pMp) {
mMediaController.show();
}
});
mVideoView.setMediaController(mMediaController);
mMediaController.setMediaPlayer(mVideoView);
mVideoView.setVideoPath(uri); // may not be applicable in your case
mVideoView.requestFocus();
mVideoView.start();
As Oneworld mentioned on the other answer, I had same issue with old Samsung devices. Even though MediaController wired with VideoView properly, play button loses its sync until pause and play again with MediaController.
This thing seems only happens on old Samsung devices(KitKat and below i guess).
The only solution I found was play video programmically by videoview.start() before showing controller by mc.show().
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.