Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
3.3 kB
1
Indexable
Never
class SimplePlayer extends StatefulWidget {
 
  const SimplePlayer();

  @override
  State<SimplePlayer> createState() => _SimplePlayerState();
}

class _SimplePlayerState extends State<SimplePlayer> with WidgetsBindingObserver {
  bool isLoadSuccess = false;
  @override
  void initState() {
    init();
    initializePlayer();
    super.initState();
  }

  initializePlayer() {
    viewController = BccmPlayerViewController(
      playerController: BccmPlayerController.primary,
      config: BccmPlayerViewConfig(
        useSurfaceView: false,
        deviceOrientationsFullscreen: (viewController) {
          final videoSize = viewController.playerController.value.videoSize;
          if (videoSize == null || videoSize.aspectRatio == 1) {
            return [DeviceOrientation.landscapeLeft];
          }
          return null;
        },
        deviceOrientationsNormal: (viewController) => [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
        controlsConfig: BccmPlayerControlsConfig(
          additionalActionsBuilder: (context) => [
            if (Platform.isIOS)
              Padding(
                padding: EdgeInsets.only(right: 4),
                child: Transform.scale(
                  scale: 0.85,
                  child: AirPlayRoutePickerView(
                    width: 20,
                    height: 34,
                    prioritizesVideoDevices: true,
                    tintColor: Colors.white,
                    activeTintColor: Colors.white,
                    backgroundColor: Colors.transparent,
                  ),
                ),
              )
          ],
        ),
      ),
    );
  }

  init() async {

    MediaItem item = MediaItem(
      url: <YOUR MEDIA URL>,
      mimeType: <YOUR MEDIA URL>.contains('.m3u8') ? 'application/x-mpegURL' : 'video/mp4',
      metadata: MediaMetadata(title: <TITLE OF YOUR MEDIA>),
    );
  

    await BccmPlayerInterface.instance.replaceCurrentMediaItem(BccmPlayerController.primary.value.playerId, item, autoplay: true).then((value) {
      if (mounted) {
        setState(() {
          isLoadSuccess = true;
        });
      } else {
        _stopAndDisposePlayerAfterLoading();
      }
    });

  }


  @override
  void dispose() {
    _stopAndDisposePlayer(); // Stop and dispose the player
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  void _stopAndDisposePlayer() async {
    viewController.playerController.pause();
    viewController.playerController.stop(reset: true);
  }

  void _stopAndDisposePlayerAfterLoading() async {
    viewController.playerController.pause();
    viewController.playerController.stop(reset: true);
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: [
        (isLoadSuccess)
            ? BccmPlayerView.withViewController(viewController)
            : CircularProgressIndicator(
                strokeWidth: 2,
                valueColor: AlwaysStoppedAnimation(
                  defaultLoaderAccentColorGlobal ?? Theme.of(context).colorScheme.secondary,
                ),
              ),
      ],
    );
  }
}
Leave a Comment