2013-01-01 2 views
-1

, 나는 URL이 비디오 링크 (유튜브) 또는 동영상을 웹 페이지의 일부로서 포함되어있는 경우 비디오를 재생합니다. 나는 webview에 직접 URL을 추가하려했지만 YouTube 링크도 재생할 수 없다. webview에서 이미 플러그인과 자바 스크립트를 사용할 수있다. ..... 누구든지이 문제에 관해 나를 도와 줄 수있다.안드로이드 웹보기 비디오 재생

+1

다음 링크가 당신에게 도움이 될 수 있습니다 http://stackoverflow.com/questions/6466536/how-to-play-video-in-webview-by-html- 5 http://stackoverflow.com/questions/3815090/web 보기 및 HTML5 비디오 –

답변

1

시험해보기 방법 :

public class FlashContentPlayer extends Activity { 
private WebView m_wvMain; 
private Button m_btnPlayVideo; 
private ProgressDialog m_progressLoading; 
private String m_videoUrl; 
private Context m_context; 
@Override 
protected void onCreate(Bundle p_savedInstanceState) { 
    super.onCreate(p_savedInstanceState); 
    setContentView(R.layout.flashcontentplayer); 
    m_context = FlashContentPlayer.this; 
    m_wvMain = (WebView) findViewById(R.id.fcpwvMain); 
    m_btnPlayVideo = (Button) findViewById(R.id.fcpbtnPlayVideo); 
    m_wvMain.getSettings().setJavaScriptEnabled(true); 
    m_wvMain.getSettings().setAllowFileAccess(true); 
    m_wvMain.getSettings().setPluginsEnabled(true); 
    m_wvMain.getSettings().setPluginState(WebSettings.PluginState.ON); 
    if (getIntent().hasExtra("video_url")) { 
     m_videoUrl = getIntent().getExtras().getString(
       AppConstants.VIDEO_URL); 
    } 
    m_wvMain.setWebViewClient(new VideoWebViewClient()); 
    m_progressLoading = new ProgressDialog(m_context); 
    m_progressLoading.setTitle(getString(R.string.app_name)); 
    m_progressLoading.setMessage("Loading ..."); 
    m_btnPlayVideo.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View p_v) { 
      if (m_videoUrl == null) { 
       m_wvMain.loadUrl("http://www.hackerdude.com/channels-test/20051210-w50s.flv"); 
      } else { 
       if (m_videoUrl.contains("www.youtube.com/") 
         || m_videoUrl.contains("youtu.be/")) { 
        System.out.println("video id==>" 
          + getYoutubeVideoId(m_videoUrl)); 
        if (isYouTubeInstalled(FlashContentPlayer.this)) { 
         Intent m_intent = new Intent(Intent.ACTION_VIEW, 
           Uri.parse("vnd.youtube:" 
             + getYoutubeVideoId(m_videoUrl))); 
         startActivity(m_intent); 
        } else { 
         Intent m_yVideoIntent = new Intent(null, Uri 
           .parse("ytv://" 
             + getYoutubeVideoId(m_videoUrl)), 
           m_context, OpenYouTubePlayerActivity.class); 
         startActivity(m_yVideoIntent); 
        } 
       } else { 
        m_wvMain.loadUrl(m_videoUrl); 
       } 
      } 
     } 
    }); 
} 
/** 
* Method to get the Video id from the Youtube Url 
* 
* @param p_url 
*   The url of the YouTube video 
* @return The id of the YouTube video 
*/ 
public String getYoutubeVideoId(String p_url) { 
    String m_videoID = null; 
    try { 
     Pattern m_compiledPattern = Pattern 
       .compile(AppConstants.YOUTUBE_URL_PATTERN); 
     Matcher m_matcher = m_compiledPattern.matcher(p_url); 
     if (m_matcher.find()) { 
      int m_start = m_matcher.end(); 
      m_videoID = p_url.substring(m_start, m_start + 11); 
     } 
    } catch (Throwable p_e) { 
     CustomLogHandler.printErrorlog(p_e); 
    } 
    return m_videoID; 
} 
public class VideoWebViewClient extends WebViewClient { 
    @Override 
    public void onPageStarted(WebView p_view, String p_url, Bitmap p_favicon) { 
     m_progressLoading.show(); 
     super.onPageStarted(p_view, p_url, p_favicon); 
    } 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView p_view, String p_url) { 
     return super.shouldOverrideUrlLoading(p_view, p_url); 
    } 
    @Override 
    public void onPageFinished(WebView p_view, String p_url) { 
     if (m_progressLoading.isShowing()) { 
      m_progressLoading.cancel(); 
     } 
     super.onPageFinished(p_view, p_url); 
    } 
} 
/** 
* Method to check if YouTube application is installed or not in the device 
* 
* @param p_ctx 
*   The context of the activity 
* @return true if YouTube application is installed on the device false 
*   otherwise 
*/ 
public static boolean isYouTubeInstalled(Context p_ctx) { 
    PackageManager m_packageManager = p_ctx.getPackageManager(); 
    boolean m_isInstalled = false; 
    List m_appList; 
    try { 
     // act_list = 
     // pm.getInstalledPackages(PackageManager.GET_ACTIVITIES); 
     m_appList = m_packageManager 
       .getInstalledApplications(PackageManager.GET_ACTIVITIES); 
     for (int m_i = 0; m_i < m_appList.size(); m_i++) { 
      if (m_appList.get(m_i).toString() 
        .contains("com.google.android.youtube")) { 
       // System.out.println("In if of com.google.android.youtube"); 
       m_isInstalled = true; 
       break; 
      } 
     } 
     m_packageManager.getPackageInfo("com.google.android.youtube", 
       PackageManager.GET_ACTIVITIES); 
    } catch (Exception p_e) { 
     m_isInstalled = false; 
    } 
    return m_isInstalled; 
}} 
0
if (webView == null) { 
      webView = (WebView) findViewById(R.id.web_view); 
      webView.getSettings().setJavaScriptEnabled(true); 
      webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false); 
      webView.getSettings().setPluginsEnabled(true); 
      webView.getSettings().setSupportMultipleWindows(false); 
      webView.getSettings().setSupportZoom(false); 
      webView.setVerticalScrollBarEnabled(false); 
      webView.setHorizontalScrollBarEnabled(false); 
      webView.setWebChromeClient(new WebChromeClient()); 

     } 

webView.setWebViewClient(new WebViewClient() { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       if (url.startsWith("vnd.youtube")) { 

        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 

        return true; 
       } else { 
        return false; 
       } 
      } 

      public void onPageFinished(WebView view, String url) { 
       // hide progress indicator 
       hideProgressDialog(); 
      } 
     });