207 lines
6.1 KiB
Dart
207 lines
6.1 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:didvan/constants/assets.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/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:path/path.dart' as p;
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:universal_html/html.dart' as html;
|
|
|
|
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,
|
|
bool isBlob = false,
|
|
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.playing) {
|
|
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(
|
|
source,
|
|
tag: isVoiceMessage
|
|
? null
|
|
: {
|
|
"artist": 'استودیو دیدوان',
|
|
"title": currentPodcast?.title ?? '',
|
|
},
|
|
);
|
|
} else {
|
|
if (isBlob) {
|
|
audioPlayer.setAudioSource(AudioSource.uri(Uri.parse(audioSource)));
|
|
} 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 Future<String?> downloadFile(String url) async {
|
|
final basename = p.basename(url).split('?accessToken=').first;
|
|
Directory? dir;
|
|
try {
|
|
if (Platform.isIOS) {
|
|
dir = await getApplicationDocumentsDirectory(); // for iOS
|
|
} else {
|
|
dir = Directory('/storage/emulated/0/Download/'); // for android
|
|
if (!await dir.exists()) dir = (await getExternalStorageDirectory())!;
|
|
}
|
|
} catch (err) {
|
|
if (kDebugMode) {
|
|
print("Cannot get download folder path $err");
|
|
}
|
|
}
|
|
String path = "${dir?.path}$basename";
|
|
|
|
File file = File(path);
|
|
|
|
try {
|
|
final http.Response audioResponse = await http.get(Uri.parse(url));
|
|
final bytes = audioResponse.bodyBytes;
|
|
file.writeAsBytes(bytes);
|
|
} catch (e) {
|
|
if (kDebugMode) {
|
|
print("Exception$e");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
return path;
|
|
}
|
|
|
|
static String downloadFileFromWeb(String url) {
|
|
html.AnchorElement anchorElement = html.AnchorElement(href: url);
|
|
anchorElement.download = url;
|
|
anchorElement.click();
|
|
return anchorElement.pathname!;
|
|
}
|
|
|
|
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,
|
|
));
|
|
}
|
|
}
|