Advertisement
serjuzu

FullScreenPlayer con sonido/mute

May 29th, 2024 (edited)
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.84 KB | Software | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_toktik/presentation/widgets/video/video_background.dart';
  3. import 'package:video_player/video_player.dart';
  4.  
  5. class FullScreenPlayer extends StatefulWidget {
  6.   final String videoUrl;
  7.   final String caption;
  8.  
  9.   const FullScreenPlayer(
  10.       {super.key, required this.videoUrl, required this.caption});
  11.  
  12.   @override
  13.   State<FullScreenPlayer> createState() => _FullScreenPlayerState();
  14. }
  15.  
  16. class _FullScreenPlayerState extends State<FullScreenPlayer> {
  17.   late VideoPlayerController controller;
  18.   bool isMuted = true; // Variable para trackear el estado del mute
  19.  
  20.   @override
  21.   void initState() {
  22.     super.initState();
  23.  
  24.     controller = VideoPlayerController.asset(widget.videoUrl)
  25.       ..setVolume(0)
  26.       ..setLooping(true)
  27.       ..play();
  28.   }
  29.  
  30.   @override
  31.   void dispose() {
  32.     controller.dispose();
  33.     super.dispose();
  34.   }
  35.  
  36.   void _toggleVolume() {
  37.     setState(() {
  38.       if (isMuted) {
  39.         controller.setVolume(1.0); // Setea el volumen al que haya en el dispositivo
  40.       } else {
  41.         controller.setVolume(0); // Mutea el video
  42.       }
  43.       isMuted = !isMuted;
  44.     });
  45.   }
  46.  
  47.   @override
  48.   Widget build(BuildContext context) {
  49.     return FutureBuilder(
  50.       future: controller.initialize(),
  51.       builder: (context, snapshot) {
  52.         if (snapshot.connectionState != ConnectionState.done) {
  53.           return const Center(
  54.             child: CircularProgressIndicator(
  55.               strokeWidth: 2,
  56.             ),
  57.           );
  58.         }
  59.  
  60.         return GestureDetector(
  61.           onTap: () {
  62.             if (controller.value.isPlaying) {
  63.               controller.pause();
  64.               return;
  65.             }
  66.             controller.play();
  67.           },
  68.           onDoubleTap: _toggleVolume,//llamamos al toggle con un doble tap
  69.          
  70.           child: AspectRatio(
  71.               aspectRatio: controller.value.aspectRatio,
  72.               child: Stack(children: [
  73.                 VideoPlayer(controller),
  74.  
  75.                 //gradiente
  76.                 VideoBackground(
  77.                   stops: const [0.8,1.0],
  78.                 ),
  79.                 //Texto
  80.                 Positioned(
  81.                   bottom: 50,
  82.                   left: 20,
  83.                   child: _VideoCaption(caption: widget.caption),
  84.                 )
  85.               ])),
  86.         );
  87.       },
  88.     );
  89.   }
  90. }
  91.  
  92. class _VideoCaption extends StatelessWidget {
  93.   final String caption;
  94.   const _VideoCaption({ required this.caption});
  95.  
  96.   @override
  97.   Widget build(BuildContext context) {
  98.     final size = MediaQuery.of(context).size;
  99.     final titleStyle = Theme.of(context).textTheme.titleLarge;
  100.  
  101.     return SizedBox(
  102.         width: size.width * 0.6,
  103.         child: Text(
  104.           caption,
  105.           maxLines: 2,
  106.           style: titleStyle,
  107.         ));
  108.   }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement