Android WebViewを使用してYoutube再生時にフルスクリーン再生をしたい
AndroidStudioにて開発を進めている開発初心者なのですが、
WebViewを使用してYoutube再生時にフルスクリーン再生をしたいのですがうまくいきません。
どなたかご教授頂けないでしょうか?
発生している問題・エラーメッセージ
"Failed to execute 'requestFullscreen' on 'Element': API can only be initiated by a user gesture.
該当のソースコード
public class BrowsingActivity extends AppCompatActivity { WebView myWebView; String Homeurl; MenuItem battery; MenuItem battery_text; MenuItem wifi; private View containerView; private FrameLayout customViewContainer; private View videoCustomView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Window window = getWindow(); // メニューバーを非表示にする window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); // タイトルバーを非表示にする window.requestFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_browsing); //アクションバーのタイトル setTitle("Internet Browser"); myWebView = (WebView)findViewById(R.id.webView1); //標準ブラウザをキャンセル myWebView.setWebViewClient(new WebViewClient()); //キャッシュをクリア myWebView.clearCache(true); //フォームデータを削除 myWebView.clearFormData(); WebViewDatabase.getInstance(this).clearUsernamePassword(); WebViewDatabase.getInstance(this).clearHttpAuthUsernamePassword(); myWebView.getSettings().setSaveFormData(false); //履歴をクリア myWebView.clearHistory(); //キャッシュを残さない myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false); myWebView.setWebChromeClient(new CustomWebChromeClient()); customViewContainer = (FrameLayout) findViewById(R.id.customView_frame); //COOKIE削除 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { CookieManager.getInstance().removeAllCookies(null); CookieManager.getInstance().flush(); } else { CookieSyncManager cookieSyncMngr= CookieSyncManager.createInstance(getApplicationContext()); cookieSyncMngr.startSync(); CookieManager cookieManager= CookieManager.getInstance(); cookieManager.removeAllCookie(); cookieManager.removeSessionCookie(); cookieSyncMngr.stopSync(); cookieSyncMngr.sync(); } //インテントでURLデータ受け取り Intent intent = getIntent(); Homeurl = intent.getStringExtra("URL"); //アプリ起動時に読み込むURL myWebView.loadUrl(Homeurl); // JavaScript 有効 myWebView.getSettings().setJavaScriptEnabled(true); // リンクタップ時に標準ブラウザを使わない myWebView.setWebViewClient( new WebViewClient() ); // 読み込み時にページ横幅を画面幅に無理やり合わせる myWebView.getSettings().setLoadWithOverviewMode(true); // ワイドビューポートへの対応 myWebView.getSettings().setUseWideViewPort(true); // ズーム機能 myWebView.getSettings().setBuiltInZoomControls(true); // リンクタップ時 myWebView.setWebViewClient( new WebViewClient(){ public boolean shouldOverrideUrlLoading( WebView view, String url ){ myWebView.loadUrl(url); HideBar(); //ナビゲーションバー非表示 // trueを返却 return( true ); } }); // アクションバーセット //setupActionBar(); } public void HideBar(){ //ナビゲーションバー非表示 View decor = this.getWindow().getDecorView(); decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); } class CustomWebChromeClient extends WebChromeClient { @Override public void onShowCustomView(View view, CustomViewCallback callback) { if (videoCustomView != null) { callback.onCustomViewHidden(); return; } final FrameLayout frame = ((FrameLayout) view); final View v1 = frame.getChildAt(0); view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER)); v1.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { onHideCustomView(); return true; } return false; } }); videoCustomView = view; customViewContainer.setVisibility(View.VISIBLE); customViewContainer.setBackgroundColor(Color.BLACK); customViewContainer.bringToFront(); myWebView.setVisibility(View.GONE); customViewContainer.addView(videoCustomView); } @Override public void onHideCustomView() { super.onHideCustomView(); customViewContainer.removeView(videoCustomView); videoCustomView = null; customViewContainer.setVisibility(View.INVISIBLE); myWebView.setVisibility(View.VISIBLE); } } @Override protected void onResume() { super.onResume(); HideBar(); //ナビゲーションバー非表示 //COOKIE削除 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { CookieManager.getInstance().removeAllCookies(null); CookieManager.getInstance().flush(); } else { CookieSyncManager cookieSyncMngr= CookieSyncManager.createInstance(getApplicationContext()); cookieSyncMngr.startSync(); CookieManager cookieManager= CookieManager.getInstance(); cookieManager.removeAllCookie(); cookieManager.removeSessionCookie(); cookieSyncMngr.stopSync(); cookieSyncMngr.sync(); } myWebView.onResume(); } //youtube時音声が残る対策 @Override protected void onPause(){ super.onPause(); myWebView.onPause(); } //youtube時音声が残る対策 @Override protected void onDestroy() { super.onDestroy(); myWebView.onPause(); } private void setupActionBar() { ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { // Show the Up button in the action bar. actionBar.setDisplayHomeAsUpEnabled(true); } } @Override public boolean dispatchTouchEvent(MotionEvent event) { HideBar(); //ナビゲーションバー非表示 return super.dispatchTouchEvent(event); } } xml側 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.example.webview.app.BrowsingActivity"> //WebView部分の記述 <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_above="@+id/container" android:scrollbars="none" /> <FrameLayout android:id="@+id/customView_frame" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" /> </RelativeLayout>
試したこと
エラーの内容から直接画面に触れて触らないとダメだよという事かと思うのですが、
当然youtubeのフルスクリーンボタン?みたいな物に指で直接触れています。
補足情報(FW/ツールのバージョンなど)
AndroidStudio3.3、TargetSDK28で作成しております。
デバッグ用のAndroid端末はAndroid7.1.1になります。
ソースコードは一部抜粋になりますが、CustomWebChromeClientというのを使用しています。
開発初心者なのですが、なぜフルスクリーン再生ができないのかどなたかご教授頂けませんでしょうか。
宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。