在单个视频播放器中查看YouTube视频和普通视频的任何软件包或方式 [英] Any package or way to view both Youtube videos and Normal Videos in a Single Video Player

查看:104
本文介绍了在单个视频播放器中查看YouTube视频和普通视频的任何软件包或方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,我在其中播放来自两个链接的视频,这些页面是:

  • Youtube网址
  • 普通视频网址,例如Vimeo等

我使用了这个著名的flutter软件包,即 video_player ,并且还使用了有用的软件包,其中将 video_player 与所有内置控件包装在一起,即 youtube_player_flutter 解决方案

video_player chewie 只能联网(带有流URL),资产或文件视频.因此,首先我们需要一种方法来识别视频URL 是否来自youtube.然后,我们可以使用 youtube_explode_dart 获取youtube视频流的网址,以便播放器可以复制.

以下是我的一个项目的代码段:

  String convertUrlToId(String url,{bool trimWhitespaces = true}){assert(url?.isNotEmpty?false,'Url不能为空');字符串_url;如果(!url.contains('http')&&(url.length == 11))返回url;如果(trimWhitespaces){_url = url.trim();} 别的 {_url = url;}为([RegExp(r'^ https:\/\/(?: www \.| m \.)?youtube \ .com \/watch \?v =([__- a-zA-Z0-9] {11}).* $'),RegExp(r'^ https:\/\/(?: www \.| m \.)?youtube(?:-nocookie)?\.com \/embed \/([__- a-zA-Z0-9]{11}).* $'),RegExp(r'^ https:\/\/youtu \ .be \/([__- a-zA-Z0-9] {11}).* $')]){最终比赛比赛= exp.firstMatch(_url);if(match!= null& match.groupCount> = 1)返回match.group(1);}返回null;} 

  Future< String>_extractVideoUrl()异步{如果(%% IS_VIDEO_FROM_YOUTUBE %%){最终提取器= YoutubeExplode();最终的videoId = convertUrlToId(widget.videoUrl);final streamManifest =等待extractor.videos.streamsClient.getManifest(videoId);final streamInfo = streamManifest.muxed.withHighestBitrate();extractor.close();返回streamInfo.url.toString();}返回null;} 

然后,最后.

  _videoPlayerController = VideoPlayerController.network(%% URL_EXTRACTED %% .. initialize(); 

I have a page on which I am playing videos from both the links, these are:

  • Youtube URLs
  • Normal Video URLs like Vimeo etc

I have used this famous flutter package that is, video_player, and also used a very useful package which wraps the video_player with all the inbuilt controls, which is, chewie 0.9.10

Now the above one is capable of running the videos which are normal, and not from Youtube. For Youtube videos, there are packages available, these are: youtube_player_flutter and youtube_player.

But the Challenge here is, to find a workaround which plays both of the videos that is from NORMAL VIDEO URL and YOUTUBE URL without having any problem. I am searching for a way out, and found nothing till then. I hope I find some useful answers here, so that I can make use of it in my flutter application. Thanks

解决方案

video_player or chewie can only network(with streaming url), assets or file videos. So first we need a way to indentify if the video URL is from youtube. Then we can use the youtube_explode_dart to obtain the youtube video stream url, so the player can reproduce.

Below is a code snippet from one of my projects:

String convertUrlToId(String url, {bool trimWhitespaces = true}) {
  assert(url?.isNotEmpty ?? false, 'Url cannot be empty');
  String _url;
  if (!url.contains('http') && (url.length == 11)) return url;
  if (trimWhitespaces) {
    _url = url.trim();
  } else {
    _url = url;
  }

  for (final exp in [
    RegExp(r'^https:\/\/(?:www\.|m\.)?youtube\.com\/watch\?v=([_\-a-zA-Z0-9]{11}).*$'),
    RegExp(
        r'^https:\/\/(?:www\.|m\.)?youtube(?:-nocookie)?\.com\/embed\/([_\-a-zA-Z0-9]{11}).*$'),
    RegExp(r'^https:\/\/youtu\.be\/([_\-a-zA-Z0-9]{11}).*$')
  ]) {
    final Match match = exp.firstMatch(_url);
    if (match != null && match.groupCount >= 1) return match.group(1);
  }

  return null;
}

Future<String> _extractVideoUrl() async {
  if (%%IS_VIDEO_FROM_YOUTUBE%%) {
    final extractor = YoutubeExplode();
    final videoId = convertUrlToId(widget.videoUrl);
    final streamManifest = await extractor.videos.streamsClient.getManifest(videoId);
    final streamInfo = streamManifest.muxed.withHighestBitrate();
    extractor.close();
    return streamInfo.url.toString();
  }
  return null;
}

And then, finally.

_videoPlayerController = VideoPlayerController.network(%%URL_EXTRACTED%%)..initialize();

这篇关于在单个视频播放器中查看YouTube视频和普通视频的任何软件包或方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