Muting and unmuting audio when autoplaying are available.

This commit is contained in:
Alex Ning 2020-05-01 10:50:40 +08:00
parent c52cf467ac
commit 25328e5d18
5 changed files with 76 additions and 11 deletions

View File

@ -13,6 +13,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
@ -37,6 +38,10 @@ import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target; import com.bumptech.glide.request.target.Target;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.PlayerView; import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.material.card.MaterialCardView; import com.google.android.material.card.MaterialCardView;
@ -44,6 +49,8 @@ import com.libRG.CustomTextView;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import java.util.List;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import im.ene.toro.CacheManager; import im.ene.toro.CacheManager;
@ -51,6 +58,7 @@ import im.ene.toro.ToroPlayer;
import im.ene.toro.ToroUtil; import im.ene.toro.ToroUtil;
import im.ene.toro.exoplayer.ExoCreator; import im.ene.toro.exoplayer.ExoCreator;
import im.ene.toro.exoplayer.ExoPlayerViewHelper; import im.ene.toro.exoplayer.ExoPlayerViewHelper;
import im.ene.toro.exoplayer.Playable;
import im.ene.toro.media.PlaybackInfo; import im.ene.toro.media.PlaybackInfo;
import im.ene.toro.widget.Container; import im.ene.toro.widget.Container;
import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.BlurTransformation;
@ -1301,7 +1309,10 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
super.onViewRecycled(holder); super.onViewRecycled(holder);
if (holder instanceof PostBaseViewHolder) { if (holder instanceof PostBaseViewHolder) {
if (holder instanceof PostImageAndGifAutoplayTypeViewHolder) { if (holder instanceof PostVideoAutoplayViewHolder) {
((PostVideoAutoplayViewHolder) holder).muteButton.setImageDrawable(mActivity.getDrawable(R.drawable.ic_mute_24dp));
((PostVideoAutoplayViewHolder) holder).muteButton.setVisibility(View.GONE);
} else if (holder instanceof PostImageAndGifAutoplayTypeViewHolder) {
mGlide.clear(((PostImageAndGifAutoplayTypeViewHolder) holder).imageView); mGlide.clear(((PostImageAndGifAutoplayTypeViewHolder) holder).imageView);
((PostImageAndGifAutoplayTypeViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.FIT_START); ((PostImageAndGifAutoplayTypeViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.FIT_START);
((PostImageAndGifAutoplayTypeViewHolder) holder).imageView.getLayoutParams().height = FrameLayout.LayoutParams.WRAP_CONTENT; ((PostImageAndGifAutoplayTypeViewHolder) holder).imageView.getLayoutParams().height = FrameLayout.LayoutParams.WRAP_CONTENT;
@ -1452,9 +1463,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
this.saveButton = saveButton; this.saveButton = saveButton;
this.shareButton = shareButton; this.shareButton = shareButton;
scoreTextView.setOnClickListener(view -> { scoreTextView.setOnClickListener(null);
//Do nothing in order to prevent clicking this to start ViewPostDetailActivity
});
if (mVoteButtonsOnTheRight) { if (mVoteButtonsOnTheRight) {
ConstraintSet constraintSet = new ConstraintSet(); ConstraintSet constraintSet = new ConstraintSet();
@ -1520,10 +1529,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
userTextView.setOnClickListener(view -> { userTextView.setOnClickListener(view -> {
if (canStartActivity) { if (canStartActivity) {
canStartActivity = false; Post post = getItem(getAdapterPosition());
Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); if (post != null) {
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, getItem(getAdapterPosition()).getAuthor()); canStartActivity = false;
mActivity.startActivity(intent); Intent intent = new Intent(mActivity, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, post.getAuthor());
mActivity.startActivity(intent);
}
} }
}); });
@ -1835,6 +1847,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
AspectRatioFrameLayout aspectRatioFrameLayout; AspectRatioFrameLayout aspectRatioFrameLayout;
@BindView(R.id.player_view_item_post_video_type_autoplay) @BindView(R.id.player_view_item_post_video_type_autoplay)
PlayerView videoPlayer; PlayerView videoPlayer;
@BindView(R.id.mute_exo_playback_control_view)
ImageButton muteButton;
@BindView(R.id.bottom_constraint_layout_item_post_video_type_autoplay) @BindView(R.id.bottom_constraint_layout_item_post_video_type_autoplay)
ConstraintLayout bottomConstraintLayout; ConstraintLayout bottomConstraintLayout;
@BindView(R.id.plus_button_item_post_video_type_autoplay) @BindView(R.id.plus_button_item_post_video_type_autoplay)
@ -1881,6 +1895,18 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
shareButton); shareButton);
aspectRatioFrameLayout.setOnClickListener(null); aspectRatioFrameLayout.setOnClickListener(null);
muteButton.setOnClickListener(view -> {
if (helper != null) {
if (helper.getVolume() != 0) {
muteButton.setImageDrawable(mActivity.getDrawable(R.drawable.ic_mute_24dp));
helper.setVolume(0f);
} else {
muteButton.setImageDrawable(mActivity.getDrawable(R.drawable.ic_unmute_24dp));
helper.setVolume(1f);
}
}
});
} }
void bindVideoUri(Uri videoUri) { void bindVideoUri(Uri videoUri) {
@ -1903,6 +1929,33 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
public void initialize(@NonNull Container container, @NonNull PlaybackInfo playbackInfo) { public void initialize(@NonNull Container container, @NonNull PlaybackInfo playbackInfo) {
if (helper == null) { if (helper == null) {
helper = new ExoPlayerViewHelper(this, mediaUri, null, mExoCreator); helper = new ExoPlayerViewHelper(this, mediaUri, null, mExoCreator);
helper.addEventListener(new Playable.EventListener() {
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
if (!trackGroups.isEmpty()) {
for (int i = 0; i < trackGroups.length; i++) {
String mimeType = trackGroups.get(i).getFormat(0).sampleMimeType;
if (mimeType != null && mimeType.contains("audio")) {
helper.setVolume(0f);
muteButton.setVisibility(View.VISIBLE);
break;
}
}
} else {
muteButton.setVisibility(View.GONE);
}
}
@Override
public void onMetadata(Metadata metadata) {
}
@Override
public void onCues(List<Cue> cues) {
}
});
} }
helper.initialize(container, playbackInfo); helper.initialize(container, playbackInfo);
} }

View File

@ -47,6 +47,9 @@ import javax.inject.Named;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import im.ene.toro.exoplayer.ExoCreator; import im.ene.toro.exoplayer.ExoCreator;
import im.ene.toro.media.PlaybackInfo;
import im.ene.toro.media.VolumeInfo;
import im.ene.toro.widget.Container;
import ml.docilealligator.infinityforreddit.Activity.BaseActivity; import ml.docilealligator.infinityforreddit.Activity.BaseActivity;
import ml.docilealligator.infinityforreddit.Activity.FilteredThingActivity; import ml.docilealligator.infinityforreddit.Activity.FilteredThingActivity;
import ml.docilealligator.infinityforreddit.Activity.MainActivity; import ml.docilealligator.infinityforreddit.Activity.MainActivity;
@ -78,6 +81,9 @@ import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.Utils.Utils; import ml.docilealligator.infinityforreddit.Utils.Utils;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import static im.ene.toro.media.PlaybackInfo.INDEX_UNSET;
import static im.ene.toro.media.PlaybackInfo.TIME_UNSET;
/** /**
* A simple {@link Fragment} subclass. * A simple {@link Fragment} subclass.
@ -564,6 +570,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mPostRecyclerView.setAdapter(mAdapter); mPostRecyclerView.setAdapter(mAdapter);
mPostRecyclerView.setCacheManager(mAdapter); mPostRecyclerView.setCacheManager(mAdapter);
mPostRecyclerView.setPlayerInitializer(order -> {
VolumeInfo volumeInfo = new VolumeInfo(true, 0f);
return new PlaybackInfo(INDEX_UNSET, TIME_UNSET, volumeInfo);
});
mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts)); mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts));
mPostViewModel.hasPost().observe(this, hasPost -> { mPostViewModel.hasPost().observe(this, hasPost -> {

View File

@ -514,6 +514,6 @@ public class Post implements Parcelable {
if (!(obj instanceof Post)) { if (!(obj instanceof Post)) {
return false; return false;
} }
return ((Post) obj).getFullName().equals(fullName); return ((Post) obj).id.equals(id);
} }
} }

View File

@ -248,7 +248,6 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
nextPageKey = lastItem; nextPageKey = lastItem;
} }
int currentPostsSize = postLinkedHashSet.size();
if (newPosts.size() != 0) { if (newPosts.size() != 0) {
postLinkedHashSet.addAll(newPosts); postLinkedHashSet.addAll(newPosts);
callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); callback.onResult(new ArrayList<>(newPosts), null, nextPageKey);

View File

@ -26,7 +26,9 @@
<ImageButton <ImageButton
android:id="@+id/mute_exo_playback_control_view" android:id="@+id/mute_exo_playback_control_view"
style="@style/ExoMediaButton" style="@style/ExoMediaButton"
android:layout_alignParentEnd="true"/> android:src="@drawable/ic_mute_24dp"
android:layout_alignParentEnd="true"
android:visibility="gone" />
</RelativeLayout> </RelativeLayout>