首页 > 解决方案 > Flutter Dart:如何从视频 URL 中提取 youtube 视频 ID?

问题描述

如何使用 dart 正则表达式从视频 URL 中提取 YouTube 视频 ID?

示例网址

https://www.youtube.com/watch?v=SEkUienM2oY&t=1265s

或者

https://m.youtube.com/watch?v=SEkUienM2oY&t=1265s

返回

SEkUienM2oY&t=1265s

它为我工作

  String getVideoID(String url) {
    url = url.replaceAll("https://www.youtube.com/watch?v=", "");
    url = url.replaceAll("https://m.youtube.com/watch?v=", "");
    return url;
  }

但是如何用正则表达式做到这一点?

标签: regexflutterdart

解决方案


没有正则表达式,我希望这能完美工作 将此依赖项添加到您的 pubspec.yaml 文件中

youtube_player_flutter: ^6.1.0+4

try {

    videoIdd = YoutubePlayer.convertUrlToId("your url here");
    print('this is '+videoId);

  } on Exception catch (exception) {

    // only executed if error is of type Exception
    print('exception');

  } catch (error) {

    // executed for errors of all types other than Exception
     print('catch error');
   //  videoIdd="error";

}

推荐阅读