android 如何在Webview中单击YouTube搜索页面链接获得视频ID

sbtkgmzw  于 2024-01-04  发布在  Android
关注(0)|答案(2)|浏览(156)

我有一个Webview,其中我提出YouTube搜索页面.(https://www.youtube.com/results?search_query=)我想接收用户按下的剪辑的视频ID.到目前为止,我已经尝试了以下没有成功:
1.使用shouldOverrideUrlLoading -在搜索中按下视频剪辑时未调用

  1. public WebViewClient getWebViewClient()
  2. {
  3. return new WebViewClient() {
  4. @Override
  5. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  6. Log.d("TAG","called overide url");
  7. if (url.startsWith(YOUTUBE_VIDEO_URL)) {
  8. setVideoIdFromLink(url);
  9. return true;
  10. }
  11. else
  12. {
  13. Log.d("GABI","url:"+url);
  14. }
  15. return false;
  16. }
  17. /*
  18. public void onLoadResource (WebView view, String url){
  19. if(url.startsWith(YOUTUBE_VIDEO_URL)){
  20. setVideoIdFromLink(url);
  21. }
  22. }*/
  23. };
  24. }

字符串
1.设置onTouchPad-方法总是返回类型0 extras null

  1. private OnTouchListener linkClickTouch()
  2. {
  3. return new OnTouchListener() {
  4. @Override
  5. public boolean onTouch(View v, MotionEvent event) {
  6. switch (event.getAction()) {
  7. case MotionEvent.ACTION_DOWN: {
  8. startClickTime = Calendar.getInstance().getTimeInMillis();
  9. break;
  10. }
  11. case MotionEvent.ACTION_UP: {
  12. long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
  13. if(clickDuration < MAX_CLICK_DURATION) {
  14. v.performClick();
  15. }
  16. }
  17. }
  18. return true;
  19. }
  20. };
  21. }


点击动作:

  1. private OnClickListener linkClick()
  2. {
  3. return new OnClickListener()
  4. {
  5. public void onClick(View v) {
  6. final WebView webview = (WebView) v;
  7. final WebView.HitTestResult result = webview.getHitTestResult();
  8. Log.d("TAG", "result:"+result.getType()+"\nextras:"+
  9. result.getExtra());
  10. if (result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
  11. }
  12. if (result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
  13. }
  14. if (result.getType() == WebView.HitTestResult.IMAGE_TYPE) {
  15. }
  16. }
  17. };
  18. }


我知道我可能可以使用YouTube API,但我想在不构建搜索活动的情况下实现这一点,当一个像样的搜索页面已经存在时,搜索活动只会显示找到ID的结果。

lfapxunr

lfapxunr1#

经过多次尝试和错误:shouldOverrideUrlLoading或onPageStarted永远不会被调用。
你应该使用-

  1. public void onLoadResource (WebView view, String url)
  2. {
  3. if(url.startsWith("https://m.youtube.com/watch?"))
  4. {
  5. setVideoIdFromLink(url);
  6. view.stopLoading();
  7. }
  8. }
  9. public String setVideoIdFromLink(String url)
  10. {
  11. //get the video id from the link
  12. String[] args=url.split("v=");
  13. String videoId=args[args.length-1];
  14. return videoId;
  15. }

字符串

展开查看全部
mpgws1up

mpgws1up2#

尝试此操作以提取Youtude ID

  1. String pattern = "(?<=watch\\?v=|/videos/|embed\\/|youtu.be\\/|\\/v\\/|\\/e\\/|watch\\?v%3D|watch\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\u200C\u200B2F|youtu.be%2F|%2Fv%2F)[^#\\&\\?\\n]*";
  2. Pattern compiledPattern = Pattern.compile(pattern);
  3. Matcher matcher = compiledPattern.matcher(url); //url is youtube url for which you want to extract the id.
  4. if (matcher.find()) {
  5. return matcher.group();
  6. }

字符串
在webview中

  1. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  2. Log.d("TAG","called overide url");
  3. if (url.startsWith(YOUTUBE_VIDEO_URL)) {
  4. String pattern = "(?<=watch\\v=|/videos/|embed\\/|youtu.be\\/|\\/v\\/|\\/e\\/|watch\\?v%3D|watch\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\u200C\u200B2F|youtu.be%2F|%2Fv%2F)[^#\\&\\?\\n]*";
  5. Pattern compiledPattern = Pattern.compile(pattern);
  6. Matcher matcher = compiledPattern.matcher(url);
  7. if (matcher.find()) {
  8. String id = matcher.group();
  9. }
  10. return true;
  11. }
  12. else
  13. {
  14. Log.d("GABI","url:"+url);
  15. }
  16. return false;
  17. }

展开查看全部

相关问题