2012-10-03 45 views
42

Tôi đang cố gắng phát video YouTube trong WebView, WebView hiển thị giao diện đầu tiên của video bằng nút phát, Nhưng sau khi nhấp vào nút phát bắt đầu thanh tiến trình và sau 2-3 giây dừng thanh tiến trình và màn hình trống với màu đen.YouTube Video không phát trong WebView - Android

Image1: Giao diện đầu tiên của video bằng nút phát

Image2: Sau khi nhấp vào màn hình nút phát sẽ trống.

Xin vui lòng! giúp tôi tại sao video không bắt đầu.

IMAGE: 1 This is first look of web view

IMAGE: 2 enter image description here

Đây là mã nguồn của tôi để chơi YouTubeVideo trong webview .. Xin hãy giúp tôi ...

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    WebView wv = (WebView) findViewById(R.id.webView1); 
    wv.getSettings().setJavaScriptEnabled(true); 
    wv.getSettings().setPluginsEnabled(true); 
    final String mimeType = "text/html"; 
    final String encoding = "UTF-8"; 
    String html = getHTML(); 
    wv.loadDataWithBaseURL("", html, mimeType, encoding, ""); 
} 

public String getHTML() { 
    String html = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 95%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/" 
      + "J2fB5XWj6IE" 
      + "?fs=0\" frameborder=\"0\">\n" 
      + "</iframe>\n"; 
    return html; 
} 
+0

Điều này xảy ra với tất cả các phiên bản của Android cho bạn? Tôi đã nhìn thấy tương tự nhưng chỉ trên 4.1 – Nick

+0

@Nick: Vấn đề này cũng với Android 3.0, 4.0. –

+0

@RanjitChandel tôi có cùng một sự cố ... –

Trả lời

66

Thêm các dòng trước tải nội dung HTML vào WebView của bạn.

wv.setWebChromeClient(new WebChromeClient() { 
}); 

Từ các tài liệu:

Để hỗ trợ inline HTML5 video trong ứng dụng của bạn, bạn cần phải có khả năng tăng tốc phần cứng bật, và thiết lập một WebChromeClient. Để có hỗ trợ toàn màn hình, việc triển khai onShowCustomView (Xem, WebChromeClient.CustomViewCallback) và onHideCustomView() là bắt buộc, getVideoLoadingProgressView() là tùy chọn.

+0

Vui lòng cung cấp nhận xét của bạn tại đây http://stackoverflow.com/questions/18533678/playing-youtube-videos-smoothly-in-web-view – anshul

+1

vẫn còn từ tài liệu, về onShowCustomView(): Phương pháp này không được chấp nhận ở cấp API 18. Phương pháp này hỗ trợ cơ chế plugin lỗi thời và sẽ không được gọi trong tương lai – elgui

+0

Tuyệt vời!cảm ơn bạn – NeoVe

5

Có một số sự cố với video youtbe được truyền trực tuyến trên thiết bị di động. Nếu bạn trực tiếp cố gắng tải URL trong chế độ xem web và chạy nó thì video sẽ không phát. Một cách khó giải quyết vấn đề này là phát trực tuyến video trong chế độ xem video. Tôi đã không cố gắng này nhưng điều này có thể được thực hiện.
Cách khác để phát video youtube, tôi sẽ gọi đây là cách dễ dàng hơn là thay đổi tác nhân người dùng trong cài đặt chế độ xem web từ thiết bị di động sang máy tính để bàn. Tác nhân người dùng cho biết loại thiết bị mà video youtube sẽ chạy và theo đó loại trang web được gửi bởi máy chủ. Bằng cách này, video youtube có thể được phát trực tuyến và phát. Dưới đây là cách bạn có thể thực hiện việc này:

public static final int USER_MOBILE = 0; 
public static final int USER_DESKTOP = 1; 
wv.getSettings().setUserAgent(USER_DESKTOP); // 1 is for the desktop 
+0

Đặt Tác nhân người dùng thành máy tính để bàn yêu cầu tải xuống Plugin Adobe bất kỳ trợ giúp nào sẽ được đánh giá cao. –

