Skip to content

Commit

Permalink
[*] fixed recover android cache song path
Browse files Browse the repository at this point in the history
  • Loading branch information
heng30 committed May 30, 2024
1 parent 01fd31b commit b7395ff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lib/models/find_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,23 @@ class FindController extends GetxController {
infoList.addAll(l);
}

Future<String> getDownloadsDirectoryWithoutCreate() async {
try {
if (Platform.isAndroid) {
final pname = (await PackageInfo.fromPlatform()).packageName;
return "/storage/emulated/0/$pname/music";
} else {
final tmpDir = await getDownloadsDirectory() ??
await getApplicationCacheDirectory();

return "${tmpDir.path}/music";
}
} catch (e) {
log.d(e.toString());
return "";
}
}

Future<void> createDownloadDir() async {
try {
if (Platform.isAndroid) {
Expand Down
18 changes: 17 additions & 1 deletion lib/models/player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import './audio_session_controller.dart';
import './setting_controller.dart';
import '../models/player_tile_controller.dart';
import "../models/lyric_controller.dart";
import "../models/find_controller.dart";

enum PlayModel {
loop,
Expand Down Expand Up @@ -71,7 +72,22 @@ class PlayerController extends GetxController {
if (song.audioLocation == AudioLocation.asset) {
src = AssetSource(song.audioPath);
} else if (song.audioLocation == AudioLocation.local) {
src = DeviceFileSource(song.audioPath);
log.d(song.audioPath);
if (await File(song.audioPath).exists()) {
src = DeviceFileSource(song.audioPath);
} else {
// on android, file picker would return the cache file path,
// so after recovering the database, the `audioPath` is not correct.
final findController = Get.find<FindController>();
final name = basename(song.audioPath);
final downloadDir =
await findController.getDownloadsDirectoryWithoutCreate();
final maybeDownloadFile = File("$downloadDir/$name");
// log.d(maybeDownloadFile.path);
if (await maybeDownloadFile.exists()) {
src = DeviceFileSource(maybeDownloadFile.path);
}
}
} else if (song.audioLocation == AudioLocation.memory) {
final file = File(song.audioPath);
src = BytesSource(await file.readAsBytes());
Expand Down

0 comments on commit b7395ff

Please sign in to comment.