我有一个Youtube Webview并且运行良好。 现在我想添加一个选项来全屏播放视频,就像在Youtube官方应用中一样。
如何添加全屏播放视频的选项?
public class MainActivity extends AppCompatActivity {
WebView wb;
TextView tv;
private boolean exit = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activty_main);
wb = (WebView) findViewById(R.id.web);
tv = (TextView) findViewById(R.id.tv);
checkConnection();
}
protected boolean isonline() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
return true;
} else {
return false;
}
}
当我在手机上启动应用程序时,这是我的Webview设置,它不会显示全屏播放选项。
private void checkConnection() {
if (isonline()) {
Toast.makeText(MainActivity.this, "You are connented to Internet",
Toast.LENGTH_SHORT).show();
wb.loadUrl("https://www.youtube.com/?gl=IN");
WebSettings webSettings = wb.getSettings();
webSettings.setJavaScriptEnabled(true);
wb.setWebViewClient(new WebViewClient());
} else {
Toast.makeText(this, "You r not connected to Internet",
Toast.LENGTH_SHORT).show();
tv.setText("Can't Connected to the Server. \n Please Check your
Internet Connection");
}
}
@Override
public void onBackPressed() {
if (wb.canGoBack()) {
wb.goBack();
} else {
if (exit) {
finish();
} else {
Toast.makeText(this, "Press again to exit",
Toast.LENGTH_SHORT).show();
}
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
exit = false;
}
}, 2000);
exit = true;
}
}
这是我的布局文件
<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">
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:id="@+id/web"
tools:ignore="RtlCompat">
</WebView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:layout_marginTop="200dp"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="18sp" />
</RelativeLayout>
答案 0 :(得分:0)
你的代码有效吗? 它只播放音频 没有视频显示