I needed subsonic for Android to download a higher bitrate file if better internet connection is available.
For example:
I haven download a music file via mobile network (for example 80k). Then I connect to WiFi and I want to listen the same track but in higher bitrate (since WiFi is available and in settings I have chosen higher bitrate for WiFi connection).
I have written a patch to do so. Basically it adds bitrate to filenameand If you change settings to higher bitrate or if connect to a network defined as higher bitrate, the cached file will be removed and a new one will be downloaded.
If you are on lower bitrate connection it will play highest bitrate available in cache (there should usually be only one same track in cache).
I hope you will considered applying the patch in newer version of Subsonic for Android.
- Code: Select all
@@ -22,2 +22,3 @@ import java.io.File;
import java.io.FileOutputStream;
+import java.io.FilenameFilter;
import java.io.IOException;
@@ -25,2 +26,4 @@ import java.io.InputStream;
import java.io.OutputStream;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
@@ -59,2 +62,3 @@ public class DownloadFile {
private int bitRate;
+ private int checkIfHigherBitrateIsAvailable;
@@ -67,4 +71,15 @@ public class DownloadFile {
"." + bitRate + ".partial." + FileUtil.getExtension(saveFile.getName()));
- completeFile = new File(saveFile.getParent(), FileUtil.getBaseName(saveFile.getName()) +
- ".complete." + FileUtil.getExtension(saveFile.getName()));
+ int availableBitrate = checkIfHigherBitrateIsAvailable();
+ if(availableBitrate >= bitRate || availableBitrate == 0)
+ {
+ bitRate = availableBitrate;
+ }
+ else if(availableBitrate != -1)
+ {
+ Util.delete(new File(saveFile.getParent(), FileUtil.getBaseName(saveFile.getName()) + "." + availableBitrate +
+ ".complete." + FileUtil.getExtension(saveFile.getName())));
+ }
+ completeFile = new File(saveFile.getParent(), FileUtil.getBaseName(saveFile.getName()) + "." + bitRate +
+ ".complete." + FileUtil.getExtension(saveFile.getName()));
+
mediaStoreService = new MediaStoreService(context);
@@ -76,2 +91,43 @@ public class DownloadFile {
+ private int checkIfHigherBitrateIsAvailable()
+ {
+ int bestBitrate=-1;
+ if(saveFile.getParentFile().exists())
+ {
+ File filePath = new File( saveFile.getParent() );
+ String[] list;
+ list = filePath.list(new DirFilter(FileUtil.getBaseName(saveFile.getName()) + ".+[0-9]*\\.complete\\." + FileUtil.getExtension(saveFile.getName())));
+ Pattern bitRatePattern = Pattern.compile("([0-9]*)\\.complete\\." + FileUtil.getExtension(saveFile.getName()));
+ Matcher m;
+ int curBitrate;
+ for (int i = 0; i < list.length; i++)
+ {
+ m = bitRatePattern.matcher(list[i]);
+ m.find();
+ curBitrate = Integer.parseInt(m.group(1));
+ if(curBitrate >bestBitrate)
+ bestBitrate = curBitrate;
+ else if(curBitrate == 0)
+ return 0;
+ }
+ }
+ return bestBitrate;
+ }
+
+ class DirFilter implements FilenameFilter {
+ private Pattern pattern;
+
+ public DirFilter(String reg) {
+ pattern = Pattern.compile(reg);
+ }
+
+ @Override
+ public boolean accept(File dir, String name) {
+ // Strip path information, search for regex:
+ return pattern.matcher(new File(name).getName()).matches();
+ }
+ }
+
+
+
/**