youtube视频无法在我的webview应用程序中播放?

时间:2014-01-01 11:03:40

标签: android android-youtube-api

我在android中创建了一个Web视图应用程序,Web视图已成功加载,但视频无法播放,如何解决此类问题,当我播放视频时,只能听到音频。

public class ZudiozFragment extends Fragment {
WebView webView;
private ProgressBar bar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_zudioz, container, false);
    setHasOptionsMenu(true);        
    webView = (WebView)rootView.findViewById(R.id.webView1);
    bar = (ProgressBar) rootView.findViewById(R.id.progressBar);       
    new ProgressTask().execute();                                                   
    return rootView;
}
private class ProgressTask extends AsyncTask <Void,Void,Void>{
    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        bar.setVisibility(View.VISIBLE);
    }
    @Override
    protected Void doInBackground(Void... arg0) {
    return null;                  
    }
    @SuppressWarnings("deprecation")
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        webView.getSettings().setJavaScriptEnabled(true);                                          
        webView.setWebChromeClient(new WebChromeClient());          
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.clearCache(true);
        webView.clearHistory();
        webView.loadUrl("http://ebltv.com");

        bar.setVisibility(View.GONE);
    }
}
}

2 个答案:

答案 0 :(得分:4)

初始化WebView时,请设置JavaScript,插件WebViewClient和WebChromeClient。

url = new String("http://broken-links.com/tests/video/"); 
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebChromeClient(chromeClient);
mWebView.setWebViewClient(wvClient);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.loadUrl(url);

处理WebChromeClient对象中的onShowCustomView。

@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
    super.onShowCustomView(view, callback);
    if (view instanceof FrameLayout){
        FrameLayout frame = (FrameLayout) view;
        if (frame.getFocusedChild() instanceof VideoView){
            VideoView video = (VideoView) frame.getFocusedChild();
            frame.removeView(video);
            a.setContentView(video);
            video.setOnCompletionListener(this);
            video.setOnErrorListener(this);
            video.start();
        }
    }
}

处理视频的onCompletion和onError事件,以便返回到Web视图。

public void onCompletion(MediaPlayer mp) {
    Log.d(TAG, "Video completo");
    a.setContentView(R.layout.main);
    WebView wb = (WebView) a.findViewById(R.id.webview);
    a.initWebView();
}

我希望它有所帮助。

答案 1 :(得分:0)

试试这个

// fix for loading youtube videos.

webView.setWebChromeClient(new WebChromeClient());
if (Build.VERSION.SDK_INT < 8) {
    webView.getSettings().setPluginsEnabled(true);
} else {
    webView.getSettings().setPluginState(PluginState.ON);
}