在全屏播放HTML5视频时旋转屏幕 [英] Rotate screen when HTML5 video playing in fullscreen
问题描述
首先,我想为我在英语中的不良水平道歉,以及我在这篇文章中可能犯过的所有错误。
我也是Android devloppement中的初级(我在空闲时间做这个),我不够好,经验不足^^很抱歉,如果我可能有一些困难要理解答案你可能会给我。
First of all, i would like to apologize for my bad level in english, and for all the mistakes i might have done in this post. I'm also a "junior" in Android devloppement (i'm doing this on my free time), i'm not good and experienced enough yet ^^ so sorry if i might have some difficulties to understands the answer you might give me.
那我在做什么?
我正在创建一个简单的Android应用程序。加载我的网站只是一个Webview。
我的网站只是一个随机的Youtube视频播放器。
So what i'm doing ? I'm creating a simple Android App. It's just a Webview to load my website. My website is just a random Youtube video player.
首先我遇到了在全屏播放HTML5视频的问题。我成功完成了这个项目 VideoEnabledWebView
First i had the problem to play HTML5 Video in Fullscreen. I succeed to done that with this project VideoEnabledWebView
所以我的应用程序与VideoEnabledWebView完全相同。
So my app is exactly the same as VideoEnabledWebView.
我的问题是什么?
我在我的应用程序中屏蔽了屏幕旋转。只有肖像模式,我成功完成了。
但是当我以全屏模式播放视频时,我无法进入横向模式。我理解为什么,但我找不到解决方案,黑客能够做到这一点。
所以我想以横向模式旋转屏幕,只有当我在全屏播放视频时。
What is my problem ? I block the screen rotation in my app. Only "Portrait" mode and i succeed to done that. But when i play a video in fullscreen mode, i can't be in landscape mode. I understand why, but i can't find a solution, a hack to able to do that. So i would like to rotate the screen in landscape mode only if i'm playing the video in fullscreen.
首先,这是我的MainActivity.java:
First, Here is my MainActivity.java :
public class MainActivity extends ActionBarActivity
{
private VideoEnabledWebView webView;
private VideoEnabledWebChromeClient webChromeClient;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
webView = (VideoEnabledWebView)findViewById(R.id.webView);
View nonVideoLayout = findViewById(R.id.nonVideoLayout); // Your own view, read class comments
ViewGroup videoLayout = (ViewGroup)findViewById(R.id.videoLayout);
View loadingView = getLayoutInflater().inflate(R.layout.view_loading_video, null); // Your own view, read class comments
webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) // See all available constructors...
{
@Override
public void onProgressChanged(WebView view, int progress)
{
}
};
webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
{
@Override
public void toggledFullscreen(boolean fullscreen)
{
if (fullscreen)
{
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(attrs);
if (android.os.Build.VERSION.SDK_INT >= 14)
{
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
}
else
{
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(attrs);
if (android.os.Build.VERSION.SDK_INT >= 14)
{
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
}
});
webView.setWebChromeClient(webChromeClient);
webView.setWebViewClient(new myWebViewClient());
webView.loadUrl("https://www.awesomekraken.com/fr");
}
public class myWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
@Override
public void onBackPressed()
{
if (!webChromeClient.onBackPressed())
{
if (webView.canGoBack())
{
webView.goBack();
}
else
{
super.onBackPressed();
}
}
}
}
As你可以在onCreate方法中看到,我把这行:
As you can see in the onCreate method, i put the line :
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
我认为在这部分我可以解决我的问题:
I think it's in this part i can resolve my problem :
webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
{
@Override
public void toggledFullscreen(boolean fullscreen)
{
if (fullscreen)
{
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(attrs);
if (android.os.Build.VERSION.SDK_INT >= 14)
{
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
}
else
{
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(attrs);
if (android.os.Build.VERSION.SDK_INT >= 14)
{
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
}
});
我试图把 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
在if(全屏)下,但不起作用。应用程序在Landscape中切换半秒钟,webview重新加载所有页面。
I tried to put setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
under the if(fullscreen) but that doesn't work. The app switch in Landscape for a half second and the webview reload the all page.
我在Google& StackOverflow但从现在开始我没有发现任何非常有用的信息。
I searched a lot on Google & StackOverflow but from now i found nothing very helpfull.
只有这个帖子与我的相同,但他没有得到任何答案。
Only this post is the same as mine, but he got no any answer.
如果有人有解决方案,提示或建议。 ..
If anyone have a solution, tip or advice ...
当然,如果我获得更多信息或解决我的问题,我会更新这篇文章。
Of course, i will update this post if i get more information, or solve my problem.
提前感谢您的帮助!
推荐答案
我最终在旋转自定义视图容器:
I ended up in rotating the custom view container:
public class RotatedVerticalFrameLayout extends FrameLayout{
public RotatedVerticalFrameLayout(Context context, AttributeSet attrs){
super(context, attrs);
}
@SuppressWarnings("SuspiciousNameCombination")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
int w = getWidth();
int h = getHeight();
setRotation(90.0f);
setTranslationX((h - w) / 2);
setTranslationY((w - h) / 2);
ViewGroup.LayoutParams lp = getLayoutParams();
lp.height = w;
lp.width = h;
requestLayout();
}
}
如果只显示自定义,这看起来像横向模式查看而不是动作栏和其他内容。
This looks like landscape mode if you only show the custom view instead of actionbar and other contents.
这篇关于在全屏播放HTML5视频时旋转屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!