audio_picker_handler.dart 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. return ps.hasAccess;
  9. }
  10. //是否有权限
  11. static Future<bool> hasPermission() async {
  12. final PermissionState ps = await PhotoManager.getPermissionState(
  13. requestOption: const PermissionRequestOption());
  14. return ps.hasAccess;
  15. }
  16. //获取AssetPathEntity
  17. static Future<List<AssetPathEntity>> getAssetPathList() async {
  18. final PMFilter filter = FilterOptionGroup(
  19. audioOption: const FilterOption(
  20. sizeConstraint: SizeConstraint(ignoreSize: true),
  21. ),
  22. );
  23. final List<AssetPathEntity> paths = await PhotoManager.getAssetPathList(
  24. onlyAll: true,
  25. type: RequestType.audio,
  26. filterOption: filter,
  27. );
  28. return paths;
  29. }
  30. //获取本地音频资源列表
  31. static Future<List<AssetEntity>> getAssetList(AssetPathEntity path, int page,
  32. {int size = 100}) async {
  33. final List<AssetEntity> entities = await path.getAssetListPaged(
  34. page: page,
  35. size: size,
  36. );
  37. return entities;
  38. }
  39. //根据id获取AssetEntity
  40. static Future<AssetEntity?> getAssetEntity(String? id) async {
  41. if (id == null) {
  42. return null;
  43. }
  44. return await AssetEntity.fromId(id);
  45. }
  46. //根据id获取文件
  47. static Future<File?> getAssetFile(String? id) async {
  48. if (id == null) {
  49. return null;
  50. }
  51. AssetEntity? assetEntity = await AssetEntity.fromId(id);
  52. return await assetEntity?.file;
  53. }
  54. }