Skip to content

Commit

Permalink
Add VideoPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
briceln committed Jan 1, 2019
1 parent 4c77747 commit 12e2721
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ android {
buildTypes.each {
it.buildConfigField 'String', 'IMGUR_API_KEY', ImgurApiKey
}
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
Expand Down Expand Up @@ -69,7 +72,7 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.1'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.1'

implementation 'com.github.pedromassango:IBackdrop:2.1.0'
implementation 'com.google.android.exoplayer:exoplayer:2.9.3'

testImplementation 'junit:junit:4.12'
implementation 'com.android.support:design:28.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.Response;
import com.rockerhieu.rvadapter.states.StatesRecyclerViewAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ private String getImgLink(String url) {
return url;
}

public boolean isVideo() {
return (original_url.endsWith(".mp4") || original_url.endsWith(".gifv"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.text.Html;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -29,6 +31,18 @@
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.target.Target;
import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;
import com.rockerhieu.rvadapter.states.StatesRecyclerViewAdapter;
Expand Down Expand Up @@ -172,8 +186,30 @@ private void onLinearLayout(final ViewHolder holder, final CircularProgressDrawa
ElapsedTime elapsedTime = new ElapsedTime(holder.mItem.time);
holder.timePost.setText(elapsedTime.getTimeString(res).toUpperCase());

if (images.size() <= 1) {
if (images.get(0).endsWith(".mp4") || images.get(0).endsWith(".gifv")) {
holder.carouselView.setVisibility(View.GONE);
holder.mImageView.setVisibility(View.GONE);
holder.player.setVisibility(View.VISIBLE);

SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(holder.mView.getContext());

holder.player.setPlayer(player);

Uri uri = Uri.parse(images.get(0).endsWith(".mp4") ? images.get(0) : images.get(0).replace(".gifv", ".mp4") );

// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(holder.mView.getContext(),
Util.getUserAgent(holder.mView.getContext(), "yourApplicationName"));
// This is the MediaSource representing the media to be played.
MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
// Prepare the player with the source.
player.prepare(videoSource);


} else if (images.size() <= 1) {
holder.carouselView.setVisibility(View.GONE);
holder.player.setVisibility(View.GONE);
holder.mImageView.setVisibility(View.VISIBLE);
ImageView imageView = holder.mImageView;
holder.mImageView.setImageDrawable(null);
Expand All @@ -193,6 +229,7 @@ private void onLinearLayout(final ViewHolder holder, final CircularProgressDrawa
} else {
CarouselView carouselView = holder.carouselView;
carouselView.setVisibility(View.VISIBLE);
holder.player.setVisibility(View.GONE);
holder.mImageView.setVisibility(View.GONE);
carouselView.setPageCount(images.size());

Expand Down Expand Up @@ -253,6 +290,7 @@ public void onBindViewHolder(@NonNull final ViewHolder holder, final int positio
Display display = mainActivity.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Log.e("INFOS", holder.mItem.toString());
ViewGroup.LayoutParams lp = card.getLayoutParams();
if (lp instanceof StaggeredGridLayoutManager.LayoutParams) {
card.setLayoutParams(lp);
Expand Down Expand Up @@ -473,6 +511,7 @@ class ViewHolder extends RecyclerView.ViewHolder {
public final ImageButton deleteButton;
public final ImageButton editButton;
public final ChipGroup chips;
public final PlayerView player;
public PostItem mItem;

ViewHolder(View view) {
Expand All @@ -490,6 +529,7 @@ class ViewHolder extends RecyclerView.ViewHolder {
mDislike = view.findViewById(R.id.button_dislike);
deleteButton = view.findViewById(R.id.delete_button);
editButton = view.findViewById(R.id.edit_button);
player = view.findViewById(R.id.video_player);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void onRefresh() {
View loadingView = getLayoutInflater().inflate(R.layout.view_loading, recyclerView, false);
View emptyView = getLayoutInflater().inflate(R.layout.view_empty, recyclerView, false);
View errorView = getLayoutInflater().inflate(R.layout.view_error, recyclerView, false);
adapter = new MyPostsRecyclerViewAdapter(mListener, getContext(), (MainActivity)getActivity(), imgurApi);
adapter = new MyPostsRecyclerViewAdapter(mListener, getContext(), (MainActivity) getActivity(), imgurApi);
statesRecyclerViewAdapter = new StatesRecyclerViewAdapter(adapter, loadingView, emptyView, errorView);
recyclerView.setAdapter(statesRecyclerViewAdapter);
adapter.statesRecyclerViewAdapter = statesRecyclerViewAdapter;
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/fragment_posts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@

</com.synnapps.carouselview.CarouselView>

<com.google.android.exoplayer2.ui.PlayerView
android:visibility="gone"
android:id="@+id/video_player"
android:layout_width="match_parent"
android:layout_height="400dp">

</com.google.android.exoplayer2.ui.PlayerView>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
Expand Down

0 comments on commit 12e2721

Please sign in to comment.