+0

WebSettings.setUserAgent (int i) hiện không được chấp nhận và cũng không giải quyết được sự cố của tôi. –

17

Các mã sau đây là cần thiết để hiển thị các máy nghe nhạc video được bắt đầu bởi khung cốt lõi của web. Chìa khóa cho toàn bộ luồng là VideoView được chuyển lại cho WebChromeClient và bạn cần đính kèm chế độ xem đó vào hệ thống phân cấp chế độ xem của bạn.

Tôi đã tập hợp nó thông qua việc xem xét mã nguồn trình duyệt có sẵn như là một phần của AOSP.

Mã này tham chiếu 4 chế độ xem có thể không rõ ràng.Các hệ thống phân cấp view là:

  • FrameLayout mContentView (gốc)
  • WebView mWebView (con của mContentView)
  • FrameLAyout mCustomViewContainer (con của mContentView)
  • View mCustomView (con của mCustomViewContainer)

Quan điểm được định cấu hình như một phần của việc thiết lập hoạt động vùng chứa.

<FrameLayout 
    android:id="@+id/fullscreen_custom_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FF000000"/> 

    <FrameLayout 
     android:id="@+id/main_content" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

     <WebView 
      android:id="@+id/webView" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </FrameLayout> 
</FrameLayout> 

Trong hoạt động của mình onCreate:

mContentView = (FrameLayout) findViewById(R.id.main_content); 
    mWebView = (WebView) findViewById(R.id.webView); 
    mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content); 

Đăng ký một WebChromeClient với mWebView. khách hàng mà nên ghi đè lên 2 sau - 4 phương pháp:

void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) 
{ 
    // if a view already exists then immediately terminate the new one 
    if (mCustomView != null) 
    { 
     callback.onCustomViewHidden(); 
     return; 
    } 

    // Add the custom view to its container. 
    mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER); 
    mCustomView = view; 
    mCustomViewCallback = callback; 

    // hide main browser view 
    mContentView.setVisibility(View.GONE); 

    // Finally show the custom view container. 
    mCustomViewContainer.setVisibility(View.VISIBLE); 
    mCustomViewContainer.bringToFront(); 
} 

void onHideCustomView() 
{ 
    if (mCustomView == null) 
     return; 

    // 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); 
} 

public Bitmap getDefaultVideoPoster() 
{ 
    if (mDefaultVideoPoster == null) 
    { 
     mDefaultVideoPoster = BitmapFactory.decodeResource(getResources(), R.drawable.default_video_poster); 
    } 
    return mDefaultVideoPoster; 
} 

public View getVideoLoadingProgressView() 
{ 
    if (mVideoProgressView == null) 
    { 
     LayoutInflater inflater = LayoutInflater.from(this); 
     mVideoProgressView = inflater.inflate(R.layout.video_loading_progress, null); 
    } 
    return mVideoProgressView; 
} 

Bạn cũng có thể muốn thêm hoạt động bindings vòng đời như:

@Override 
protected void onStop() 
{ 
    super.onStop(); 
    if (mCustomView != null) 
    { 
     if (mCustomViewCallback != null) 
      mCustomViewCallback.onCustomViewHidden(); 
     mCustomView = null; 
    } 
} 

@Override 
public void onBackPressed() 
{ 
    if (mCustomView != null) 
    { 
     onHideCustomView(); 
    } else 
    { 
     finish(); 
    } 
} 

Để hoạt động của bạn để làm cho hide video khi hoạt động là dừng lại hoặc nhấn nút quay lại.

+9

bạn có thể vui lòng cung cấp mã sạch không? Có rất nhiều lỗi? Ví dụ: mCustomView ở đâu? COVER_SCREEN_GRAVITY_CENTER là gì? –

+0

mCustomView được mô tả trong phần nội dung của câu trả lời. COVER_SCREEN_GRAVITY_CENTER là một LayoutParameter với các giới hạn được đặt thành MATCH_PARENT và trọng lực được đặt thành CENTER. Tôi đã nhận ra điều đó là hiển nhiên, xin lỗi. –

