can play youtube video

pull/310/head
li3317 4 years ago
parent a78fa1957f
commit d3e5d5603e
  1. 2
      example/android/app/build.gradle
  2. 10
      lib/src/widgets/editor.dart
  3. 80
      lib/src/widgets/youtube_video_app.dart
  4. 1
      pubspec.yaml

@ -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

@ -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 '

@ -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<YoutubeVideoApp> {
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();
}
}

@ -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:

Loading…
Cancel
Save