93 lines
2.9 KiB
Dart
93 lines
2.9 KiB
Dart
import 'package:didvan/models/requests/studio.dart';
|
|
import 'package:didvan/models/studio_details_data.dart';
|
|
import 'package:didvan/providers/media.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:flutter/foundation.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:assets_audio_player/assets_audio_player.dart';
|
|
|
|
class MediaService {
|
|
static final audioPlayer = AssetsAudioPlayer();
|
|
static String? audioPlayerTag;
|
|
static StudioDetailsData? currentPodcast;
|
|
static StudioRequestArgs? podcastPlaylistArgs;
|
|
|
|
static Duration? get duration =>
|
|
audioPlayer.current.valueOrNull?.audio.duration;
|
|
|
|
static Future<void> handleAudioPlayback({
|
|
required dynamic audioSource,
|
|
required int id,
|
|
bool isNetworkAudio = true,
|
|
bool isVoiceMessage = true,
|
|
void Function(bool isNext)? onTrackChanged,
|
|
}) async {
|
|
String tag;
|
|
tag = '${isVoiceMessage ? 'message' : 'podcast'}-$id';
|
|
if (!isVoiceMessage && MediaProvider.downloadedItemIds.contains(id)) {
|
|
audioSource = StorageService.appDocsDir + '/podcasts/podcast-$id.mp3';
|
|
isNetworkAudio = false;
|
|
}
|
|
if (audioPlayerTag == tag) {
|
|
await audioPlayer.playOrPause();
|
|
return;
|
|
}
|
|
await audioPlayer.stop();
|
|
audioPlayerTag = tag;
|
|
Audio audio;
|
|
String source;
|
|
if (isNetworkAudio) {
|
|
if (isVoiceMessage) {
|
|
source = RequestHelper.baseUrl +
|
|
audioSource +
|
|
'?accessToken=${RequestService.token}';
|
|
} else {
|
|
source = audioSource;
|
|
}
|
|
audio = Audio.network(
|
|
kIsWeb ? source.replaceAll('%3A', ':') : source,
|
|
metas: isVoiceMessage
|
|
? null
|
|
: Metas(
|
|
artist: 'استودیو دیدوان',
|
|
title: currentPodcast!.title,
|
|
),
|
|
);
|
|
} else {
|
|
audio = Audio.file(
|
|
audioSource,
|
|
metas: isVoiceMessage
|
|
? null
|
|
: Metas(
|
|
artist: 'استودیو دیدوان',
|
|
title: currentPodcast!.title,
|
|
),
|
|
);
|
|
}
|
|
await audioPlayer.open(
|
|
audio,
|
|
showNotification: !isVoiceMessage,
|
|
notificationSettings: NotificationSettings(
|
|
customStopAction: (_) => resetAudioPlayer(),
|
|
customNextAction: (_) => onTrackChanged?.call(true),
|
|
customPrevAction: (_) => onTrackChanged?.call(false),
|
|
),
|
|
);
|
|
}
|
|
|
|
static Future<void> resetAudioPlayer() async {
|
|
audioPlayerTag = null;
|
|
currentPodcast = null;
|
|
podcastPlaylistArgs = null;
|
|
MediaService.audioPlayer.stop();
|
|
}
|
|
|
|
static Future<XFile?> pickImage({required ImageSource source}) async {
|
|
final imagePicker = ImagePicker();
|
|
final XFile? pickedFile = await imagePicker.pickImage(source: source);
|
|
return pickedFile;
|
|
}
|
|
}
|