fluttercommunity / fluttercommunity/chewie

When playing multiple videos, playing one of them will pause the others

Open
#536 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Dart
Stars
2.1k
Forks
1.1k
Avg merge
6d 8h
Merged PRs (30d)
6

Description

Video:

https://user-images.githubusercontent.com/25337480/132444214-3dc1e23b-2123-430a-8876-7ea6be5aabb6.mp4

I put Chewie inside the ListView, like this.

```dart
Container(
height: (210 * chewieControllers.length).h,
child: ListView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: chewieControllers.length,
itemBuilder: (context, index) {
return Container(
height: 200.h,
decoration: BoxDecoration(
color: Colors.grey,
),
child: Chewie(
controller: chewieControllers[index],
),
);
}),
)
```

The videoPlayerControllers and chewieControllers were added by me in a loop, like this.

```dart
void videoPlayerInit() {
List videoList = [
/// links...
];

for (var videoUrl in videoList) {
var tempVideoPlayerController = VideoPlayerController.network(videoUrl);
videoPlayerControllers.add(tempVideoPlayerController);

var tempChewieController = ChewieController(
videoPlayerController: tempVideoPlayerController,
aspectRatio: 3 / 2,
autoPlay: false,
looping: false,
autoInitialize: true,
allowPlaybackSpeedChanging: false
);

tempChewieController.addListener(() {
//判断是否全屏
if (!tempChewieController.isFullScreen) {
SystemChrome.setPreferredOrientations([
// 强制竖屏
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown
]);
}
});

chewieControllers.add(tempChewieController);
}
}
```

Then I control the playback like this

```dart
PlayButton(event: (bool isPlay) {
for (var videoPlayerController in videoPlayerControllers) {
if(isPlay){
videoPlayerController.play();
}else{
videoPlayerController.pause();
}
}
}),
```

PlayButton is my custom button that returns a bool

```dart
class PlayButton extends StatefulWidget {
final Function event;

const PlayButton({Key key, @required this.event}) : super(key: key);

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

class _PlayButtonState extends State {
bool _isPlay = false;

@override
Widget build(BuildContext context) {
return ElevatedButton(
style: ButtonStyle(
minimumSize: MaterialStateProperty.all(Size(156, 36)),
elevation: MaterialStateProperty.all(0),
backgroundColor: MaterialStateProperty.all(Color(0xFFe12f36)),
shape: MaterialStateProperty.all(
StadiumBorder(),
),
),
onPressed: () {
setState(() {
_isPlay = !_isPlay;
});
if (widget.event != null) {
widget.event(_isPlay);
}
},
child: Text(
_isPlay ? '暂停' : '播放',
style: TextStyle(
fontSize: 14.h,
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
);
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.