aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDocile-Alligator <25734209+Docile-Alligator@users.noreply.github.com>2023-08-13 02:52:20 +0000
committerDocile-Alligator <25734209+Docile-Alligator@users.noreply.github.com>2023-08-13 02:52:20 +0000
commitc5243ade5647b6a788b6c0a318863caad57a18f7 (patch)
tree0075c2963a7d92b53b00d498c1c7bc17596aaf90
parentd0a9d9af9a46477a9bc1ff36af11278fcba06aa5 (diff)
downloadinfinity-for-reddit-c5243ade5647b6a788b6c0a318863caad57a18f7.tar
infinity-for-reddit-c5243ade5647b6a788b6c0a318863caad57a18f7.tar.gz
infinity-for-reddit-c5243ade5647b6a788b6c0a318863caad57a18f7.tar.bz2
infinity-for-reddit-c5243ade5647b6a788b6c0a318863caad57a18f7.tar.lz
infinity-for-reddit-c5243ade5647b6a788b6c0a318863caad57a18f7.tar.xz
infinity-for-reddit-c5243ade5647b6a788b6c0a318863caad57a18f7.tar.zst
infinity-for-reddit-c5243ade5647b6a788b6c0a318863caad57a18f7.zip
Fix Reddit video downloaded without sound.
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java35
1 files changed, 28 insertions, 7 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java
index 43565dd7..0fbaf4c1 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java
@@ -26,6 +26,8 @@ import android.os.Message;
import android.os.Process;
import android.provider.MediaStore;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
import androidx.core.app.NotificationChannelCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
@@ -83,6 +85,7 @@ public class DownloadRedditVideoService extends Service {
private ServiceHandler serviceHandler;
private NotificationManagerCompat notificationManager;
private NotificationCompat.Builder builder;
+ private final String[] possibleAudioUrlSuffices = new String[]{"/DASH_AUDIO_128.mp4", "/DASH_audio.mp4", "/DASH_audio", "/audio.mp4", "/audio"};
public DownloadRedditVideoService() {
}
@@ -95,7 +98,9 @@ public class DownloadRedditVideoService extends Service {
public void handleMessage(Message msg) {
Bundle intent = msg.getData();
String videoUrl = intent.getString(EXTRA_VIDEO_URL);
- String audioUrl = Build.VERSION.SDK_INT > Build.VERSION_CODES.N ? videoUrl.substring(0, videoUrl.lastIndexOf('/')) + "/DASH_audio.mp4" : null;
+
+ String audioUrlPrefix = Build.VERSION.SDK_INT > Build.VERSION_CODES.N ? videoUrl.substring(0, videoUrl.lastIndexOf('/')) : null;
+
String subredditName = intent.getString(EXTRA_SUBREDDIT);
String fileNameWithoutExtension = subredditName + "-" + intent.getString(EXTRA_POST_ID);
boolean isNsfw = intent.getBoolean(EXTRA_IS_NSFW, false);
@@ -129,7 +134,7 @@ public class DownloadRedditVideoService extends Service {
retrofit = retrofit.newBuilder().client(client).build();
- DownloadFile downloadFile = retrofit.create(DownloadFile.class);
+ DownloadFile downloadFileRetrofit = retrofit.create(DownloadFile.class);
boolean separateDownloadFolder = sharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_FOLDER_FOR_EACH_SUBREDDIT, false);
@@ -138,7 +143,7 @@ public class DownloadRedditVideoService extends Service {
String destinationFileName = fileNameWithoutExtension + ".mp4";
try {
- Response<ResponseBody> videoResponse = downloadFile.downloadFile(videoUrl).execute();
+ Response<ResponseBody> videoResponse = downloadFileRetrofit.downloadFile(videoUrl).execute();
if (videoResponse.isSuccessful() && videoResponse.body() != null) {
String externalCacheDirectoryPath = externalCacheDirectory.getAbsolutePath() + "/";
String destinationFileDirectory;
@@ -218,13 +223,13 @@ public class DownloadRedditVideoService extends Service {
return;
}
- if (audioUrl != null) {
- Response<ResponseBody> audioResponse = downloadFile.downloadFile(audioUrl).execute();
+ if (audioUrlPrefix != null) {
+ ResponseBody audioResponse = getAudioResponse(downloadFileRetrofit, audioUrlPrefix, 0);
String outputFilePath = externalCacheDirectoryPath + fileNameWithoutExtension + ".mp4";
- if (audioResponse.isSuccessful() && audioResponse.body() != null) {
+ if (audioResponse != null) {
String audioFilePath = externalCacheDirectoryPath + fileNameWithoutExtension + "-cache.mp3";
- String savedAudioFilePath = writeResponseBodyToDisk(audioResponse.body(), audioFilePath);
+ String savedAudioFilePath = writeResponseBodyToDisk(audioResponse, audioFilePath);
if (savedAudioFilePath == null) {
downloadFinished(null, ERROR_AUDIO_FILE_CANNOT_SAVE, randomNotificationIdOffset);
return;
@@ -298,6 +303,22 @@ public class DownloadRedditVideoService extends Service {
}
}
+ @Nullable
+ private ResponseBody getAudioResponse(DownloadFile downloadFileRetrofit, @NonNull String audioUrlPrefix, int audioSuffixIndex) throws IOException {
+ if (audioSuffixIndex >= possibleAudioUrlSuffices.length) {
+ return null;
+ }
+
+ String audioUrl = audioUrlPrefix + possibleAudioUrlSuffices[audioSuffixIndex];
+ Response<ResponseBody> audioResponse = downloadFileRetrofit.downloadFile(audioUrl).execute();
+ ResponseBody responseBody = audioResponse.body();
+ if (audioResponse.isSuccessful() && responseBody != null) {
+ return responseBody;
+ }
+
+ return getAudioResponse(downloadFileRetrofit, audioUrlPrefix, audioSuffixIndex + 1);
+ }
+
private String writeResponseBodyToDisk(ResponseBody body, String filePath) {
try {
File file = new File(filePath);