Skip to content

Commit

Permalink
feat: 支持mp3格式保存到本地
Browse files Browse the repository at this point in the history
  • Loading branch information
lyy committed Oct 18, 2024
1 parent 74c0398 commit e551bb1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.0.1

- [feat] 现在返回的是纯音频信息了
- [feat] 新增搜索建议
- [feat] 支持将单首歌曲添加到播放列表
- [feat] 支持下载为 mp3 格式文件并保存到用户本地目录

## 0.0.5

- [feat] 完善文档
Expand Down
7 changes: 1 addition & 6 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,5 @@
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<!-- if androidExistNotSave = true -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
</manifest>
36 changes: 16 additions & 20 deletions lib/modules/download/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DownloadModel extends ChangeNotifier {
// 下载
void download(List<MusicItem> musics) async {
for (var item in musics) {
final name = '${item.name}.mp4';
final name = '${item.name}.mp3';
String resultPath = "";
try {
if (Platform.isAndroid) {
Expand Down Expand Up @@ -80,25 +80,32 @@ Future<String> _downloadForAndroid(
String name,
) async {
// 判断是否授权
var status = await Permission.phone.status;
var status = await Permission.manageExternalStorage.status;
if (!status.isGranted) {
var per = await Permission.photos.request();
var per = await Permission.manageExternalStorage.request();
if (!per.isGranted) {
throw '未授权';
}
}
// 文件保存路径
String savePath = "";

// 获取目录下的文件列表
final downloadDir = path.join('/storage/emulated/0/Download/哔哔音乐');
// 判断目录是否存在
final dir = Directory(downloadDir);
if (!dir.existsSync()) {
dir.createSync(recursive: true);
}

// 查询缓存文件
final key = music2cacheKey(music);
final cacheFile = await audioCacheManage.getFileFromCache(key);
// 保存路径
String savePath = path.join(downloadDir, name);
if (cacheFile?.file != null) {
savePath = cacheFile!.file.path;
// 文件保存
cacheFile!.file.copy(savePath);
} else {
// 没有缓存文件,从网络下载
var appDocDir = await getTemporaryDirectory();
savePath = path.join('${appDocDir.path}/$name');
final m = await service.getMusicUrl(music.id);
await dio.download(
m.url,
Expand All @@ -107,16 +114,5 @@ Future<String> _downloadForAndroid(
);
}

// 保存到相册
await SaverGallery.saveFile(
file: savePath,
androidExistNotSave: true,
name: name,
androidRelativePath: "Movies",
);
//根据文件路径删除临时文件
if (cacheFile?.file == null) {
await File(savePath).delete();
}
return "";
return savePath;
}
5 changes: 4 additions & 1 deletion lib/modules/search/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class _SearchViewState extends State<SearchView> {
}
});
getSearchHistory();
// 输入框选中
_focusNode.requestFocus();
}

getSearchHistory() async {
Expand Down Expand Up @@ -150,6 +152,7 @@ class _SearchViewState extends State<SearchView> {
@override
void dispose() {
_keywordController.dispose();
_focusNode.unfocus();
_focusNode.dispose();
super.dispose();
}
Expand Down Expand Up @@ -220,6 +223,7 @@ class _SearchViewState extends State<SearchView> {
onTap: () {
_keywordController.text = item.value;
_searchHandler(true);
_focusNode.unfocus();
},
);
}).toList(),
Expand Down Expand Up @@ -357,7 +361,6 @@ class _SearchFormState extends State<_SearchForm> {
child: TextField(
controller: widget.keywordController,
onSubmitted: (value) => widget.onSearch(),
autofocus: true,
focusNode: widget.focusNode,
decoration: const InputDecoration(
border: InputBorder.none,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: bbmusic
description: '哔哔音乐,听歌自由'
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+2
version: 1.0.1+3

environment:
sdk: '>=3.3.1 <4.0.0'
Expand Down

0 comments on commit e551bb1

Please sign in to comment.