diff --git a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java index fbedac5..2cdd5c7 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java @@ -60,19 +60,29 @@ import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.ExoPlayerFactory; +import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; +import com.google.android.exoplayer2.source.MediaSource; +import com.google.android.exoplayer2.source.MergingMediaSource; import com.google.android.exoplayer2.source.ProgressiveMediaSource; +import com.google.android.exoplayer2.source.SingleSampleMediaSource; +import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; +import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; +import com.google.android.exoplayer2.trackselection.TrackSelector; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; import com.google.android.exoplayer2.ui.PlayerControlView; import com.google.android.exoplayer2.ui.PlayerView; +import com.google.android.exoplayer2.upstream.BandwidthMeter; import com.google.android.exoplayer2.upstream.DataSource; +import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; +import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.Util; import org.jetbrains.annotations.NotNull; - import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; @@ -111,6 +121,7 @@ import app.fedilab.fedilabtube.webview.MastalabWebViewClient; import es.dmoral.toasty.Toasty; import static app.fedilab.fedilabtube.helper.Helper.getAttColor; +import static app.fedilab.fedilabtube.helper.Helper.getLiveInstance; import static app.fedilab.fedilabtube.helper.Helper.isLoggedIn; import static app.fedilab.fedilabtube.viewmodel.PlaylistsVM.action.GET_PLAYLIST; import static app.fedilab.fedilabtube.viewmodel.PlaylistsVM.action.GET_PLAYLIST_FOR_VIDEO; @@ -142,7 +153,6 @@ public class PeertubeActivity extends AppCompatActivity { private boolean playInMinimized; private boolean onStopCalled; private List captions; - private String captionValue; public static void hideKeyboard(Activity activity) { if (activity != null && activity.getWindow() != null) { @@ -684,7 +694,45 @@ public class PeertubeActivity extends AppCompatActivity { } } dialogBuilder.setSingleChoiceItems(itemsLabelLanguage, i, (dialog, which) -> { - captionValue = itemsKeyLanguage[which]; + Uri uri = Uri.parse("https://" + getLiveInstance(PeertubeActivity.this) + captions.get(which).getCaptionPath()); + int video_cache = sharedpreferences.getInt(Helper.SET_VIDEO_CACHE, Helper.DEFAULT_VIDEO_CACHE_MB); + + long position = player.getCurrentPosition(); + + if (player != null) + player.release(); + + BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); + TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter)); + player = ExoPlayerFactory.newSimpleInstance(PeertubeActivity.this, trackSelector); + ProgressiveMediaSource videoSource; + MediaSource subtitleSource; + Format subtitleFormat = Format.createTextSampleFormat( + null, + MimeTypes.APPLICATION_SUBRIP, + Format.NO_VALUE, + "en"); + if (video_cache == 0) { + DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(PeertubeActivity.this, + Util.getUserAgent(PeertubeActivity.this, null), null); + videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory) + .createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null, PeertubeActivity.this))); + + subtitleSource = new SingleSampleMediaSource(uri, dataSourceFactory, subtitleFormat, C.TIME_UNSET); + } else { + CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(PeertubeActivity.this); + videoSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory) + .createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null, PeertubeActivity.this))); + subtitleSource = new SingleSampleMediaSource(uri, cacheDataSourceFactory, subtitleFormat, C.TIME_UNSET); + } + MergingMediaSource mergedSource = + new MergingMediaSource(videoSource, subtitleSource); + playerView.setPlayer(player); + player.prepare(mergedSource); + player.seekTo(0, position); + player.setPlayWhenReady(true); + + }); dialogBuilder.setOnDismissListener(dialogInterface -> { @@ -748,6 +796,7 @@ public class PeertubeActivity extends AppCompatActivity { change(); } + public void manageVIewComment(APIResponse apiResponse) { if (apiResponse == null || (apiResponse.getError() != null && apiResponse.getError().getStatusCode() != 404 && apiResponse.getError() != null && apiResponse.getError().getStatusCode() != 501)) { if (apiResponse == null) diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/APIResponse.java b/app/src/main/java/app/fedilab/fedilabtube/client/APIResponse.java index cbeaa22..058d170 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/APIResponse.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/APIResponse.java @@ -45,6 +45,7 @@ public class APIResponse { private List instances; private String stringData; private int statusCode; + private String captionText; public List getAccounts() { return accounts; @@ -180,4 +181,12 @@ public class APIResponse { public void setCaptions(List captions) { this.captions = captions; } + + public String getCaptionText() { + return captionText; + } + + public void setCaptionText(String captionText) { + this.captionText = captionText; + } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java index 235781f..c9348ba 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java @@ -21,7 +21,6 @@ import android.database.sqlite.SQLiteDatabase; import android.os.Build; import android.text.Html; import android.text.SpannableString; -import android.util.Log; import org.json.JSONArray; import org.json.JSONException; @@ -1010,7 +1009,6 @@ public class PeertubeAPI { apiResponse = new APIResponse(); try { String response = new HttpsConnection(context).get(getAbsoluteUrl(String.format("/videos/%s/captions", videoId)), 60, null, prefKeyOauthTokenT); - Log.v(Helper.TAG, "response: " + response); JSONArray jsonArray = new JSONObject(response).getJSONArray("data"); List captions = parseCaption(jsonArray); apiResponse.setCaptions(captions); diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/CaptionsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/CaptionsVM.java index 2fdf41b..3def757 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/CaptionsVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/CaptionsVM.java @@ -25,6 +25,7 @@ import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import app.fedilab.fedilabtube.client.APIResponse; +import app.fedilab.fedilabtube.client.HttpsConnection; import app.fedilab.fedilabtube.client.PeertubeAPI; @@ -41,6 +42,12 @@ public class CaptionsVM extends AndroidViewModel { return apiResponseMutableLiveData; } + public LiveData loadCaption(String url) { + apiResponseMutableLiveData = new MutableLiveData<>(); + load(url); + return apiResponseMutableLiveData; + } + private void loadCaptions(String videoId) { Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { @@ -55,4 +62,21 @@ public class CaptionsVM extends AndroidViewModel { } }).start(); } + + private void load(String url) { + Context _mContext = getApplication().getApplicationContext(); + new Thread(() -> { + try { + String captionText = new HttpsConnection(_mContext).get(url); + APIResponse apiResponse = new APIResponse(); + apiResponse.setCaptionText(captionText); + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse); + mainHandler.post(myRunnable); + } catch (Exception e) { + e.printStackTrace(); + } + }).start(); + } + }