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

Android: After 30 seconds of live streaming, the icon changes to "start over/restart", although the broadcast continues and the expected result is "pause". #894

Open
ohlegs opened this issue Feb 12, 2025 · 1 comment

Comments

@ohlegs
Copy link

ohlegs commented Feb 12, 2025

2025-02-12.17.24.09.mov

Code example but not work in web: https://zapp.run/edit/flutter-zlkg06gqlkh0?entry=lib/main.dart&file=lib/main.dart:0-4178

OR

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


const primaryColor = Color(0xFF0C3955);
const backgroudColor = Color(0xFFEDF1F7);

class TranslationScreen extends StatefulWidget {
  final String name;
  final bool isLive;

  const TranslationScreen({
    super.key,
    required this.name,
    required this.isLive,
  });

  @override
  State<StatefulWidget> createState() {
    return _TranslationScreenState();
  }
}

class _TranslationScreenState extends State<TranslationScreen> {
  VideoPlayerController? _videoPlayerController;
  late ChewieController _chewieController;
  int? bufferDelay;
  bool _isError = false;
  final bool _isVideoFinished = false;

  @override
  void initState() {
    super.initState();
    initializePlayer('https://back.art-caramel.ru/stream/5debd92c-5247-4760-9bca-176290c2f4b8.m3u8');
  }

  @override
  void dispose() {
    _videoPlayerController?.dispose();
    _chewieController.dispose();
    super.dispose();
  }

  Uri _getUrl(String videoUrl) {
    return Uri.parse(videoUrl.trim());
  }

  Future<void> initializePlayer(String videoUrl) async {
    try {
      _videoPlayerController = VideoPlayerController.networkUrl(_getUrl(videoUrl));
      await _videoPlayerController!.initialize();
      _videoPlayerController!.addListener(checkVideo);
      _createChewieController();
      setState((){});
    } catch (e) {
      print(e);
      setState(() {
        _isError = true;
      });
    }
  }

  void _createChewieController() {
    if (_videoPlayerController != null) {
      _chewieController = ChewieController(
        isLive: widget.isLive,
        videoPlayerController: _videoPlayerController!,
        autoPlay: true,
        showControlsOnInitialize: false,
        deviceOrientationsAfterFullScreen: [
          DeviceOrientation.portraitUp,
          DeviceOrientation.portraitDown,
        ],
        aspectRatio: 16 / 9,
        looping: false,
        materialProgressColors: ChewieProgressColors(
          bufferedColor: Colors.grey,
          playedColor: primaryColor,
        ),
        cupertinoProgressColors: ChewieProgressColors(
          bufferedColor: Colors.grey,
          playedColor: primaryColor,
        ),
        controlsSafeAreaMinimum: const EdgeInsets.all(8),
        progressIndicatorDelay: bufferDelay != null ? Duration(milliseconds: bufferDelay!) : null,
        hideControlsTimer: const Duration(seconds: 5),
      );
      setState(() {}); // Trigger a rebuild to update the UI with the Chewie controller
    }
  }

  void checkVideo() {
    // if (_videoPlayerController1!.value.position ==
    //     Duration(seconds: 0, minutes: 0, hours: 0)) {
    //   print('video Started');
    // }

    // if (_videoPlayerController1!.value.isCompleted) {
    //   logger.d('video Ended');
    //   _chewieController!.pause();
    // }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: _isError ? backgroudColor : Colors.black,
      appBar: AppBar(
        foregroundColor: backgroudColor,
        leading: IconButton(
          icon: const Icon(
            Icons.arrow_back,
            color: primaryColor,
          ),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        elevation: 0,
        backgroundColor: backgroudColor,
        centerTitle: false,
        title: Text(widget.name),
      ),
      body: _videoPlayerController != null && _videoPlayerController!.value.isInitialized
          ? Chewie(controller: _chewieController)
          : const Center(child: CircularProgressIndicator()), // Show a loading indicator while initializing
    );
  }
}
@ohlegs
Copy link
Author

ohlegs commented Feb 12, 2025

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

1 participant