From d3e5d5603e31ea0700c11333bece99b90355a644 Mon Sep 17 00:00:00 2001 From: li3317 Date: Sat, 24 Jul 2021 18:08:05 +0800 Subject: [PATCH] can play youtube video --- example/android/app/build.gradle | 2 +- lib/src/widgets/editor.dart | 10 +++- lib/src/widgets/youtube_video_app.dart | 80 ++++++++++++++++++++++++++ pubspec.yaml | 1 + 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 lib/src/widgets/youtube_video_app.dart diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index b53f38f0..86d081d8 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -34,7 +34,7 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.example.app" - minSdkVersion 16 + minSdkVersion 17 targetSdkVersion 29 versionCode flutterVersionCode.toInteger() versionName flutterVersionName diff --git a/lib/src/widgets/editor.dart b/lib/src/widgets/editor.dart index 02f4a4cd..774c0282 100644 --- a/lib/src/widgets/editor.dart +++ b/lib/src/widgets/editor.dart @@ -8,6 +8,7 @@ import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; +import 'package:flutter_quill/src/widgets/youtube_video_app.dart'; import 'package:string_validator/string_validator.dart'; import 'package:url_launcher/url_launcher.dart'; @@ -109,7 +110,14 @@ Widget _defaultEmbedBuilder( : Image.file(io.File(imageUrl)); case 'video': final videoUrl = node.value.data; - return VideoApp(videoUrl: videoUrl, context: context, readOnly: readOnly); + if (videoUrl.toString().contains('www.youtube.com') || + videoUrl.toString().contains('youtu.be')) { + return YoutubeVideoApp( + videoUrl: videoUrl, context: context, readOnly: readOnly); + } else { + return VideoApp( + videoUrl: videoUrl, context: context, readOnly: readOnly); + } default: throw UnimplementedError( 'Embeddable type "${node.value.type}" is not supported by default ' diff --git a/lib/src/widgets/youtube_video_app.dart b/lib/src/widgets/youtube_video_app.dart new file mode 100644 index 00000000..f299f1fd --- /dev/null +++ b/lib/src/widgets/youtube_video_app.dart @@ -0,0 +1,80 @@ +import 'package:flutter/gestures.dart'; +import 'package:flutter/material.dart'; +import 'package:url_launcher/url_launcher.dart'; +import 'package:youtube_player_flutter/youtube_player_flutter.dart'; +import '../../flutter_quill.dart'; + +class YoutubeVideoApp extends StatefulWidget { + const YoutubeVideoApp( + {required this.videoUrl, required this.context, required this.readOnly}); + + final String videoUrl; + final BuildContext context; + final bool readOnly; + + @override + _YoutubeVideoAppState createState() => _YoutubeVideoAppState(); +} + +class _YoutubeVideoAppState extends State { + var _youtubeController; + + @override + void initState() { + super.initState(); + final videoId = YoutubePlayer.convertUrlToId(widget.videoUrl); + if (videoId != null) { + _youtubeController = YoutubePlayerController( + initialVideoId: videoId, + flags: const YoutubePlayerFlags( + autoPlay: false, + ), + ); + } + } + + @override + Widget build(BuildContext context) { + final defaultStyles = DefaultStyles.getInstance(context); + if (_youtubeController == null) { + print('problem with youtube video'); + if (widget.readOnly) { + return RichText( + text: TextSpan( + text: widget.videoUrl, + style: defaultStyles.link, + recognizer: TapGestureRecognizer() + ..onTap = () => launch(widget.videoUrl)), + ); + } + + return RichText( + text: TextSpan(text: widget.videoUrl, style: defaultStyles.link)); + } + + return Container( + height: 300, + child: YoutubePlayerBuilder( + player: YoutubePlayer( + controller: _youtubeController, + showVideoProgressIndicator: true, + ), + builder: (context, player) { + return Column( + children: [ + // some widgets + player, + //some other widgets + ], + ); + }, + ), + ); + } + + @override + void dispose() { + super.dispose(); + _youtubeController.dispose(); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 27433734..add26fd5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -24,6 +24,7 @@ dependencies: pedantic: ^1.11.0 video_player: ^2.1.10 characters: ^1.1.0 + youtube_player_flutter: ^8.0.0 dev_dependencies: flutter_test: