Update Section video to support youtube, vimeo and local video (to be tested)
This commit is contained in:
parent
af1eb1ed75
commit
6942518b4b
@ -1,12 +1,22 @@
|
|||||||
import 'dart:convert';
|
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:manager_api_new/api.dart';
|
import 'package:manager_api_new/api.dart';
|
||||||
|
import 'package:tablet_app/Components/video_viewer.dart';
|
||||||
import 'package:tablet_app/constants.dart';
|
import 'package:tablet_app/constants.dart';
|
||||||
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
|
import 'package:webview_flutter/webview_flutter.dart';
|
||||||
import 'package:youtube_player_iframe/youtube_player_iframe.dart' as iframe;
|
import 'package:youtube_player_iframe/youtube_player_iframe.dart' as iframe;
|
||||||
//import 'package:youtube_player_flutter/youtube_player_flutter.dart';
|
|
||||||
|
enum _VideoSourceType { youtube, vimeo, direct }
|
||||||
|
|
||||||
|
_VideoSourceType _detectSourceType(String url) {
|
||||||
|
if (url.contains('youtube.com') || url.contains('youtu.be')) return _VideoSourceType.youtube;
|
||||||
|
if (url.contains('vimeo.com')) return _VideoSourceType.vimeo;
|
||||||
|
return _VideoSourceType.direct;
|
||||||
|
}
|
||||||
|
|
||||||
|
String? _extractVimeoId(String url) {
|
||||||
|
final match = RegExp(r'vimeo\.com/(?:.*?/)?(\d+)').firstMatch(url);
|
||||||
|
return match?.group(1);
|
||||||
|
}
|
||||||
|
|
||||||
class VideoView extends StatefulWidget {
|
class VideoView extends StatefulWidget {
|
||||||
final VideoDTO section;
|
final VideoDTO section;
|
||||||
@ -17,23 +27,23 @@ class VideoView extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _VideoView extends State<VideoView> {
|
class _VideoView extends State<VideoView> {
|
||||||
iframe.YoutubePlayer? _videoViewWeb;
|
iframe.YoutubePlayerController? _youtubeController;
|
||||||
YoutubePlayer? _videoView;
|
WebViewController? _vimeoController;
|
||||||
VideoDTO? videoDTO;
|
_VideoSourceType? _sourceType;
|
||||||
|
|
||||||
|
static const _browserUserAgent =
|
||||||
|
'Mozilla/5.0 (Linux; Android 10; Tablet) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
//print(widget.section!.data);
|
super.initState();
|
||||||
//videoDTO = VideoDTO.fromJson(jsonDecode(widget.section!.data!));
|
final source = widget.section.source_;
|
||||||
//print(videoDTO);
|
if (source == null || source.isEmpty) return;
|
||||||
videoDTO= widget.section;
|
|
||||||
|
|
||||||
String? videoId;
|
_sourceType = _detectSourceType(source);
|
||||||
if (videoDTO!.source_ != null && videoDTO!.source_!.length > 0 ) {
|
|
||||||
videoId = YoutubePlayer.convertUrlToId(videoDTO!.source_!);
|
|
||||||
|
|
||||||
if (true) {
|
if (_sourceType == _VideoSourceType.youtube) {
|
||||||
final _controllerWeb = iframe.YoutubePlayerController(
|
_youtubeController = iframe.YoutubePlayerController(
|
||||||
params: iframe.YoutubePlayerParams(
|
params: iframe.YoutubePlayerParams(
|
||||||
mute: false,
|
mute: false,
|
||||||
showControls: true,
|
showControls: true,
|
||||||
@ -43,58 +53,57 @@ class _VideoView extends State<VideoView> {
|
|||||||
strictRelatedVideos: false,
|
strictRelatedVideos: false,
|
||||||
enableKeyboard: false,
|
enableKeyboard: false,
|
||||||
enableCaption: false,
|
enableCaption: false,
|
||||||
pointerEvents: iframe.PointerEvents.auto
|
pointerEvents: iframe.PointerEvents.auto,
|
||||||
),
|
userAgent: _browserUserAgent,
|
||||||
);
|
|
||||||
|
|
||||||
_controllerWeb.loadVideo(videoDTO!.source_!);
|
|
||||||
|
|
||||||
_videoViewWeb = iframe.YoutubePlayer(
|
|
||||||
controller: _controllerWeb,
|
|
||||||
//showVideoProgressIndicator: false,
|
|
||||||
/*progressIndicatorColor: Colors.amber,
|
|
||||||
progressColors: ProgressBarColors(
|
|
||||||
playedColor: Colors.amber,
|
|
||||||
handleColor: Colors.amberAccent,
|
|
||||||
),*/
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
videoId = YoutubePlayer.convertUrlToId(videoDTO!.source_!);
|
|
||||||
YoutubePlayerController _controller = YoutubePlayerController(
|
|
||||||
initialVideoId: videoId!,
|
|
||||||
flags: YoutubePlayerFlags(
|
|
||||||
autoPlay: true,
|
|
||||||
controlsVisibleAtStart: false,
|
|
||||||
loop: true,
|
|
||||||
hideControls: false,
|
|
||||||
hideThumbnail: false,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
_videoView = YoutubePlayer(
|
|
||||||
controller: _controller,
|
|
||||||
//showVideoProgressIndicator: false,
|
|
||||||
progressIndicatorColor: Colors.amber,
|
|
||||||
progressColors: ProgressBarColors(
|
|
||||||
playedColor: Colors.amber,
|
|
||||||
handleColor: Colors.amberAccent,
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
_youtubeController!.loadVideo(source);
|
||||||
|
} else if (_sourceType == _VideoSourceType.vimeo) {
|
||||||
|
final vimeoId = _extractVimeoId(source);
|
||||||
|
if (vimeoId != null) {
|
||||||
|
_vimeoController = WebViewController()
|
||||||
|
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||||
|
..setUserAgent(_browserUserAgent)
|
||||||
|
..loadRequest(Uri.parse('https://player.vimeo.com/video/$vimeoId'));
|
||||||
}
|
}
|
||||||
super.initState();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_videoView = null;
|
_youtubeController?.close();
|
||||||
_videoViewWeb = null;
|
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) => videoDTO!.source_ != null && videoDTO!.source_!.length > 0 ?
|
Widget build(BuildContext context) {
|
||||||
(true ? _videoViewWeb! : _videoView!):
|
final source = widget.section.source_;
|
||||||
Center(child: Text("La vidéo ne peut pas être affichée, l'url est incorrecte", style: new TextStyle(fontSize: kNoneInfoOrIncorrect)));
|
if (source == null || source.isEmpty) {
|
||||||
|
return Center(
|
||||||
|
child: Text(
|
||||||
|
"La vidéo ne peut pas être affichée, l'url est incorrecte",
|
||||||
|
style: TextStyle(fontSize: kNoneInfoOrIncorrect),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (_sourceType) {
|
||||||
|
case _VideoSourceType.youtube:
|
||||||
|
return iframe.YoutubePlayer(controller: _youtubeController!);
|
||||||
|
case _VideoSourceType.vimeo:
|
||||||
|
if (_vimeoController == null) {
|
||||||
|
return Center(
|
||||||
|
child: Text(
|
||||||
|
"Impossible d'extraire l'identifiant Vimeo depuis l'URL",
|
||||||
|
style: TextStyle(fontSize: kNoneInfoOrIncorrect),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return WebViewWidget(controller: _vimeoController!);
|
||||||
|
case _VideoSourceType.direct:
|
||||||
|
return VideoViewer(videoUrl: source, file: null);
|
||||||
|
default:
|
||||||
|
return const SizedBox();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user