audio_picker_handler.dart 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import 'dart:io';
  2. import 'package:photo_manager/photo_manager.dart';
  3. class AudioPickerHandler {
  4. AudioPickerHandler._();
  5. //申请权限
  6. static Future<bool> requestPermissionExtend() async {
  7. final PermissionState ps = await PhotoManager.requestPermissionExtend(
  8. requestOption: PermissionRequestOption(
  9. androidPermission: AndroidPermission(
  10. type: RequestType(RequestType.audio.value | RequestType.video.value),
  11. mediaLocation: false,
  12. )));
  13. return ps.hasAccess;
  14. }
  15. //是否有权限
  16. static Future<bool> hasPermission() async {
  17. final PermissionState ps = await PhotoManager.getPermissionState(
  18. requestOption: const PermissionRequestOption());
  19. return ps.hasAccess;
  20. }
  21. //获取AssetPathEntity
  22. static Future<List<AssetPathEntity>> getAssetPathList() async {
  23. final PMFilter filter = FilterOptionGroup(
  24. audioOption: const FilterOption(
  25. sizeConstraint: SizeConstraint(ignoreSize: true),
  26. ),
  27. );
  28. final List<AssetPathEntity> paths = await PhotoManager.getAssetPathList(
  29. onlyAll: true,
  30. type: RequestType(RequestType.audio.value | RequestType.video.value),
  31. filterOption: filter,
  32. );
  33. return paths;
  34. }
  35. //获取本地音频资源列表
  36. static Future<List<AssetEntity>> getAssetList(AssetPathEntity path, int page,
  37. {int size = 300}) async {
  38. final List<AssetEntity> entities = await path.getAssetListPaged(
  39. page: page,
  40. size: size,
  41. );
  42. return entities;
  43. }
  44. //根据id获取AssetEntity
  45. static Future<AssetEntity?> getAssetEntity(String? id) async {
  46. if (id == null) {
  47. return null;
  48. }
  49. return await AssetEntity.fromId(id);
  50. }
  51. //根据id获取文件
  52. static Future<File?> getAssetFile(String? id) async {
  53. if (id == null) {
  54. return null;
  55. }
  56. AssetEntity? assetEntity = await AssetEntity.fromId(id);
  57. return await assetEntity?.file;
  58. }
  59. }