0

Thử này một

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(stringUrl))); 
12

Thêm webview.setWebChromeClient(new WebChromeClient()); và cho phép plugin cho video của bạn thêm

if (Build.VERSION.SDK_INT < 8) { 
      webview.getSettings().setPluginsEnabled(true); 
     } else { 
      webview.getSettings().setPluginState(PluginState.ON); 
     } 

Thnaks

+0

Vâng, đây là liên kết còn thiếu của tôi. Nó hiện hiển thị hình thu nhỏ của nút phát trên YouTube trong phần tử video. –

+1

nó không hoạt động cho tôi. Hơn nữa, setPluginState (PluginState.ON); được deprectated. – Cocorico

2

tôi sao chép mã bị tắc nghẽn và nó làm việc cho tôi .... tôi nghĩ rằng u phải cài đặt flash payer .. di u ?? và bạn đã thêm quyền truy cập internet ???

btw mã của tôi là ở đây ...

package com.example.youtube; 



import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Handler; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ProgressBar; 
import android.widget.Toast; 
import android.util.Log; 
import android.view.Menu; 
import android.view.Window; 
import android.view.WindowManager; 
import android.webkit.DownloadListener; 
import android.webkit.WebChromeClient; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebChromeClient; 
import android.webkit.WebViewClient; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.widget.AbsoluteLayout; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.IntentFilter; 
import android.os.AsyncTask; 
import android.util.Log; 
import android.widget.TextView; 


import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
public class MainActivity extends Activity{ 


    @SuppressLint("SetJavaScriptEnabled") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     getWindow().requestFeature(Window.FEATURE_PROGRESS); 




      setContentView(R.layout.activity_main); 


final ProgressBar Pbar; 

Pbar = (ProgressBar) findViewById(R.id.pB4); 
WebView wv = (WebView) findViewById(R.id.webVie1); 
//wv.setWebViewClient(new Callback()); 
WebSettings webSettings = wv.getSettings(); 
webSettings.setBuiltInZoomControls(true); 
webSettings.setJavaScriptEnabled(true); 
//wv.setBackgroundColor(0x919191); 
final String mimeType = "text/html"; 
final String encoding = "UTF-8"; 
String html = getHTML(); 
wv.loadDataWithBaseURL("", html, mimeType, encoding, ""); 




final Activity activity = this; 

wv.setWebChromeClient(new WebChromeClient() { 
     public void onProgressChanged(WebView view, int progress) { 
     // Activities and WebViews measure progress with different scales. 
     // The progress meter will automatically disappear when we reach 100% 

     activity.setProgress(progress * 100); 
     { 
      if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){ 
       Pbar.setVisibility(ProgressBar.VISIBLE); 

      } 
      Pbar.setProgress(progress); 
      if(progress == 100) { 
       Pbar.setVisibility(ProgressBar.GONE); 

      } 
      } 
     } 
    }); 
    wv.setWebViewClient(new WebViewClient() { 
     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
     Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_LONG).show(); 
     } 
    }); 
    wv.setDownloadListener(new DownloadListener() 
    { 

     public void onDownloadStart(String url, String userAgent,String contentDisposition, String mimetype,long contentLength) 
     { 
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 

      startActivity(intent); 
     } 


    }); 







} 

    private String getHTML() { 
     // TODO Auto-generated method stub 
     String html1 = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 95%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/" 
       + "J2fB5XWj6IE" 
       + "?fs=0\" frameborder=\"0\">\n" 
       + "</iframe>\n"; 
     return html1; 
    } 


    /* public void onPause() 
    { 
     super.onPause(); 
     System.exit(0); 
    }*/ 




} 

file layout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/page_buttons" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:orientation="horizontal" > 


</LinearLayout> 

<WebView 
    android:id="@+id/webVie1" 
    android:layout_width="316dp" 
    android:layout_height="392dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentTop="true" 
    android:layout_weight="0.99" /> 

<ProgressBar 
    android:id="@+id/pB4" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true"/> /> 
</LinearLayout> 
Các vấn đề liên quan