skip to Main Content
// ignore_for_file: library_private_types_in_public_api, avoid_print

import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';
import 'package:fluttertoast/fluttertoast.dart';

class HymnTune extends StatefulWidget {
  final String hymnMusicPath;
  const HymnTune({super.key, required this.hymnMusicPath});

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

class _HymnTuneState extends State<HymnTune> with SingleTickerProviderStateMixin {
  late AnimationController iconController;
  bool isPlaying = false;
  double iconSize = 30.0;
  final AudioPlayer hymnTunePlayer = AudioPlayer();

  @override
  void initState() {
    super.initState();
    iconController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 100),
    );
    loadMusic();
  }

  Future<void> loadMusic() async {
    if (widget.hymnMusicPath.isNotEmpty) {
      try {
        await hymnTunePlayer.setAudioSource(AudioSource.uri(Uri.parse(widget.hymnMusicPath)));
      } catch (e) {
        Fluttertoast.showToast(
          msg: "Error loading audio source: $e",
          textColor: Colors.white,
          backgroundColor: Colors.black45,
          toastLength: Toast.LENGTH_LONG,
          gravity: ToastGravity.BOTTOM,
        );
      }
    }
  }

  Future<bool> checkInternetConnection() async {
    var connectivityResult = await Connectivity().checkConnectivity();
    return connectivityResult != ConnectivityResult.none;
  }

  void showNoInternetToast() {
    Fluttertoast.showToast(
      msg: 'No Internet Connection',
      textColor: Colors.white,
      backgroundColor: Colors.black45,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.BOTTOM,
    );
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => animateIcon(),
      child: AnimatedIcon(
        icon: AnimatedIcons.play_pause,
        progress: iconController,
        size: iconSize,
      ),
    );
  }

  void animateIcon() async {
    if (widget.hymnMusicPath.isNotEmpty) {
      bool isConnected = await checkInternetConnection();

      if (!isConnected) {
        showNoInternetToast();
        return;
      }

      setState(() {
        isPlaying = !isPlaying;
      });

      if (isPlaying) {
        await iconController.forward();
        await hymnTunePlayer.play();
      } else {
        await iconController.reverse();
        await hymnTunePlayer.pause();
      }
    }
  }

  @override
  void dispose() {
    hymnTunePlayer.dispose();
    iconController.dispose();
    super.dispose();
  }
}

E/flutter ( 7782): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method check on channel plugins.flutter.io/connectivity)
E/flutter ( 7782): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:332:7)
E/flutter ( 7782):
E/flutter ( 7782): #1 MethodChannelConnectivity.checkConnectivity (package:connectivity_platform_interface/src/method_channel_connectivity.dart:41:9)
E/flutter ( 7782):
E/flutter ( 7782): #2 _HymnTuneState.checkInternetConnection (package:disciples_hymn/screens/hymn_tune.dart:49:30)
E/flutter ( 7782):
E/flutter ( 7782): #3 _HymnTuneState.animateIcon (package:disciples_hymn/screens/hymn_tune.dart:77:26)
E/flutter ( 7782):
E/flutter ( 7782):

2

Answers


  1. start your app to run again (don’t hot restart / hot reload)

    STOP THE APP AND RE-RUN

    Login or Signup to reply.
  2. you’re using the discounted package for checking connectivity and that package is not supported on the Linux platform you can check here, You can use connectivity_plus, you can use a similar code to check the internet connectivity, just need to change the import file line.
    As shown below,

    import package

    import 'package:connectivity_plus/connectivity_plus.dart';
    

    connectivity checking

    Future<bool> checkInternetConnection() async {
        var connectivityResult = await Connectivity().checkConnectivity();
        return connectivityResult != ConnectivityResult.none;
      }
    

    NOTE: This doesn’t check the active internet connection with the device you have to write separate logic for that you can you internet_connection_checker_plus package for the same purpose.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search