Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get onClickListener for play button. #16

Closed
smzranz opened this issue Apr 30, 2018 · 6 comments
Closed

Get onClickListener for play button. #16

smzranz opened this issue Apr 30, 2018 · 6 comments

Comments

@smzranz
Copy link

smzranz commented Apr 30, 2018

Am new to flutter and dart.
Is there any delegate method or any OnClickListener method when tapping play button.
On Pressed of floating button is not working properly.
I tried to add Gesture too.

import 'package:chewie/chewie.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter/services.dart';



class ChewieDemo extends StatefulWidget {
  final String title;

  ChewieDemo({this.title = 'Chewie Demo'});

  @override
  _ChewieDemoState createState() => new _ChewieDemoState();
}

class _ChewieDemoState extends State<ChewieDemo> {
  TargetPlatform _platform = TargetPlatform.android;
  VideoPlayerController _controller;




  @override
  void initState() {
    super.initState();
    _platform = TargetPlatform.android;
    _controller = new VideoPlayerController.network(
      'https://flutter.github.io/assets-for-api-docs/videos/butterfly.mp4',
    );

  }

  @override
  void didUpdateWidget(ChewieDemo oldWidget) {
    print("didUpdateWidget");
    new SimpleDialog(
      title: const Text('Select assignment'),
      children: <Widget>[
        new SimpleDialogOption(
          onPressed: () {
            //  Navigator.pop(context, Department.treasury);
          },
          child: const Text('Treasury department'),
        ),
        new SimpleDialogOption(
          onPressed: () {
            //Navigator.pop(context, Department.state);
          },
          child: const Text('State department'),
        ),
      ],
    );
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Column(
        children: <Widget>[
          new Chewie(
            _controller,
            aspectRatio: 3 / 2,
            autoPlay: false,
            looping: true,
            autoInitialize: true,
            materialProgressColors: new ChewieProgressColors(
              playedColor: Colors.red,
              handleColor: Colors.blue,
              backgroundColor: Colors.grey,
              bufferedColor: Colors.lightGreen,
            ),
          ),
        ],
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed:
            _controller.value.isPlaying ? _controller.pause : _controller.play,
        child: new Icon(
          _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }
}
@brianegan
Copy link
Collaborator

brianegan commented Apr 30, 2018

Hey, thanks for writing in. I'm not handling that case effectively. I mostly assumed with this plugin that you'd just use Chewie for playback, rather than providing additional controls outside of the widget, but it could be changed to support this.

Traveling for the next couple weeks, but happy to accept a PR if you need this functionality sooner, or I'll try to take a look when I'm back in action!

@brianegan
Copy link
Collaborator

After looking at this one, rather than supplying an onClick listener, do you think it would make more sense to simply listen for changes to the VideoPlayerController? You can register callbacks on that which would give you access to all states of the player (isPlaying, paused, isBuffering, etc).

@brianegan
Copy link
Collaborator

brianegan commented Jun 11, 2018

Hi @smzranz -- does the idea of listening to the play events from the videoPlayerController work for ya? Or would ya need the event handler to fire as soon as tapping the button?

@smzranz
Copy link
Author

smzranz commented Jun 12, 2018

hi,
@brianegan Sorry for the late reply.
i had already tried with VideoPlayerController?.I tried adding a floating button .Its not working.
Here is the code that i have tried.

floatingActionButton: new FloatingActionButton(
          child: new Icon(Icons.play_arrow),
          onPressed: () {
            if (_controller.value.isPlaying) {
              setState(() {
                _controller.pause;
                child:
                new Icon(Icons.pause);
              });
            } else {
              setState(() {
                _controller.play;
                child:
                new Icon(Icons.play_arrow);
              });
            }
          })```

@brianegan
Copy link
Collaborator

Ah, I was thinking of a slightly different use-case, sorry about that. Ok, I'm thinking of introducing a ChewiePlayerController which would hopefully make this type of thing easier for different use-cases.

I'll try that path and see if it makes this a bit easier to work with

@cbenhagen
Copy link
Collaborator

I don't think there is much to do on our side. Adding play / pause can be easily done from the outside by using the play / pause methods of the controller. @smzranz instead of controller.pause; you should use controller.pause();. Also you should set the Icon based on isPlaying:

floatingActionButton: FloatingActionButton(
    child: _controller.value.isPlaying
        ? Icon(Icons.pause)
        : Icon(Icons.play_arrow),
    onPressed: () {
      if (_controller.value.isPlaying) {
        setState(() {
          _controller.pause();
        });
      } else {
        setState(() {
          _controller.play();
        });
      }
    }),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants