jhomlala / betterplayer

Better video player for Flutter, with multiple configuration options. Solving typical use cases!
Apache License 2.0
932 stars 1.05k forks source link

[BUG] when open close player screen 4,5 times double sound playing in the application #895

Open Raj-impetrosys opened 2 years ago

Raj-impetrosys commented 2 years ago

class VideoPlayer extends StatefulWidget { @override _VideoPlayerState createState() => _VideoPlayerState(); }

class _VideoPlayerState extends State with WidgetsBindingObserver, RouteAware { BetterPlayerController _betterPlayerController; GlobalKey _betterPlayerKey = GlobalKey(); String videoUrl; AudioPlayer audioPlayer = new AudioPlayer( playerId: 'Soundchat Radio', mode: PlayerMode.MEDIA_PLAYER);

stopaudio() async { await audioPlayer.stop(); play = true; MediaNotification.hideNotification(); }

setUrl() async { // List urls = // Provider.of(context, listen: false).data['data']; // int index = // urls.indexWhere((element) => element["content_type"] == "video"); // videoUrl = Provider.of(context, listen: false) // .data['data'][index]['livelink']; videoUrl = await Sharedpreferences().getLiveVideoUrl(); print(videoUrl); videoPlayerConfig(); }

videoPlayerConfig() { BetterPlayerConfiguration betterPlayerConfiguration = BetterPlayerConfiguration( autoPlay: true, allowedScreenSleep: false, aspectRatio: 16 / 9, fit: BoxFit.contain, deviceOrientationsOnFullScreen: [ DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ], deviceOrientationsAfterFullScreen: [ DeviceOrientation.portraitUp, ], ); BetterPlayerDataSource dataSource = BetterPlayerDataSource(BetterPlayerDataSourceType.network, videoUrl, liveStream: true, cacheConfiguration: BetterPlayerCacheConfiguration( useCache: false, ), videoExtension: "m3u8", notificationConfiguration: BetterPlayerNotificationConfiguration( title: "Sound Chat Radio", imageUrl: logoUrl, showNotification: true, )); _betterPlayerController = BetterPlayerController(betterPlayerConfiguration); _betterPlayerController.setupDataSource(dataSource); _betterPlayerController.setBetterPlayerGlobalKey(_betterPlayerKey); _betterPlayerController.addEventsListener((p0) { switch (p0.betterPlayerEventType) { case BetterPlayerEventType.play: stopaudio(); break; case BetterPlayerEventType.initialized: break; case BetterPlayerEventType.pause: break; case BetterPlayerEventType.seekTo: break; case BetterPlayerEventType.openFullscreen: break; case BetterPlayerEventType.hideFullscreen: break; case BetterPlayerEventType.setVolume: break; case BetterPlayerEventType.progress: break; case BetterPlayerEventType.finished: break; case BetterPlayerEventType.exception: break; case BetterPlayerEventType.controlsVisible: break; case BetterPlayerEventType.controlsHiddenStart: break; case BetterPlayerEventType.controlsHiddenEnd: break; case BetterPlayerEventType.setSpeed: break; case BetterPlayerEventType.changedSubtitles: break; case BetterPlayerEventType.changedTrack: break; case BetterPlayerEventType.changedPlayerVisibility: break; case BetterPlayerEventType.changedResolution: break; case BetterPlayerEventType.pipStart: // _betterPlayerController.play(); break; case BetterPlayerEventType.pipStop: break; case BetterPlayerEventType.setupDataSource: break; case BetterPlayerEventType.bufferingStart: break; case BetterPlayerEventType.bufferingUpdate: break; case BetterPlayerEventType.bufferingEnd: break; case BetterPlayerEventType.changedPlaylistItem: break; } }); }

@override void didChangeAppLifecycleState(AppLifecycleState state) { print(state); switch (state) { case AppLifecycleState.detached: exit(0); break; case AppLifecycleState.paused: _betterPlayerController.enablePictureInPicture(_betterPlayerKey); break; default: break; } super.didChangeAppLifecycleState(state); }

@override void initState() { // WidgetsBinding.instance.addObserver(this); Wakelock.enable(); setUrl(); // videoPlayerConfig(); // createAudiovideoUrlsState(context); // .whenComplete(() => setUrl()) // .whenComplete(() => videoPlayerConfig()); // videoPlayerConfig(); super.initState(); }

///Route aware

// @override // void didChangeDependencies() { // routeObserver.subscribe(this, ModalRoute.of(context)); //Subscribe it here // super.didChangeDependencies(); // }

// /// Called when the current route has been popped off. // @override // void didPop() { // print("didPop"); // super.didPop(); // }

// /// Called when the top route has been popped off, and the current route // /// shows up. // @override // void didPopNext() { // print("didPopNext"); // _betterPlayerController.play(); // super.didPopNext(); // }

// /// Called when the current route has been pushed. // @override // void didPush() { // print("didPush"); // super.didPush(); // }

// /// Called when a new route has been pushed, and the current route is no // /// longer visible. // @override // void didPushNext() { // print("didPushNext"); // _betterPlayerController.pause(); // super.didPushNext(); // }

///

@override void dispose() { // routeObserver.unsubscribe(this); //Don't forget to unsubscribe it!!!!!! // WidgetsBinding.instance.removeObserver(this); _betterPlayerController.pause(); _betterPlayerController.dispose(forceDispose: true); Wakelock.disable(); super.dispose(); }

@override Widget build(BuildContext context) { return Material( color: Colors.black, child: VisibilityDetector( key: Key("visibilitykey1"), onVisibilityChanged: (VisibilityInfo info) { debugPrint("${info.visibleFraction} of my widget is visible"); if (info.visibleFraction == 0) { // _betterPlayerController.pause(); } else { // _betterPlayerController.play(); } }, child: AspectRatio( aspectRatio: 16 / 9, child: (_betterPlayerController != null && _betterPlayerController.isVideoInitialized()) ? BetterPlayer( controller: _betterPlayerController, key: _betterPlayerKey, ) : Center( child: CircularProgressIndicator(), ), ), ), ); } }

31Carlton7 commented 2 years ago

I'm also experiencing this bug

Sumit258000 commented 4 months ago

any update on this. Still facing this issue