我正在开发一个使用 webview 显示网页的应用程序,如果 url 是视频链接(youtube)或视频包含作为网页的一部分,我想播放视频。我尝试将 url 直接添加到 webview 但它甚至不播放 youtube 链接,我已经在 webview 中启用了插件和 javascript ..... 谁能帮我解决这个问题,
问问题
4197 次
2 回答
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;
}}
于 2013-01-01T09:11:00.187 回答
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();
}
});
于 2014-07-15T05:13:26.887 回答