159 lines
4.8 KiB
Dart
159 lines
4.8 KiB
Dart
import 'package:didvan/constants/assets.dart';
|
|
import 'package:didvan/main.dart';
|
|
import 'package:didvan/models/requests/studio.dart';
|
|
import 'package:didvan/models/studio_details_data.dart';
|
|
import 'package:didvan/models/view/action_sheet_data.dart';
|
|
import 'package:didvan/providers/media.dart';
|
|
import 'package:didvan/services/app_initalizer.dart';
|
|
import 'package:didvan/services/network/request.dart';
|
|
import 'package:didvan/services/network/request_helper.dart';
|
|
import 'package:didvan/services/storage/storage.dart';
|
|
import 'package:didvan/utils/action_sheet.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:just_audio/just_audio.dart';
|
|
import 'package:file_picker/file_picker.dart';
|
|
|
|
class MediaService {
|
|
static final audioPlayer = AudioPlayer();
|
|
static String? audioPlayerTag;
|
|
static StudioDetailsData? currentPodcast;
|
|
static StudioRequestArgs? podcastPlaylistArgs;
|
|
|
|
static Duration? get duration => audioPlayer.duration;
|
|
|
|
static Future<void> handleAudioPlayback({
|
|
required dynamic audioSource,
|
|
required int id,
|
|
bool isNetworkAudio = true,
|
|
bool isVoiceMessage = true,
|
|
void Function(bool isNext)? onTrackChanged,
|
|
}) async {
|
|
try {
|
|
String tag;
|
|
tag =
|
|
'${currentPodcast?.description == 'radar' ? 'radar' : isVoiceMessage ? 'message' : 'podcast'}-$id';
|
|
if (!isVoiceMessage && MediaProvider.downloadedItemIds.contains(id)) {
|
|
audioSource = '${StorageService.appDocsDir}/podcasts/podcast-$id.mp3';
|
|
isNetworkAudio = false;
|
|
}
|
|
if (audioPlayerTag == tag) {
|
|
if (audioPlayer.playerState ==
|
|
PlayerState(true, ProcessingState.ready)) {
|
|
await audioPlayer.pause();
|
|
} else {
|
|
await audioPlayer.play();
|
|
}
|
|
|
|
return;
|
|
}
|
|
// await audioPlayer.stop();
|
|
audioPlayerTag = tag;
|
|
String source;
|
|
if (isNetworkAudio) {
|
|
if (isVoiceMessage) {
|
|
source =
|
|
'${RequestHelper.baseUrl + audioSource}?accessToken=${RequestService.token}';
|
|
} else {
|
|
source = audioSource;
|
|
}
|
|
audioPlayer.setUrl(
|
|
kIsWeb ? source.replaceAll('%3A', ':') : source,
|
|
tag: isVoiceMessage
|
|
? null
|
|
: {
|
|
"artist": 'استودیو دیدوان',
|
|
"title": currentPodcast?.title ?? '',
|
|
},
|
|
);
|
|
} else {
|
|
audioPlayer.setFilePath(
|
|
audioSource,
|
|
tag: isVoiceMessage
|
|
? null
|
|
: {
|
|
"artist": 'استودیو دیدوان',
|
|
"title": currentPodcast?.title ?? '',
|
|
},
|
|
);
|
|
}
|
|
await audioPlayer.play();
|
|
// await audioPlayer.open(
|
|
// audio,
|
|
// showNotification: !isVoiceMessage,
|
|
// notificationSettings: NotificationSettings(
|
|
// customStopAction: (_) => resetAudioPlayer(),
|
|
// customNextAction: (_) => onTrackChanged?.call(true),
|
|
// customPrevAction: (_) => onTrackChanged?.call(false),
|
|
// ),
|
|
// );
|
|
} catch (e) {
|
|
// resetAudioPlayer();
|
|
// rethrow;
|
|
}
|
|
}
|
|
|
|
static Future<void> resetAudioPlayer() async {
|
|
audioPlayerTag = null;
|
|
currentPodcast = null;
|
|
podcastPlaylistArgs = null;
|
|
await MediaService.audioPlayer.stop();
|
|
}
|
|
|
|
static Future<XFile?> pickImage({required ImageSource source}) async {
|
|
try {
|
|
final imagePicker = ImagePicker();
|
|
final XFile? pickedFile = await imagePicker.pickImage(source: source);
|
|
return pickedFile;
|
|
} catch (e) {
|
|
e.printError(info: 'Pick Image Fail');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static Future<FilePickerResult?> pickPdfFile() async {
|
|
try {
|
|
final FilePickerResult? result = await FilePicker.platform.pickFiles(
|
|
type: FileType.custom,
|
|
allowedExtensions: ['pdf'],
|
|
allowMultiple: false,
|
|
);
|
|
return result;
|
|
} catch (e) {
|
|
e.printError(info: 'Pick PDF Fail');
|
|
// navigatorKey.currentState!.pop();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static Future<FilePickerResult?> pickAudioFile() async {
|
|
try {
|
|
return await FilePicker.platform.pickFiles(
|
|
type: FileType.audio,
|
|
allowMultiple: false,
|
|
);
|
|
} catch (e) {
|
|
e.printError(info: 'Pick Audio Fail');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static onLoadingPickFile(BuildContext context) {
|
|
ActionSheetUtils(context).openDialog(
|
|
barrierDismissible: false,
|
|
data: ActionSheetData(
|
|
content: Center(
|
|
child: Image.asset(
|
|
Assets.loadingAnimation,
|
|
width: 60,
|
|
height: 60,
|
|
),
|
|
),
|
|
hasConfirmButton: false,
|
|
hasDismissButton: false,
|
|
));
|
|
}
|
|
}
|