Load HLS video instead of Dash video to support older Android versions. Fixed app crashes on opening video or images on older Android versions.

This commit is contained in:
Alex Ning 2019-09-10 16:41:54 +08:00
parent f4515514c1
commit 354a215209
11 changed files with 31 additions and 39 deletions

View File

@ -429,7 +429,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
Intent intent = new Intent(mActivity, ViewVideoActivity.class);
intent.setData(gifVideoUri);
intent.putExtra(ViewVideoActivity.TITLE_KEY, mPost.getTitle());
intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, mPost.isDashVideo());
intent.putExtra(ViewVideoActivity.IS_HLS_VIDEO_KEY, mPost.isHLSVideo());
intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mPost.isDownloadableGifOrVideo());
if(mPost.isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mPost.getGifOrVideoDownloadUrl());
@ -447,7 +447,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
Intent intent = new Intent(mActivity, ViewVideoActivity.class);
intent.setData(videoUri);
intent.putExtra(ViewVideoActivity.TITLE_KEY, mPost.getTitle());
intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, mPost.isDashVideo());
intent.putExtra(ViewVideoActivity.IS_HLS_VIDEO_KEY, mPost.isHLSVideo());
intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mPost.isDownloadableGifOrVideo());
if(mPost.isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mPost.getGifOrVideoDownloadUrl());

View File

@ -34,7 +34,7 @@ public class JSONUtils {
static final String URL_KEY = "url";
static final String MEDIA_KEY = "media";
static final String REDDIT_VIDEO_KEY = "reddit_video";
static final String DASH_URL_KEY = "dash_url";
static final String HLS_URL_KEY = "hls_url";
static final String IS_VIDEO_KEY = "is_video";
static final String CROSSPOST_PARENT_LIST = "crosspost_parent_list";
static final String REDDIT_VIDEO_PREVIEW_KEY = "reddit_video_preview";

View File

@ -262,7 +262,7 @@ class ParsePost {
//Video post
JSONObject redditVideoObject = data.getJSONObject(JSONUtils.MEDIA_KEY).getJSONObject(JSONUtils.REDDIT_VIDEO_KEY);
int postType = Post.VIDEO_TYPE;
String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.DASH_URL_KEY)).toString();
String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString();
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
title, previewUrl, permalink, score, postType, voteType, gilded, flair, hidden,
@ -290,10 +290,10 @@ class ParsePost {
post.setDownloadableGifOrVideo(true);
post.setGifOrVideoDownloadUrl(gifDownloadUrl);
} else if(data.getJSONObject(JSONUtils.PREVIEW_KEY).has(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY)) {
//Gif video post (Dash)
//Gif video post (HLS)
int postType = Post.GIF_VIDEO_TYPE;
String videoUrl = Html.fromHtml(data.getJSONObject(JSONUtils.PREVIEW_KEY)
.getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.DASH_URL_KEY)).toString();
.getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.HLS_URL_KEY)).toString();
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
formattedPostTime, title, previewUrl, permalink, score, postType, voteType,

View File

