Skip to content

Flutter Custom Video Player Doesn't respect Parent Container Width and Height  #48854

Closed

Description

@premingiet

I have added a VideoPlayer and added it as a child of a Container Widget. Now when video loads, that video player sizes itself based on the video file aspect ratio and doesn't stay within parent container.

I tried with SizedBox and FittedBox widgets too but same issue

Here is a github test repo i made to reproduce the issue

Press the Load Asset Video button to see the issue.
I have used Android device to test it

Activity

  1. iapicca commented on Jan 15, 2020

    @iapicca

    Hi @premingiet
    I see here that you are using
    the third party plugin flutter_ijkplayer,
    you may want to open an issue in the dedicated github

    Closing, as this isn't an issue with Flutter itself,
    if you disagree please write in the comments and I will reopen it
    Thank you

  2. n-santamaria commented on Feb 21, 2020

    @n-santamaria

    Hello @iapicca,
    I've faced the similar issue with video_player plugin.

    import 'dart:async';
    import 'package:flutter/material.dart';
    import 'package:flutter_widgets/flutter_widgets.dart';
    import 'package:video_player/video_player.dart';

    class VideoPlayerScreen extends StatefulWidget {

    final String videoUrl;

    VideoPlayerScreen({@required this.videoUrl, Key key}) : super(key: key);

    @OverRide
    _VideoPlayerScreenState createState() => _VideoPlayerScreenState();
    }

    class _VideoPlayerScreenState extends State {

    VideoPlayerController _controller;
    Future _initializeVideoPlayerFuture;
    bool _volumeUp = true;
    var _scaffoldKey = new GlobalKey();
    var a;

    @OverRide
    void initState() {
    // Create and store the VideoPlayerController. The VideoPlayerController
    // offers several different constructors to play videos from assets, files,
    // or the internet.
    _controller = VideoPlayerController.network(
    widget.videoUrl
    );

    // Initialize the controller and store the Future for later use.
    _initializeVideoPlayerFuture = _controller.initialize();
    
    _controller.addListener(checkIfVideoFinished);
    
    super.initState();
    

    }

    void checkIfVideoFinished() {
    if(_controller.value.position == _controller.value.duration) {
    _controller.removeListener(checkIfVideoFinished);
    setState(() {
    _controller.pause();
    });
    }
    }

    @OverRide
    void dispose() {
    // Ensure disposing of the VideoPlayerController to free up resources.
    _controller.removeListener(checkIfVideoFinished);
    _controller.dispose();

    super.dispose();
    

    }

    @OverRide
    Widget build(BuildContext context) {
    return VisibilityDetector(
    key: _scaffoldKey,
    onVisibilityChanged: (VisibilityInfo info) {
    if(info.visibleFraction < 1.0) {
    if(_controller.value.isPlaying) {
    if(this.mounted) {
    setState(() {
    _controller.pause();
    });
    }
    }
    }
    // debugPrint( widget.videoUrl + "\n ${info.visibleFraction} of my widget is visible");
    },
    child: Stack(
    children: [
    FutureBuilder(
    future: _initializeVideoPlayerFuture,
    builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
    // If the VideoPlayerController has finished initialization, use
    // the data it provides to limit the aspect ratio of the video.
    return Align(
    alignment: Alignment.center,
    child: AspectRatio(
    aspectRatio: 4/3,
    child: VideoPlayer(_controller),
    ),
    );
    } else {
    // If the VideoPlayerController is still initializing, show a
    // loading spinner.
    return Center(child: CircularProgressIndicator());
    }
    },
    ),
    Align(
    alignment: Alignment.bottomRight,
    child: Container(
    color: Colors.black87.withOpacity(0.3),
    child: Row(
    mainAxisSize: MainAxisSize.min,
    children: [
    IconButton(
    icon: Icon(_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,),
    color: Colors.white,
    onPressed: () {
    if (_controller.value.isPlaying) {
    setState(() {
    _controller.pause();
    });
    _controller.removeListener(checkIfVideoFinished);
    } else {
    setState(() {
    _controller.play();
    });
    _controller.addListener(checkIfVideoFinished);
    }
    },
    ),
    IconButton(
    icon: Icon(_volumeUp ? Icons.volume_up : Icons.volume_off,),
    color: Colors.white,
    onPressed: () {
    setState(() {
    if (_volumeUp) {
    _controller.setVolume(0.0);
    _volumeUp = false;
    } else {
    _controller.setVolume(1.0);
    _volumeUp = true;
    }
    });
    },
    ),
    ],
    ),
    ),
    ),
    ],
    ),
    );
    }
    }

    video_player

  3. iapicca commented on Feb 21, 2020

    @iapicca

    Hi @n-santamaria
    could you (or anyone with the same problem)
    please file a new issue with the exact descriptions what happens, logs and the output of 'flutter doctor -v' please.
    All system setups can be slightly different so it's always better to open new issues and reference related issues.

  4. n-santamaria commented on Feb 22, 2020

    @n-santamaria

    Hello @iapicca
    I've created a new issue.

    Best Regards,
    Neville Santamaria

  5. github-actions commented on Aug 22, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  6. locked as resolved and limited conversation to collaborators on Aug 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions