我正在构建一个应用,用户可以看到YouTube
视频,并可以看到链接来自服务器的图像。要播放YouTube
视频嵌入密钥的值来自服务器
`< div id =“_ cvp_11204”>< span> < /跨度>< / DIV> < script type =“text / javascript”> (function(){var a; a = new XMLHttpRequest; a.onreadystatechange = function(){rs = a.readyState; if(4 == rs&& 200 == a.status){var c = JSON.parse (a.responseText),b; for(b in c.payload)if(c.payload.hasOwnProperty(b)){var d = c.payload [b]; document.getElementById(“ cvp “+ b).innerHTML = d.view}}}; a.open(”GET“,”http://dummyurl.com/media.ids=?11134)); a.send()})(window);
</script>`
我也可以播放视频,但我在性能方面面临的问题很少。
1)有时声音开始但视频会在一段时间后开始显示。
2)某些时间的控件没有出现。
3)如何直接在全屏模式下播放视频?目前要以全屏模式播放视频,我必须点击最大控制才能播放。
与webview的活动类一样
public class MainActivity extends Activity {
protected FrameLayout webViewPlaceholder;
protected WebView webView;
private FrameLayout mContentView;
private MyWebChromeClient mWebChromeClient = null;
private View mCustomView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("Oncreate is getting called ---------------------------");
// Initialize the UI
if(savedInstanceState == null){
initUI();
}
}
protected void initUI()
{
// Retrieve UI elements
webViewPlaceholder = ((FrameLayout)findViewById(R.id.webViewPlaceholder));
mContentView = webViewPlaceholder;
// Initialize the WebView if necessary
if (webView == null)
{
System.out.println("webView == null -----------------------------------");
// Create the webview
webView = new WebView(this);
webView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAllowContentAccess(true);
webSettings.setDomStorageEnabled(true);
webSettings.setRenderPriority(RenderPriority.HIGH);
webSettings.setUseWideViewPort(false);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
//--------------------------------------------------------
if (Build.VERSION.SDK_INT < 8) {
webSettings.setPluginsEnabled(true);
} else {
webSettings.setPluginState(PluginState.ON);
}
mWebChromeClient = new MyWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);
// Load the URLs inside the WebView, not in the external web browser
//webView.setWebViewClient(new WebViewClient());
// Load a page
loadWebView("asfas");
}
// Attach the WebView to its placeholder
webViewPlaceholder.addView(webView);
}
private void loadWebView(final String url) {
String s = ReadFromfile("link.txt", MainActivity.this);
s = "<html><body>"+s+"</body></html>";
webView.loadDataWithBaseURL("",Html.fromHtml(s).toString(),"text/html", "UTF-8",null);
}
public String ReadFromfile(String fileName, Context context) {
StringBuilder ReturnString = new StringBuilder();
InputStream fIn = null;
InputStreamReader isr = null;
BufferedReader input = null;
try {
fIn = context.getResources().getAssets()
.open(fileName, context.MODE_WORLD_READABLE);
isr = new InputStreamReader(fIn);
input = new BufferedReader(isr);
String line = "";
while ((line = input.readLine()) != null) {
ReturnString.append(line);
}
} catch (Exception e) {
e.getMessage();
} finally {
try {
if (isr != null)
isr.close();
if (fIn != null)
fIn.close();
if (input != null)
input.close();
} catch (Exception e2) {
e2.getMessage();
}
}
return ReturnString.toString();
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
if (webView != null)
{
// Remove the WebView from the old placeholder
System.out.println("web view is removed from the holder -------------------------");
webViewPlaceholder.removeView(webView);
}
super.onConfigurationChanged(newConfig);
// Load the layout resource for the new configuration
setContentView(R.layout.activity_main);
// Reinitialize the UI
initUI();
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
System.out.println("onSaveInstanceState -------------------------");
// Save the state of the WebView
webView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
System.out.println("onRestoreInstanceState -------------------------");
// Restore the state of the WebView
webView.restoreState(savedInstanceState);
}
private class MyWebChromeClient extends WebChromeClient {
FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mContentView.setVisibility(View.GONE);
mCustomViewContainer = new FrameLayout(MainActivity.this);
mCustomViewContainer.setLayoutParams(LayoutParameters);
mCustomViewContainer.setBackgroundResource(android.R.color.black);
view.setLayoutParams(LayoutParameters);
// Sometimes getting remove view first it parent excepetion on moto devices
//webViewPlaceholder.removeView(webView);
mCustomViewContainer.addView(view);
mCustomView = view;
mCustomViewCallback = callback;
mCustomViewContainer.setVisibility(View.VISIBLE);
setContentView(mCustomViewContainer);
}
@Override
public void onHideCustomView() {
if (mCustomView == null) {
return;
} else {
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mCustomView);
mCustomView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
setContentView(mContentView);
}
}
}
}
在Manifest中我也设置了hardwareaccelarated = true
。
这是从控件开始的视频
在这里,当我点击最大化按钮视频获得我想要的全屏而不点击最大按钮,但视频应该直接以全屏模式播放。
主要是,我如何加快和提高WebView
的性能?