@ -47,7 +47,7 @@ class Post implements Parcelable {
private boolean locked;
private boolean saved;
private boolean isCrosspost;
private boolean isDashVideo;
private boolean isHLSVideo;
private boolean isDownloadableGifOrVideo;
private String crosspostParentId;
@ -55,7 +55,7 @@ class Post implements Parcelable {
String postTime, String title, String previewUrl, String permalink, int score, int postType,
int voteType, int gilded, String flair, boolean hidden, boolean spoiler, boolean nsfw,
boolean stickied, boolean archived, boolean locked, boolean saved, boolean isCrosspost,
boolean isDashVideo) {
boolean isHLSVideo) {
this.id = id;
this.fullName = fullName;
this.subredditName = subredditName;
@ -79,7 +79,7 @@ class Post implements Parcelable {
this.locked = locked;
this.saved = saved;
this.isCrosspost = isCrosspost;
this.isDashVideo = isDashVideo;
this.isHLSVideo = isHLSVideo;
}
Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author,
@ -172,7 +172,7 @@ class Post implements Parcelable {
locked = in.readByte() != 0;
saved = in.readByte() != 0;
isCrosspost = in.readByte() != 0;
isDashVideo = in.readByte() != 0;
isHLSVideo = in.readByte() != 0;
isDownloadableGifOrVideo = in.readByte() != 0;
crosspostParentId = in.readString();
}
@ -354,8 +354,8 @@ class Post implements Parcelable {
return 0;
}
boolean isDashVideo() {
return isDashVideo;
boolean isHLSVideo() {
return isHLSVideo;
}
void setDownloadableGifOrVideo(boolean isDownloadableGifOrVideo) {
@ -431,7 +431,7 @@ class Post implements Parcelable {
parcel.writeByte((byte) (locked ? 1 : 0));
parcel.writeByte((byte) (saved ? 1 : 0));
parcel.writeByte((byte) (isCrosspost ? 1 : 0));
parcel.writeByte((byte) (isDashVideo ? 1 : 0));
parcel.writeByte((byte) (isHLSVideo ? 1 : 0));
parcel.writeByte((byte) (isDownloadableGifOrVideo ? 1 : 0));
parcel.writeString(crosspostParentId);
}

View File

@ -419,7 +419,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
Intent intent = new Intent(mContext, ViewVideoActivity.class);
intent.setData(gifVideoUri);
intent.putExtra(ViewVideoActivity.TITLE_KEY, title);
intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, post.isDashVideo());
intent.putExtra(ViewVideoActivity.IS_HLS_VIDEO_KEY, post.isHLSVideo());
intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, post.isDownloadableGifOrVideo());
if(post.isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, post.getGifOrVideoDownloadUrl());
@ -437,7 +437,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
Intent intent = new Intent(mContext, ViewVideoActivity.class);
intent.setData(videoUri);
intent.putExtra(ViewVideoActivity.TITLE_KEY, title);
intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, post.isDashVideo());
intent.putExtra(ViewVideoActivity.IS_HLS_VIDEO_KEY, post.isHLSVideo());
intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, post.isDownloadableGifOrVideo());
if(post.isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, post.getGifOrVideoDownloadUrl());

View File

@ -95,7 +95,7 @@ public class ViewImageActivity extends AppCompatActivity {
ActionBar actionBar = getSupportActionBar();
Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp);
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparentActionBarColor)));
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor)));
setTitle("");
Intent intent = getIntent();
@ -113,7 +113,7 @@ public class ViewImageActivity extends AppCompatActivity {
float pxHeight = getResources().getDisplayMetrics().heightPixels;
int activityColorFrom = getResources().getColor(android.R.color.black);
int actionBarColorFrom = getResources().getColor(R.color.transparentActionBarColor);
int actionBarColorFrom = getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor);
int actionBarElementColorFrom = getResources().getColor(android.R.color.white);
int colorTo = getResources().getColor(android.R.color.transparent);

View File

