alexmercerind / dart_vlc

Flutter bindings to libVLC.
GNU Lesser General Public License v2.1
513 stars 137 forks source link

[PageView] Cannot call dispose function? #180

Closed ggdream closed 2 years ago

ggdream commented 3 years ago

Please see my code:

PageView.builder(
  itemCount: 4,
  itemBuilder: (ctx, index) {
    return xxx();
}
)

# xxx(): a packed widget, used stf and dart_vlc.Video.

Under normal circumstances, the children of PageView are not cached, and the original Widget will be cleared when the page is switched, but the dispose of xxx() is not called normally.

Want to know what is the reason and how to solve it? thanks

alexmercerind commented 3 years ago

I'm not sure about that. Could you share your code & be more specific? Is Player.dispose not working?

ggdream commented 3 years ago

Please look:

It stands to reason that when PageView is switched, its child Widget will be destroyed. In my case, VideoPlayerOther.dispose should be called, but after debugging, it is not called. I hope can give me some pointers, thank you

PageView.builder(
        scrollDirection: Axis.vertical,
        onPageChanged: _controller.onPage,
        controller: _controller.pageController,
        itemCount: _controller.urls.length,
        itemBuilder: (context, index) {
          if (GetPlatform.isMobile) {
            return VideoPlayer(
              video: _controller.urls[index],
            );
          } else if (GetPlatform.isWeb || GetPlatform.isDesktop) {
            return VideoPlayerOther(
              video: _controller.urls[index],
            );
          }

          return SizedBox();
        },
      );
    })
import 'dart:math';

import 'package:dart_vlc/dart_vlc.dart';
import 'package:flutter/material.dart';

class VideoPlayerOther extends StatefulWidget {
  const VideoPlayerOther({
    Key? key,
    required this.video,
  }) : super(key: key);

  final String video;

  @override
  _VideoPlayerOtherState createState() => _VideoPlayerOtherState();
}

class _VideoPlayerOtherState extends State<VideoPlayerOther> {
  late final Player _player;
  bool _fullScreen = false;

  @override
  void initState() {
    super.initState();

    final media = Media.network(widget.video);
    _player = Player(id: Random().nextInt(65536))
      ..setPlaylistMode(PlaylistMode.repeat)
      ..open(media, autoStart: true);
  }

  @override
  void dispose() {
    _player.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return RotatedBox(
      quarterTurns: _fullScreen ? 1 : 0,
      child: GestureDetector(
        onTap: () => _player.playOrPause(),
        onLongPress: () {
          setState(() {
            _fullScreen = !_fullScreen;
          });
        },
        child: Video(
          player: _player,
          showControls: false,
        ),
      ),
    );
  }
}

Addition:

alexmercerind commented 2 years ago

Sorry to say I don't think this is related to dart_vlc or it's Native/Dart implementation. Closing.