Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
5.8 kB
3
Indexable
Never
// ignore_for_file: avoid_print

import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gurbani_app/widgets/constraints.dart';
import 'package:gurbani_app/widgets/custom_text.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({
    Key? key,
    required this.audioLink,
    required this.thumbnailLink,
    required this.headingName,
    required this.heroTag,
    required this.audioPlayer,
    required this.albumName,
  }) : super(key: key);

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

class _JustMusicPlayState extends State<JustMusicPlay> {
  bool isPlaying = false;

  late PlayerState _playerState;

  @override
  void initState() {
    super.initState();
    _playerState = widget.audioPlayer.playerState;

    WidgetsBinding.instance.addPostFrameCallback((_) {
      playSongs();
    });

  }

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

      if (widget.audioPlayer.playing) {
        // this was the issue for notification not changing
        setState(() {
          widget.audioPlayer.stop();
        });
      }

      widget.audioPlayer.play();

      widget.audioPlayer.playingStream.listen((playerState) {
        if (mounted) {
          setState(() {
            // Update isPlaying based on playerState
            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: CustomText(
          title: widget.headingName,
          fontSize: 18.sp,
          fontWeight: FontWeight.w600,
          color: Colors.white,
        ),


        // 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);
            // navigatorKey.currentState!.pop();
          },
        ),
        actions: [
          IconButton(
            icon: const Icon(Icons.share),
            onPressed: () {},
          ),
        ],
      ),
      body: Column(
        children: [
          //image of channel
          Padding(
            padding: const EdgeInsets.only(top: 20.0, left: 20.0, right: 20.0),
            child: ClipRRect(
                borderRadius: const BorderRadius.all(Radius.circular(10.0)),
                //image code
                child: Hero(
                    tag: widget.heroTag,
                    child: Image.network(
                      (widget.thumbnailLink),
                      height: 300.h,
                      width: 440.w,
                      fit: BoxFit.cover,
                    ))),
          ),

          const SizedBox(
            height: 20,
          ),

          //player icons with func
          Center(
            child: Column(
              children: [
                Text(
                  widget.headingName,
                  style:  TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 22.sp,
                    color: whiteColor,
                  ),
                ),
                const SizedBox(
                  height: 10,
                ),
                Text(
                  widget.albumName,
                  style:  TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 18.sp,
                    color: 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: whiteColor,
                          // size: 55,
                        )
                      : const Icon(
                          Icons.play_circle,
                          color: whiteColor,
                        ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
Leave a Comment