Untitled
unknown
plain_text
a month ago
7.1 kB
4
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; bool isErrorShown = 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) { setState(() { isBuffering = true; }); _showBufferingDialog(); } } else if (event.processingState == ProcessingState.idle) { // if (!isErrorShown) { // _showErrorDialog(); // } } else { if (isErrorShown) { Navigator.of(context).pop(); // Close the error dialog setState(() { isErrorShown = false; }); } if (isBuffering) { Navigator.of(context).pop(); // Close the buffering dialog setState(() { isBuffering = false; }); } } }); WidgetsBinding.instance.addPostFrameCallback((_) { playSongs(); }); } void _showErrorDialog() { setState(() { isErrorShown = true; isBuffering = false; }); showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return AlertDialog( content: SizedBox( // height: MediaQuery.of(context).size.height * 0.5, child: Column( mainAxisAlignment: MainAxisAlignment.center, mainAxisSize: MainAxisSize.min, children: [ const Text("Channel is currently Offline"), const SizedBox(width: 20), IconButton( onPressed: () { setState(() { isErrorShown = false; }); Navigator.of(context).popUntil((route) => route.isFirst); }, icon: const Icon(Icons.arrow_back)) ], ), ), ); }, ); } 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 { await widget.audioPlayer.setAudioSource( AudioSource.uri( Uri.parse(widget.audioLink), tag: newMethod(), ), ); widget.audioPlayer.play(); widget.audioPlayer.playingStream.listen((playerState) { if (mounted) { setState(() { isPlaying = playerState; }); } }); isPlaying = true; } catch (e) { log("ERROR PARSING DATA: $e"); if (mounted && !isErrorShown) { _showErrorDialog(); } } } 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