audio_picker_utils.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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: PermissionRequestOption(
  33. androidPermission: AndroidPermission(
  34. type: RequestType.video | RequestType.audio,
  35. mediaLocation: false,
  36. )));
  37. return ps.hasAccess;
  38. }
  39. //获取AssetPathEntity
  40. static Future<List<AssetPathEntity>> getAssetPathList() async {
  41. final PMFilter filter = FilterOptionGroup(
  42. audioOption: const FilterOption(
  43. sizeConstraint: SizeConstraint(ignoreSize: true),
  44. ),
  45. );
  46. final List<AssetPathEntity> paths = await PhotoManager.getAssetPathList(
  47. onlyAll: true,
  48. type: RequestType.audio,
  49. filterOption: filter,
  50. );
  51. return paths;
  52. }
  53. //获取本地音频资源列表
  54. static Future<List<AssetEntity>> getAssetListPaged(
  55. AssetPathEntity path, int page, int size) async {
  56. final List<AssetEntity> entities = await path.getAssetListPaged(
  57. page: page,
  58. size: size,
  59. );
  60. return entities;
  61. }
  62. static Future<List<AssetEntity>> getAssetListRange(
  63. AssetPathEntity path, int offset, int limit) async {
  64. final List<AssetEntity> entities = await path.getAssetListRange(
  65. start: offset,
  66. end: offset + limit,
  67. );
  68. return entities;
  69. }
  70. //根据id获取AssetEntity
  71. static Future<AssetEntity?> getAssetEntity(String? id) async {
  72. if (id == null) {
  73. return null;
  74. }
  75. return await AssetEntity.fromId(id);
  76. }
  77. //根据id获取文件
  78. static Future<File?> getAssetFile(String? id) async {
  79. if (id == null) {
  80. return null;
  81. }
  82. AssetEntity? assetEntity = await AssetEntity.fromId(id);
  83. return await assetEntity?.file;
  84. }
  85. //通过平台文件管理器的方式选择单一文件
  86. static Future<File?> pickSingleFileByPlatform(
  87. {List<String> type = allowFileType}) async {
  88. FilePickerResult? result = await FilePicker.platform
  89. .pickFiles(type: FileType.custom, allowedExtensions: type);
  90. if (result != null) {
  91. String filePath = result.files.single.path!;
  92. File file = File(filePath);
  93. return file;
  94. }
  95. return null;
  96. }
  97. }