-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load predictable subtitle files over network
Only .srt for now, but in all possible language variants. Resolves #173
- Loading branch information
Showing
5 changed files
with
211 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
app/src/main/java/com/brouken/player/SubtitleFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package com.brouken.player; | ||
|
||
import android.content.res.Resources; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
import android.os.LocaleList; | ||
import android.widget.Toast; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.google.android.exoplayer2.MediaItem; | ||
import com.google.android.exoplayer2.util.Util; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
import okhttp3.Call; | ||
import okhttp3.Callback; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
public class SubtitleFinder { | ||
|
||
private PlayerActivity activity; | ||
private Uri baseUri; | ||
private String path; | ||
private CountDownLatch countDownLatch; | ||
private Uri subtitleUri; | ||
private final LinkedHashMap<Uri, Boolean> urls; | ||
|
||
public SubtitleFinder(PlayerActivity activity, Uri uri) { | ||
this.activity = activity; | ||
path = uri.getPath(); | ||
path = path.substring(0, path.lastIndexOf('.')); | ||
baseUri = uri; | ||
urls = new LinkedHashMap<>(); | ||
} | ||
|
||
private void addLocale(Locale locale) { | ||
final String lang = locale.getISO3Language(); | ||
urls.put(buildUri(lang + ".srt"), false); | ||
urls.put(buildUri(Util.normalizeLanguageCode(lang) + ".srt"), false); | ||
} | ||
|
||
private Uri buildUri(String suffix) { | ||
final String newPath = path + "." + suffix; | ||
return baseUri.buildUpon().path(newPath).build(); | ||
} | ||
|
||
public void start() { | ||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
urls.put(buildUri("srt"), false); | ||
|
||
if (Build.VERSION.SDK_INT >= 24) { | ||
final LocaleList localeList = Resources.getSystem().getConfiguration().getLocales(); | ||
for (int i = 0; i < localeList.size(); i++) { | ||
addLocale(localeList.get(i)); | ||
} | ||
} else { | ||
final Locale locale = Resources.getSystem().getConfiguration().locale; | ||
addLocale(locale); | ||
} | ||
|
||
OkHttpClient client = new OkHttpClient.Builder() | ||
//.callTimeout(15, TimeUnit.SECONDS) | ||
.build(); | ||
|
||
Callback callback = new Callback() { | ||
@Override | ||
public void onFailure(@NonNull Call call, @NonNull IOException e) { | ||
countDownLatch.countDown(); | ||
} | ||
|
||
@Override | ||
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { | ||
Uri url = Uri.parse(response.request().url().toString()); | ||
Utils.log(response.code() + ": " + url); | ||
if (response.isSuccessful()) { | ||
synchronized (urls) { | ||
urls.put(url, true); | ||
} | ||
} | ||
response.close(); | ||
countDownLatch.countDown(); | ||
} | ||
}; | ||
|
||
for (Uri url : urls.keySet()) { | ||
// Total Commander 3.24 / LAN plugin 3.20 does not support HTTP HEAD | ||
//Request request = new Request.Builder().url(url.toString()).head().build(); | ||
Request request = new Request.Builder().url(url.toString()).build(); | ||
client.newCall(request).enqueue(callback); | ||
} | ||
|
||
countDownLatch = new CountDownLatch(urls.size()); | ||
|
||
try { | ||
countDownLatch.await(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
for (Map.Entry<Uri, Boolean> set : urls.entrySet()) { | ||
if (set.getValue()) { | ||
subtitleUri = set.getKey(); | ||
break; | ||
} | ||
} | ||
|
||
if (subtitleUri == null) { | ||
return; | ||
} | ||
|
||
Utils.log(subtitleUri.toString()); | ||
|
||
// ProtocolException when reusing client: | ||
// java.net.ProtocolException: Unexpected status line: 1 | ||
client = new OkHttpClient.Builder() | ||
//.callTimeout(15, TimeUnit.SECONDS) | ||
.build(); | ||
|
||
Request request = new Request.Builder().url(subtitleUri.toString()).build(); | ||
try (Response response = client.newCall(request).execute()) { | ||
InputStream inputStream = response.body().byteStream(); | ||
Uri convertedSubtitleUri = SubtitleUtils.convertInputStreamToUTF(activity, subtitleUri, inputStream); | ||
|
||
activity.runOnUiThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
activity.mPrefs.updateSubtitle(convertedSubtitleUri); | ||
if (PlayerActivity.player != null) { | ||
MediaItem mediaItem = PlayerActivity.player.getCurrentMediaItem(); | ||
if (mediaItem != null) { | ||
MediaItem.SubtitleConfiguration subtitle = SubtitleUtils.buildSubtitle(activity, convertedSubtitleUri); | ||
mediaItem = mediaItem.buildUpon().setSubtitleConfigurations(Collections.singletonList(subtitle)).build(); | ||
PlayerActivity.player.setMediaItem(mediaItem, false); | ||
if (BuildConfig.DEBUG) { | ||
Toast.makeText(activity, "Subtitle found", Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} catch (IOException e) { | ||
Utils.log(e.toString()); | ||
e.printStackTrace(); | ||
} | ||
} | ||
}).start(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters