audio_picker_utils.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'dart:io';
  2. import 'package:file_picker/file_picker.dart';
  3. import 'package:photo_manager/photo_manager.dart';
  4. class AudioPickerUtils {
  5. AudioPickerUtils._();
  6. static const List<String> allowFileType = [
  7. 'wav',
  8. 'mp3',
  9. 'm4a',
  10. 'flv',
  11. 'mp4',
  12. 'wma',
  13. '3gp',
  14. 'amr',
  15. 'aac',
  16. 'ogg-opus',
  17. 'flac'
  18. ];
  19. //申请权限
  20. static Future<bool> requestPermissionExtend() async {
  21. final PermissionState ps = await PhotoManager.requestPermissionExtend(
  22. requestOption: const PermissionRequestOption(
  23. androidPermission: AndroidPermission(
  24. type: RequestType.audio,
  25. mediaLocation: false,
  26. )));
  27. return ps.hasAccess;
  28. }
  29. //是否有权限
  30. static Future<bool> hasPermission() async {
  31. final PermissionState ps = await PhotoManager.getPermissionState(
  32. requestOption: const PermissionRequestOption());
  33. return ps.hasAccess;
  34. }
  35. //获取AssetPathEntity
  36. static Future<List<AssetPathEntity>> getAssetPathList() async {
  37. final PMFilter filter = FilterOptionGroup(
  38. audioOption: const FilterOption(
  39. sizeConstraint: SizeConstraint(ignoreSize: true),
  40. ),
  41. );
  42. final List<AssetPathEntity> paths = await PhotoManager.getAssetPathList(
  43. onlyAll: true,
  44. type: RequestType.audio,
  45. filterOption: filter,
  46. );
  47. return paths;
  48. }
  49. //获取本地音频资源列表
  50. static Future<List<AssetEntity>> getAssetListPaged(
  51. AssetPathEntity path, int page, int size) async {
  52. final List<AssetEntity> entities = await path.getAssetListPaged(
  53. page: page,
  54. size: size,
  55. );
  56. return entities;
  57. }
  58. static Future<List<AssetEntity>> getAssetListRange(
  59. AssetPathEntity path, int offset, int limit) async {
  60. final List<AssetEntity> entities = await path.getAssetListRange(
  61. start: offset,
  62. end: offset + limit,
  63. );
  64. return entities;
  65. }
  66. //根据id获取AssetEntity
  67. static Future<AssetEntity?> getAssetEntity(String? id) async {
  68. if (id == null) {
  69. return null;
  70. }
  71. return await AssetEntity.fromId(id);
  72. }
  73. //根据id获取文件
  74. static Future<File?> getAssetFile(String? id) async {
  75. if (id == null) {
  76. return null;
  77. }
  78. AssetEntity? assetEntity = await AssetEntity.fromId(id);
  79. return await assetEntity?.file;
  80. }
  81. //通过平台文件管理器的方式选择单一文件
  82. static Future<File?> pickSingleFileByPlatform(
  83. {List<String> type = allowFileType}) async {
  84. FilePickerResult? result = await FilePicker.platform
  85. .pickFiles(type: FileType.custom, allowedExtensions: type);
  86. if (result != null) {
  87. String filePath = result.files.single.path!;
  88. File file = File(filePath);
  89. return file;
  90. }
  91. return null;
  92. }
  93. }