45 lines
1.3 KiB
Dart
45 lines
1.3 KiB
Dart
import 'package:didvan/services/network/request.dart';
|
|
import 'package:didvan/services/network/request_helper.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:just_audio/just_audio.dart';
|
|
|
|
class MediaService {
|
|
static final AudioPlayer audioPlayer = AudioPlayer();
|
|
static dynamic lastAudioPath;
|
|
|
|
static Future<void> handleAudioPlayback(
|
|
{required dynamic audioSource, required bool isNetworkAudio}) async {
|
|
if (lastAudioPath == audioSource) {
|
|
if (audioPlayer.playing) {
|
|
await audioPlayer.pause();
|
|
} else {
|
|
await audioPlayer.play();
|
|
}
|
|
} else {
|
|
lastAudioPath = audioSource;
|
|
if (isNetworkAudio) {
|
|
await audioPlayer.setUrl(
|
|
RequestHelper.baseUrl +
|
|
audioSource +
|
|
'?accessToken=${RequestService.token}',
|
|
);
|
|
} else {
|
|
if (kIsWeb) {
|
|
await audioPlayer
|
|
.setUrl(audioSource!.uri.path.replaceAll('%3A', ':'));
|
|
} else {
|
|
await audioPlayer.setFilePath(audioSource.path);
|
|
}
|
|
}
|
|
await audioPlayer.play();
|
|
}
|
|
}
|
|
|
|
static Future<XFile?> pickImage({required ImageSource source}) async {
|
|
final imagePicker = ImagePicker();
|
|
final XFile? pickedFile = await imagePicker.pickImage(source: source);
|
|
return pickedFile;
|
|
}
|
|
}
|