import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:just_audio/just_audio.dart'; import 'package:traderscircuit/model/ContentBytesModel/content_bytes_categories_model.dart'; import 'package:traderscircuit/model/ContentBytesModel/content_bytes_model.dart'; import '../model/ContentBytesModel/previous_read_user_model.dart'; import '../view/Sidemenu/ContentByte/ContentBytes.dart'; class ContentBytesController extends GetxController { RxBool isLoading = true.obs; ContentBytesCategoriesModel contentBytesCategoriesModel = ContentBytesCategoriesModel(); ContentBytesModel contentBytesModel = ContentBytesModel(); ContentBytesModel moreVideoModel = ContentBytesModel(); ContentBytesModel moreAudioModel = ContentBytesModel(); ContentBytesModel moreReadsModel = ContentBytesModel(); PreviousReadOfUserModel previousReadOfUserModel = PreviousReadOfUserModel(); String filterId = "0"; RxBool isApiCalling = true.obs; RxBool isAudioSeekBarVisible = false.obs; RxInt indexForAudios = 0.obs; RxBool titlePlaying = false.obs; RxList? titlePlayingList = [].obs; bool isAudioInitialize = false; final progressNotifier = ValueNotifier( ProgressBarState( current: Duration.zero, buffered: Duration.zero, total: Duration.zero, ), ); final buttonNotifier = ValueNotifier(ButtonState.paused); late AudioPlayer audioPlayer; @override void dispose() { audioPlayer.stop(); super.dispose(); } void init(index, type) async { audioPlayer = AudioPlayer(); isAudioInitialize = true; indexForAudios.value = index; try { await audioPlayer.setUrl(type == "new" ? contentBytesModel.data!.newReleaseAudio![index].link! : contentBytesModel.data!.audio![index].link!); // await audioPlayer.setAsset(url); } on PlayerException catch (e) { print("Error code: ${e.code}"); // iOS/macOS: maps to NSError.localizedDescription // Android: maps to ExoPlaybackException.getMessage() // Web/Linux: a generic message // Windows: MediaPlayerError.message print("Error message: ${e.message}"); } //Catching errors during playback (e.g. lost network connection) audioPlayer.playbackEventStream.listen((event) {}, onError: (Object e, StackTrace st) { if (e is PlayerException) { print('Error code: ${e.code}'); print('Error message: ${e.message}'); } else { print('An error occurred: $e'); } }); audioPlayer.playerStateStream.listen((playerState) { final isPlaying = playerState.playing; final processingState = playerState.processingState; if (!isPlaying) { buttonNotifier.value = ButtonState.paused; } else if (processingState != ProcessingState.completed) { buttonNotifier.value = ButtonState.playing; } else { // _playNextTrack(type); // audioPlayer.seek(Duration.zero); // audioPlayer.pause(); } }); audioPlayer.positionStream.listen((position) { final oldState = progressNotifier.value; progressNotifier.value = ProgressBarState( current: position, buffered: oldState.buffered, total: oldState.total, ); }); audioPlayer.bufferedPositionStream.listen((bufferedPosition) { final oldState = progressNotifier.value; progressNotifier.value = ProgressBarState( current: oldState.current, buffered: bufferedPosition, total: oldState.total, ); }); audioPlayer.durationStream.listen((totalDuration) { final oldState = progressNotifier.value; progressNotifier.value = ProgressBarState( current: oldState.current, buffered: oldState.buffered, total: totalDuration ?? Duration.zero, ); }); } void _playNextTrack(type) { // Determine the index of the next track (you need to implement this logic) int nextIndex = getNextTrackIndex(); if (nextIndex != -1) { // Play the next track updateTitlePlaying(nextIndex); if (isPlaying()) { stop(); } init(nextIndex, type); play(nextIndex); } else { audioPlayer.seek(Duration.zero); audioPlayer.pause(); } } int getNextTrackIndex() { if (indexForAudios.value == contentBytesModel.data!.audio!.length - 1) { return -1; } else { return indexForAudios.value + 1; } // You need to implement this logic based on your playlist structure // For example, if you have a list of tracks, return the index of the next track // If there is no next track, return -1 // This logic depends on how you manage your playlist // Example: return currentTrackIndex + 1; } void play(index) { audioPlayer.play(); isAudioSeekBarVisible.value = false; isAudioSeekBarVisible.value = true; indexForAudios.value = index; } void pause() { audioPlayer.pause(); } void seek(Duration position) { audioPlayer.seek(position); } void stop() { audioPlayer.stop(); } bool isPlaying() { return audioPlayer.playing; } addTitles() { titlePlayingList = List.generate( contentBytesModel.data!.audio!.length, (index) => false, ).obs; } void updateTitlePlaying(int index) { // Check if the index is within valid range if (index >= 0 && index < titlePlayingList!.length) { // Set all values to false titlePlayingList!.assignAll(List.generate( titlePlayingList!.length, (index) => false, )); // Set the specified index to true titlePlayingList![index] = true; } } }