solid-software / flutter_vlc_player

📺 Flutter VLC powered video player.
507 stars 247 forks source link

How to change itemcount of Gridview without play vlc player again #42

Open Marcus-Ospicon opened 4 years ago

Marcus-Ospicon commented 4 years ago

Hi everyone,

I am using the vlc player to show multiple videos in a GridView, it works fine however I have a small problem when changing the itemcount of gridview, if the vlc player is stopping it would automatically play again. I want to keep the playingState when changing the itemCount.

Here is my code

Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: false,
      backgroundColor: Theme.of(context).primaryColor,
      appBar: appBarLayout(context),
      body: ChangeNotifierProvider<MenuCameraViewModel>.value(
        value: _viewModel,
        child: Consumer<MenuCameraViewModel>(
          builder: (context, model, _) => GridView.builder(
            itemCount: model.getItemCount() + 1,
            padding: EdgeInsets.all(5),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: model.viewMode ? 1 : 2,
              childAspectRatio: 16 / 11,
            ),
            itemBuilder: (context, int index) {
              return GestureDetector(
                child: Container(
                  margin: EdgeInsets.all(5),
                  color: Colors.black.withOpacity(0.5),
                  child: VlcPlayer(
                    aspectRatio: 16 / 9,
                    url: model.getUrl(index),
                    controller: model.getController(index),
                    placeholder: Center(child: CircularProgressIndicator()),
                  ),
                ),
                onTap: () {
                  model.tap(index);
                },
              );
            },
          ),
        ),
      ),
    );
  }

Thanks!

jamiethain commented 4 years ago

Build a Wrapper around VLCPlayer from a StatefulWidget that contains and set a parameter in the State like

boolean isPlaying = true; 

And call the player when it is playing or not and play whether it's playing. 

Jamie

Marcus-Ospicon commented 4 years ago

isPlaying

Thanks, but I resolved it by using controller.listening combining with a isPlaying variable to stop when the variable inverse the state's controller

terry8204 commented 4 hours ago

@jamiethain @Marcus-Ospicon can you tell me how to resolve it ? paste some example code?

terry8204 commented 4 hours ago

I just comment out then can play, but need wait 2~3 seconds no video if (value.isInitialized) { throw Exception('Already Initialized'); } in the vlc_player_controller.dart file: Future initialize() async { if (_isDisposed) { throw Exception( 'initialize was called on a disposed VlcPlayerController'); } if (value.isInitialized) { throw Exception('Already Initialized'); }