@ -39,8 +39,8 @@ import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.dash.DashChunkSource;
import com.google.android.exoplayer2.source.dash.DashMediaSource;
import com.google.android.exoplayer2.source.dash.DefaultDashChunkSource;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
@ -59,7 +59,7 @@ public class ViewVideoActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
static final String TITLE_KEY = "TK";
static final String IS_DASH_VIDEO_KEY = "IDVK";
static final String IS_HLS_VIDEO_KEY = "IHVK";
static final String IS_DOWNLOADABLE_KEY = "IDK";
static final String DOWNLOAD_URL_KEY = "DUK";
static final String SUBREDDIT_KEY = "SK";
@ -76,7 +76,7 @@ public class ViewVideoActivity extends AppCompatActivity {
private String mGifOrVideoFileName;
private String mDownloadUrl;
private boolean mIsDashVideo;
private boolean mIsHLSVideo;
private boolean wasPlaying;
private boolean isDownloading = false;
private float totalLengthY = 0.0f;
@ -92,10 +92,9 @@ public class ViewVideoActivity extends AppCompatActivity {
ActionBar actionBar = getSupportActionBar();
Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp);
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparentActionBarColor)));
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor)));
setTitle("");
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || getResources().getBoolean(R.bool.isTablet)) {
//Set player controller bottom margin in order to display it above the navbar
int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
@ -112,7 +111,7 @@ public class ViewVideoActivity extends AppCompatActivity {
Intent intent = getIntent();
mVideoUri = intent.getData();
mIsDashVideo = intent.getExtras().getBoolean(IS_DASH_VIDEO_KEY);
mIsHLSVideo = intent.getExtras().getBoolean(IS_HLS_VIDEO_KEY);
if(intent.getExtras().getBoolean(IS_DOWNLOADABLE_KEY)) {
mGifOrVideoFileName = intent.getExtras().getString(SUBREDDIT_KEY).substring(2)
@ -123,7 +122,7 @@ public class ViewVideoActivity extends AppCompatActivity {
final float pxHeight = getResources().getDisplayMetrics().heightPixels;
int activityColorFrom = getResources().getColor(android.R.color.black);
int actionBarColorFrom = getResources().getColor(R.color.transparentActionBarColor);
int actionBarColorFrom = getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor);
int actionBarElementColorFrom = getResources().getColor(android.R.color.white);
int colorTo = getResources().getColor(android.R.color.transparent);
@ -286,7 +285,7 @@ public class ViewVideoActivity extends AppCompatActivity {
});
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory();
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
videoPlayerView.setPlayer(player);
@ -295,9 +294,9 @@ public class ViewVideoActivity extends AppCompatActivity {
Util.getUserAgent(this, "Infinity"), bandwidthMeter);
// Prepare the player with the source.
if(mIsDashVideo) {
if(mIsHLSVideo) {
DashChunkSource.Factory dashChunkSourceFactory = new DefaultDashChunkSource.Factory(dataSourceFactory);
player.prepare(new DashMediaSource(mVideoUri, dataSourceFactory, dashChunkSourceFactory, null, null));
player.prepare(new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
} else {
player.prepare(new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
}
@ -308,7 +307,7 @@ public class ViewVideoActivity extends AppCompatActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if(!mIsDashVideo) {
if(!mIsHLSVideo) {
getMenuInflater().inflate(R.menu.view_video, menu);
mMenu = menu;
}

View File

@ -7,7 +7,7 @@
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="16dp"
android:background="@color/transparentActionBarColor"
android:background="@color/transparentActionBarAndExoPlayerControllerColor"
android:orientation="vertical">
<LinearLayout

View File

@ -6,7 +6,7 @@
<color name="downvoted">#E91E63</color>
<color name="transparentActionBarColor">#88000000</color>
<color name="transparentActionBarAndExoPlayerControllerColor">#88000000</color>
<color name="gold">#FFC107</color>

View File

@ -4,7 +4,7 @@
<color name="colorPrimaryDark">#0D47A1</color>
<color name="colorAccent">#FF4081</color>
<color name="transparentActionBarColor">#88000000</color>
<color name="transparentActionBarAndExoPlayerControllerColor">#88000000</color>
<color name="gold">#FFC107</color>

View File

@ -28,7 +28,7 @@
</style>
<style name="AppTheme.ActionBar.Transparent" parent="AppTheme">
<item name="colorPrimary">@color/transparentActionBarColor</item>
<item name="android:windowBackground">@android:color/black</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
@ -37,13 +37,6 @@
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.DayNight" />
<style name="Theme.AppCompat.Transparent" parent="AppTheme.ActionBar.Transparent">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
</style>
<style name="CustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">@android:color/white</item>
<item name="tabIndicatorHeight">3dp</item>