Introduced
Android in order to achieve the purpose of saving power, the screen will be automatically displayed when there is no operation, such as when we play the video, although there is no action on the screen, but we do not want it at this time the screen, if the video is paused, this is the hope that it automatically screen.
Here's how to control the screen as you play the video chromium full screen.
Realize
Because full-screen playback is related to Contentvideoview.java, it is implemented in this class.
First declare a powersaveblocker switch in content_switches.h:
externconstchar kEnableContentVideoViewPowerSaveBlocker[];
Initialize in content_switches.cc:
// Enable the PowerSaveBlocker in ContentVideoView. Android only.constchar"enable-content-video-view-power-save-blocker";
Then turn the switch on in the aw_main_delegate.cc:
Add in the Awmaindelegate::basicstartupcomplete
cl->AppendSwitch(switches::kEnableContentVideoViewPowerSaveBlocker)
The next step is to implement it in Contentvideoview.java:
First Contentvideoview to implement the Viewandroiddelegate interface:
Three ways to implement it:
@Override PublicViewAcquireanchorview() {View Anchorview =NewView (GetContext ()); AddView (Anchorview);returnAnchorview; }@Override Public void setanchorviewposition(View view,floatXfloatYfloatWidthfloatHeight) {log.e (TAG,"setanchorviewposition isn ' t implemented"); }@Override Public void Releaseanchorview(View Anchorview) {Removeview (Anchorview); }
Add a member Variable:
// The ViewAndroid is used to keep screen on during video playback.private ViewAndroid mViewAndroid;
Implemented in the constructor:
new ViewAndroid(newthis);
Implement a function for the JNI layer invocation:
@CalledByNative privatelonggetNativeViewAndroid() { return mViewAndroid.getNativePointer(); }
The Contentvideoview class corresponds to the JNI layer class implemented in content_video_view.cc, then power management is also mainly in this class:
First look at Content_video_view.h, to declare the Powersaveblocker class and add member variables and methods in the content namespace:
#include"Base/timer/timer.h"Class Powersaveblocker; Returns The associated Nativeview Gfx::nativeview Getnativeview (); void Createpowersaveblocker (); Powersaveblock toKeep screen on forFullscreen video. There isalready blocker whenInline video started, andIt requires the//Contentview' sContainer displayed totake effect; ButinchWebView, apps//could UseAnother container toHold Contentvideoview, andThe blockerinchContentview' sContainer can notKeep screen on; So we need another blocker//here, it isNo harm, just an additonal blocker. Scoped_ptr<powersaveblocker> Power_save_blocker_;
Implemented in content_video_view.cc:
Gfx::nativeview Contentvideoview::getnativeview () {jnienv* env = Attachcurrentthread (); scopedjavalocalref<jobject> Content_video_view = Getjavaobject (env);if(Content_video_view.is_null ())returnNULL;return reinterpret_cast<gfx::NativeView> (Java_contentvideoview_getnativeviewandroid (env), Content_video_view.obj ()));}voidContentvideoview::createpowersaveblocker () {if(Base::commandline::forcurrentprocess ()->hasswitch (Switches::kenablecontentvideoviewpowersaveblocker)) {//In fullscreen Clank reuses the Power Save blocker attached to the //Container view that is created for embedded video. The WebView cannot //Reuse so we create a new blocker instead. if(Power_save_blocker_)return; Power_save_blocker_ = Powersaveblocker::create (Powersaveblocker::kpowersaveblockpreventdisplaysleep,"Playing Video"). Pass ();static_cast<PowerSaveBlockerImpl*> (Power_save_blocker_.get ()), Initdisplaysleepblocker (Getnativeview ()); }}
Call Createpowersaveblocker in the constructor
ContentVideoView::ContentVideoView( BrowserMediaPlayerManager* manager) : manager_(manager), weak_factory_(this) { …… CreatePowerSaveBlocker();}
ContentVideoView::OpenVideo()
call in and ContentVideoView::Play
time CreatePowerSaveBlocker()
to keep the screen:
In,,, ContentVideoView::OnMediaPlayerError
ContentVideoView::OnPlaybackComplete()
ContentVideoView::Pause
ContentVideoView::ExitFullscreen
when called power_save_blocker_.reset()
to let the system regain control of the screen.
This achieves the purpose of our screen control for playing video.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Chromium full-screen playback video power management