Commit fdcdb8bf by fengquan

feat: 咨询详情页视频播放

parent cce84b56
package com.yidianling.consultant.preview;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
......@@ -9,8 +10,10 @@ import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import androidx.annotation.CallSuper;
import androidx.annotation.Nullable;
......@@ -18,8 +21,18 @@ import androidx.core.view.ViewCompat;
import androidx.fragment.app.Fragment;
import com.dou361.ijkplayer.widget.IjkVideoView;
import com.ydl.ydlcommon.ui.Loading;
import com.yidianling.consultant.R;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import tv.danmaku.ijk.media.player.IMediaPlayer;
/**
* @author rainb
......@@ -44,6 +57,16 @@ public class BasePhotoFragment extends Fragment {
private LinearLayout btnLL;
private RelativeLayout videoContainer;
private IjkVideoView videoView;
private Disposable disposable;
private SeekBar seekBar;
private View ivPlay;
private boolean isPrepared = false;
private ImageView ivImg;
private volatile boolean isTouch = false;
private boolean isCompletion = false;
private boolean isVisiable = false;
private View ivBack;
// public static VideoClickListener listener;
......@@ -102,20 +125,34 @@ public class BasePhotoFragment extends Fragment {
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
isVisiable = isVisibleToUser;
if (!isVisibleToUser && beanViewInfo != null) {
videoView.seekTo(0);
seekBar.setProgress(0);
videoView.pause();
if (ivPlay != null && !TextUtils.isEmpty(beanViewInfo.getVideoUrl())) {
ivPlay.setVisibility(View.VISIBLE);
if (ivImg != null) {
ivImg.setVisibility(View.VISIBLE);
}
}
stopTiming();
}
}
@Override
public void onDestroy() {
super.onDestroy();
ZoomMediaLoader.getInstance().getLoader().clearMemory(getActivity());
if (getActivity() != null && getActivity().isFinishing()) {
// listener = null;
}
}
public void release() {
isTransPhoto = false;
if (videoView != null) {
videoView.release(true);
stopTiming();
}
}
/**
......@@ -132,8 +169,19 @@ public class BasePhotoFragment extends Fragment {
imageView.setBtnLL(btnLL);
videoContainer = view.findViewById(R.id.videoContainer);
videoView = view.findViewById(R.id.videoView);
seekBar = view.findViewById(R.id.seekbar);
ivPlay = view.findViewById(R.id.ivPlay);
ivImg = view.findViewById(R.id.ivImage);
ivBack = view.findViewById(R.id.ivBack);
imageView.setBackIv(ivBack);
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
transformOutAnim();
((GPreviewActivity) getActivity()).transformOut();
}
});
// btnVideo.setOnClickListener(new View.OnClickListener() {
// @Override
......@@ -151,27 +199,23 @@ public class BasePhotoFragment extends Fragment {
// });
mySimpleTarget = new MySimpleTarget() {
@Override
public void onResourceReady() {
loading.setVisibility(View.GONE);
String video = beanViewInfo.getVideoUrl();
if (video != null && !video.isEmpty()) {
btnVideo.setVisibility(View.VISIBLE);
ViewCompat.animate(btnVideo).alpha(1).setDuration(1000).start();
} else {
btnVideo.setVisibility(View.GONE);
}
// String video = beanViewInfo.getVideoUrl();
// if (video != null && !video.isEmpty()) {
// btnVideo.setVisibility(View.VISIBLE);
// ViewCompat.animate(btnVideo).alpha(1).setDuration(1000).start();
// } else {
// btnVideo.setVisibility(View.GONE);
// }
}
@Override
public void onLoadFailed(Drawable errorDrawable) {
loading.setVisibility(View.GONE);
btnVideo.setVisibility(View.GONE);
if (errorDrawable != null) {
imageView.setImageDrawable(errorDrawable);
}
......@@ -198,7 +242,8 @@ public class BasePhotoFragment extends Fragment {
//是否展示动画
isTransPhoto = bundle.getBoolean(KEY_TRANS_PHOTO, false);
if (!TextUtils.isEmpty(beanViewInfo.getUrl())){
if (!TextUtils.isEmpty(beanViewInfo.getUrl())) {
ivPlay.setVisibility(View.GONE);
if (beanViewInfo.getUrl().toLowerCase().contains(".gif")) {
imageView.setVisibility(View.VISIBLE);
videoContainer.setVisibility(View.GONE);
......@@ -209,20 +254,103 @@ public class BasePhotoFragment extends Fragment {
//加载图
ZoomMediaLoader.getInstance().getLoader().displayImage(this, beanViewInfo.getUrl(), imageView, mySimpleTarget);
}
}else {
} else {
ivPlay.setVisibility(View.VISIBLE);
videoContainer.setVisibility(View.VISIBLE);
imageView.setVisibility(View.GONE);
// IjkVideoView videoView = new IjkVideoView(getContext());
// videoContainer.addView(videoView);
String videoUlr = beanViewInfo.getVideoUrl();
ZoomMediaLoader.getInstance().getLoader().displayImage(this, "https://hellorfimg.zcool.cn/preview260/123343987.jpg", ivImg, mySimpleTarget);
final String videoUlr = beanViewInfo.getVideoUrl();
if (!TextUtils.isEmpty(videoUlr)) {
rootView.setBackgroundColor(Color.BLACK);
seekBar.setVisibility(View.VISIBLE);
videoView.setVideoURI(Uri.parse(videoUlr));
videoView.start();
videoView.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(IMediaPlayer iMediaPlayer) {
loading.setVisibility(View.GONE);
isPrepared = true;
int maxProgress = (int) iMediaPlayer.getDuration();
if (maxProgress > 0) {
seekBar.setMax(maxProgress);
} else {
seekBar.setVisibility(View.GONE);
}
}
});
videoView.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(IMediaPlayer iMediaPlayer) {
isCompletion = true;
videoView.seekTo(0);
stopTiming();
ivPlay.setVisibility(View.VISIBLE);
}
});
videoView.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(IMediaPlayer iMediaPlayer, int what, int extra) {
if (what == IMediaPlayer.MEDIA_INFO_BUFFERING_START && !isCompletion && isVisiable) {
Loading.INSTANCE.show(getContext(), "正在加载");
} else if (what == IMediaPlayer.MEDIA_INFO_BUFFERING_END) {
Loading.INSTANCE.close();
}
return true;
}
});
ivPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isPrepared) {
loading.setVisibility(View.VISIBLE);
}
ivImg.setVisibility(View.GONE);
ivPlay.setVisibility(View.GONE);
startTiming();
videoView.start();
isCompletion = false;
}
});
videoContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (videoView.isPlaying()) {
ivPlay.setVisibility(View.VISIBLE);
stopTiming();
videoView.pause();
}
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
isTouch = true;
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
isTouch = false;
if (seekBar != null) {
videoView.seekTo(seekBar.getProgress());
videoView.start();
ivPlay.setVisibility(View.GONE);
isCompletion = false;
}
}
});
}
}
......@@ -253,6 +381,7 @@ public class BasePhotoFragment extends Fragment {
@Override
public void onPhotoTap(View view, float x, float y) {
if (imageView.checkMinScale()) {
transformOutAnim();
((GPreviewActivity) getActivity()).transformOut();
}
}
......@@ -319,4 +448,58 @@ public class BasePhotoFragment extends Fragment {
public IThumbViewInfo getBeanViewInfo() {
return beanViewInfo;
}
private void startTiming() {
disposable = Observable.interval(3, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.computation())
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
// val pos = (video?.mMediaPlayer as IjkMediaPlayer?)?.currentPosition ?: 0
// if (pos > 0) {
//// if (vm?.isVideo == true) {
//// binding.ivCover.visibility = View.GONE
//// }
// if (!isTouch) {
// binding.seekbar.progress = pos.toInt()// video.bufferPercentage
// }
// if (video?.isPlaying == true) {
// when (vm?.isVideo) {
// true -> {
// binding.ivPlay.visibility = View.GONE
// }
// false -> {
// binding.voicePlay.start()
// }
// }
// }
// }
if (videoView != null && !isTouch) {
int currentPosition = videoView.getCurrentPosition();
seekBar.setProgress(currentPosition);
}
}
});
}
private void stopTiming() {
if (disposable != null) {
disposable.dispose();
disposable = null;
}
}
public void transformOutAnim() {
ObjectAnimator animator1 = ObjectAnimator.ofFloat(ivBack, "alpha", 1.0f, 0.0f);
animator1.setDuration(200);
animator1.start();
ObjectAnimator animator2 = ObjectAnimator.ofFloat(btnLL, "alpha", 1.0f, 0.0f);
animator2.setDuration(200);
animator2.start();
}
}
......@@ -2,6 +2,7 @@ package com.yidianling.consultant.preview;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
......@@ -49,7 +50,6 @@ public class GPreviewActivity extends AppCompatActivity {
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(currentIndex);
// viewPager.setOffscreenPageLimit(3);
viewPager.setOffscreenPageLimit(0);
}
private void initData() {
......@@ -126,15 +126,19 @@ public class GPreviewActivity extends AppCompatActivity {
isTransformOut = true;
int currentItem = viewPager.getCurrentItem();
if (currentItem < imgUrls.size()) {
BasePhotoFragment fragment = fragments.get(currentItem);
fragment.changeBg(Color.TRANSPARENT);
fragment.transformOut(new SmoothImageView.onTransformListener() {
@Override
public void onTransformCompleted(SmoothImageView.Status status) {
getViewPager().setEnabled(true);
exit();
}
});
if (TextUtils.isEmpty(imgUrls.get(currentItem).getVideoUrl())){
BasePhotoFragment fragment = fragments.get(currentItem);
fragment.changeBg(Color.TRANSPARENT);
fragment.transformOut(new SmoothImageView.onTransformListener() {
@Override
public void onTransformCompleted(SmoothImageView.Status status) {
getViewPager().setEnabled(true);
exit();
}
});
}else {
exit();
}
} else {
exit();
}
......
......@@ -70,7 +70,7 @@ public class GridPreviewActivity extends BaseActivity {
mThumbViewInfoList.add(new UserViewInfo(urls.get(i)));
}
mThumbViewInfoList.add(4, new UserViewInfo("https://pic.ydlcdn.com/Qkkb6HmQiM.mp4", null));
mThumbViewInfoList.add(4, new UserViewInfo("https://pic.ydlcdn.com/GGyHyDwKJ8.MP4", null));
adapter = new MyListAdapter();
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
......
......@@ -54,6 +54,7 @@ public class SmoothImageView extends PhotoView {
private float MAX_TRANS_SCALE = 0.2f;
private boolean isMaskPoint = false;
private View btnLL;
private View backIv;
@Override
protected void onDetachedFromWindow() {
......@@ -62,6 +63,7 @@ public class SmoothImageView extends PhotoView {
bitmapHeight = 0;
thumbRect = null;
btnLL = null;
backIv = null;
ISFUll = false;
if (animator != null) {
animator.cancel();
......@@ -190,6 +192,9 @@ public class SmoothImageView extends PhotoView {
if (btnLL != null && btnLL.getVisibility() == View.VISIBLE) {
btnLL.setVisibility(INVISIBLE);
}
if (backIv != null && backIv.getVisibility() == View.VISIBLE) {
backIv.setVisibility(INVISIBLE);
}
if (alphaChangeListener != null) {
alphaChangeListener.onAlphaChange(alpha);
}
......@@ -207,6 +212,10 @@ public class SmoothImageView extends PhotoView {
if (btnLL != null && btnLL.getVisibility() != View.VISIBLE) {
btnLL.setVisibility(VISIBLE);
}
if (backIv != null && backIv.getVisibility() != View.VISIBLE) {
backIv.setVisibility(VISIBLE);
}
} else {
changeTransform();
setTag(R.id.consultant_item_image_key, true);
......@@ -484,6 +493,10 @@ public class SmoothImageView extends PhotoView {
this.btnLL = view;
}
public void setBackIv(View view){
this.backIv = view;
}
private void initTransform() {
if (getDrawable() == null) {
return;
......
......@@ -31,6 +31,11 @@ public class TestImageLoader implements IZoomMediaLoader {
simpleTarget.onResourceReady();
imageView.setImageBitmap(resource);
}
@Override
public void onLoadFailed(@Nullable @org.jetbrains.annotations.Nullable Drawable errorDrawable) {
simpleTarget.onLoadFailed(errorDrawable);
}
});
}
......
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="@dimen/platform_dp_8"/>
<solid android:color="#40000000"/>
</shape>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<selector>
<item android:state_pressed="true">
<shape>
<corners android:radius="4dp" />
<solid android:color="#59FFFFFF" />
<stroke android:width="1dp" android:color="@color/transparent"/>
</shape>
</item>
<item>
<shape>
<corners android:radius="4dp" />
<solid android:color="#59FFFFFF" />
<stroke android:width="5dp" android:color="@color/transparent"/>
</shape>
</item>
</selector>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="4dp" />
<solid android:color="@color/white" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<selector>
<item android:state_pressed="true">
<clip>
<shape>
<corners android:radius="4dp" />
<solid android:color="@color/white" />
<stroke android:width="1dp" android:color="@color/transparent"/>
</shape>
</clip>
</item>
<item>
<clip>
<shape>
<corners android:radius="4dp" />
<solid android:color="@color/white" />
<stroke android:width="5dp" android:color="@color/transparent"/>
</shape>
</clip>
</item>
</selector>
</item>
</layer-list>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="@color/white" />
<size android:width="6dp" android:height="8dp" />
<corners android:radius="2dp" />
</shape>
</item>
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="@color/white" />
<size android:width="8dp" android:height="8dp" />
<stroke android:width="2dp" android:color="@color/transparent" />
</shape>
</item>
</selector>
\ No newline at end of file
......@@ -9,4 +9,5 @@
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
......@@ -24,11 +24,44 @@
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"/>
android:focusableInTouchMode="true"
android:layout_centerInParent="true" />
<ImageView
android:id="@+id/ivImage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/ivPlay"
android:layout_width="76dp"
android:layout_height="76dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_video_play"
android:visibility="gone" />
</RelativeLayout>
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="120dp"
android:maxHeight="10dp"
android:paddingStart="0dp"
android:paddingTop="8dp"
android:paddingEnd="0dp"
android:paddingBottom="8dp"
android:visibility="gone"
android:progressDrawable="@drawable/consultant_seekbar_style"
android:splitTrack="false"
android:thumb="@drawable/consultant_seekbar_thumb"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<LinearLayout
android:id="@+id/btnLL"
......@@ -55,13 +88,32 @@
</LinearLayout>
<ProgressBar
<LinearLayout
android:id="@+id/loading"
style="@android:style/Widget.DeviceDefault.Light.ProgressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_width="103dp"
android:layout_height="103dp"
android:layout_centerInParent="true"
android:visibility="visible" />
android:background="@drawable/consultant_bg_loading"
android:gravity="center"
android:orientation="vertical">
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/platform_dp_10"
android:text="正在加载"
android:textColor="@color/white" />
</LinearLayout>
<ImageView
android:id="@+id/btnVideo"
......@@ -70,4 +122,18 @@
android:layout_centerInParent="true"
android:alpha="0"
android:visibility="gone" />
<ImageView
android:id="@+id/ivBack"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="24dp"
android:padding="4dp"
android:scaleType="fitXY"
android:src="@drawable/consultant_back"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment