Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
6.0 kB
0
Indexable
Never
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:just_audio/just_audio.dart';
import 'package:audio_service/audio_service.dart';

class JustMusicPlay extends StatefulWidget {
  final String audioLink;
  final String thumbnailLink;
  final String headingName;
  final String albumName;
  final String heroTag;
  final AudioPlayer audioPlayer;

  const JustMusicPlay({
    super.key,
    required this.audioLink,
    required this.thumbnailLink,
    required this.headingName,
    required this.heroTag,
    required this.audioPlayer,
    required this.albumName,
  });

  @override
  State<JustMusicPlay> createState() => _JustMusicPlayState();
}

class _JustMusicPlayState extends State<JustMusicPlay> {
  bool isPlaying = false;
  bool isBuffering = false;
  late PlayerState _playerState;

  @override
  void initState() {
    super.initState();
    _playerState = widget.audioPlayer.playerState;
    widget.audioPlayer.playbackEventStream.listen((event) {
      if (event.processingState == ProcessingState.buffering ||
          event.processingState == ProcessingState.loading) {
        // Show buffering dialog
        if (isBuffering == false) {
          setState(() {
            isBuffering = true;
          });
          _showBufferingDialog();
        }
      } 
      else {
        if (isBuffering) {
          Navigator.of(context).pop(); // Close the dialog
          setState(() {
            isBuffering = false;
          });
        }
      }
    });
    WidgetsBinding.instance.addPostFrameCallback((_) {
      playSongs();
    });
  }

  void _showBufferingDialog() {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return const AlertDialog(
          content: Row(
            children: [
              CircularProgressIndicator(),
              SizedBox(width: 20),
              Text("Buffering..."),
            ],
          ),
        );
      },
    );
  }

  playSongs() async {
    try {
      widget.audioPlayer.setAudioSource(
        AudioSource.uri(
          Uri.parse(widget.audioLink),
          tag: newMethod(),
        ),
      );

      if (widget.audioPlayer.playing) {
        setState(() {
          widget.audioPlayer.stop();
        });
      }

      widget.audioPlayer.play();

      widget.audioPlayer.playingStream.listen((playerState) {
        if (mounted) {
          setState(() {
            isPlaying = playerState;
          });
        }
      });

      isPlaying = true;
      print("audio link --------- ${widget.audioLink}");
      print("thumbnail --------- ${widget.thumbnailLink}");
      print("heading --------- ${widget.headingName}");
      print("HERO TAG --------- ${widget.heroTag}");
    } on Exception {
      log("ERROR PARSING DATA");
      Navigator.of(context).pop();
    }
  }

  MediaItem newMethod() {
    return MediaItem(
      id: widget.headingName,
      album: widget.albumName,
      title: widget.headingName,
      artUri: Uri.parse(widget.thumbnailLink),
    );
  }

  @override
  Widget build(BuildContext context) {
    print(widget.audioLink);
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        title: Text(widget.headingName, style: TextStyle(fontSize: 18.h)),
        backgroundColor: Colors.transparent,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_sharp),
          color: Colors.white,
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        actions: [
          IconButton(
            icon: const Icon(Icons.share),
            onPressed: () {},
          ),
        ],
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 20.0, left: 20.0, right: 20.0),
            child: ClipRRect(
                borderRadius: const BorderRadius.all(Radius.circular(10.0)),
                child: Hero(
                    tag: widget.heroTag,
                    child: Image.network(
                      (widget.thumbnailLink),
                      height: 300.h,
                      width: 440.w,
                      fit: BoxFit.cover,
                    ))),
          ),
          const SizedBox(
            height: 20,
          ),
          Center(
            child: Column(
              children: [
                Text(
                  widget.headingName,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 22.sp,
                    color: Colors.white,
                  ),
                ),
                const SizedBox(
                  height: 10,
                ),
                Text(
                  widget.albumName,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 18.sp,
                    color: const Color(0xFFEAFF01),
                  ),
                ),
                const SizedBox(
                  height: 20,
                ),
                IconButton(
                  onPressed: () {
                    if (isPlaying) {
                      widget.audioPlayer.pause();
                    } else {
                      widget.audioPlayer.play();
                    }
                    setState(() {
                      isPlaying = !isPlaying;
                    });
                  },
                  iconSize: 75,
                  icon: widget.audioPlayer.playing
                      ? const Icon(
                          Icons.pause_circle,
                          color: Colors.white,
                        )
                      : const Icon(
                          Icons.play_circle,
                          color: Colors.white,
                        ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
Leave a Comment