I'm working on a app which uses a webview to display webpages, i want to play the videos if the url is a video link(youtube) or a video contains as a part of the webpage. I've tried to add the url directly to the webview but it doesnot play even youtube links, i've already enabled plugins and javascript in webview..... can anyone help me on this issue,
Asked
Active
Viewed 4,232 times
-1
-
1Following links can help you http://stackoverflow.com/questions/6466536/how-to-play-video-in-webview-by-html-5 http://stackoverflow.com/questions/3815090/webview-and-html5-video – Dixit Patel Jan 01 '13 at 08:55
2 Answers
1
Try out this way:
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();
}
});

TharakaNirmana
- 10,237
- 8
- 50
- 69