我正在Android Studio中开发一个应用程序,该应用程序允许打开Facebook和YouTube帐户,一切正常,但是在尝试播放视频时会出现黑屏,并且只能听到声音。
我该怎么办?我已经尝试了一切,但没有任何效果。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
my_context = container.getContext();
rootView = inflater.inflate(R.layout.fragment_web, container, false);
preferences = PreferenceManager.getDefaultSharedPreferences(my_context);
String type = getArguments().getString("type");
String url = getArguments().getString("url");
webView = (WebView) rootView.findViewById(R.id.webView);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
--------------- SWIPE容器---------------
swipeContainer = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
// Setup refresh listener which triggers new data loading
swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
});
// Configure the refreshing colors
swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
------------------ Webview设置--------------------
WebSettings webSettings = webView.getSettings();
// GET PREFERENCES
if (preferences.getBoolean("pref_webview_cache", true)) {
enableHTML5AppCache();
}
if (preferences.getBoolean("pref_webview_javascript", true)) {
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebAppInterface(my_context), "WebAppInterface"
-------------------- LOADER ------------------------ >
pd = new ProgressDialog(my_context);
pd.setMessage("Please wait Loading...");
loader = preferences.getString("pref_webview_loader_list", "dialog");
if (loader.equals("pull")) {
swipeContainer.setRefreshing(true);
} else if (loader.equals("dialog")) {
pd.show();
} else if (loader.equals("never")) {
Log.d("WebView", "No Loader selected");
}
webView.setWebViewClient(new MyWebViewClient());
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setSupportZoom(true);
// ---------------- LOADING CONTENT -----------------
if (type.equals("file")) {
webView.loadUrl("file:///android_asset/" + url);
} else if (type.equals("url")) {
webView.loadUrl(url);
}
return rootView;
}
public Boolean canGoBack() {
return webView.canGoBack();
}
public void GoBack() {
webView.goBack();
}
private void enableHTML5AppCache() {
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCachePath("/data/data/" + getActivity().getPackageName() + "/cache");
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
if (Build.VERSION.SDK_INT >= 21) {
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
}
if (android.os.Build.VERSION.SDK_INT < 16) {
webView.setBackgroundColor(0x00000000);
} else {
webView.setBackgroundColor(Color.argb(1, 0, 0, 0));
}
webView.setWebViewClient(new WebViewClient() {
/* @Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.loadUrl(request.getUrl().toString());
}
return super.shouldOverrideUrlLoading(view, String.valueOf(request));
} */
@Override
public void onPageStarted(WebView webview, String url, Bitmap favicon) {
super.onPageStarted(webview, url, favicon);
webview.setVisibility(View.INVISIBLE);
}
@Override
public void onPageFinished(WebView webview, String url) {
webview.setVisibility(View.VISIBLE);
super.onPageFinished(webview, url);
}
});
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.3.6; en-us; T-Mobile myTouch Q Build/HuaweiU8730) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
// webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; Android 4.4.2; DASH 5.0+ Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.93 Mobile Safari/537.36");
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if (loader.equals("pull")) {
swipeContainer.setRefreshing(true);
} else if (loader.equals("dialog")) {
if (!pd.isShowing()) {
pd.show();
}
} else if (loader.equals("never")) {
Log.d("WebView", "No Loader selected");
}
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
if (pd.isShowing()) {
pd.dismiss();
}
if (swipeContainer.isRefreshing()) {
swipeContainer.setRefreshing(false);
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/" + getString(R.string.error_page));
}
}
public class WebAppInterface {
Context mContext;
/**
* Instantiate the interface and set the context
*/
WebAppInterface(Context c) {
mContext = c;
}
// -------------------------------- SHOW TOAST ---------------------------------------
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
// -------------------------------- START VIBRATE MP3 ---------------------------------------
@JavascriptInterface
public void vibrate(int milliseconds) {
Vibrator v = (Vibrator) my_context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(milliseconds);
}
// -------------------------------- START PLAY MP3 ---------------------------------------
@JavascriptInterface
public void playSound() {
// mp = MediaPlayer.create(my_context, R.raw.demo);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();
}
// -------------------------------- STOP PLAY MP3 ---------------------------------------
@JavascriptInterface
public void stopSound() {
if (mp.isPlaying()) {
mp.stop();
}
}
// -------------------------------- CREATE NOTIFICATION ---------------------------------------
@JavascriptInterface
public void newNotification(String title, String message) {
mNotificationManager = (NotificationManager) my_context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(my_context, 0, new Intent(my_context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(my_context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentText(message);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(1, mBuilder.build());
}
// -------------------------------- GET DATA ACCOUNT FROM DEVICE ---------------------------------------
@JavascriptInterface
public void snakBar(String message) {
Snackbar.make(rootView, message, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
}