package org.cuieney.videolife.ui.act;
import android.annotation.TargetApi;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.transition.Transition;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.shuyu.gsyvideoplayer.GSYVideoPlayer;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import org.cuieney.videolife.R;
import org.cuieney.videolife.common.utils.LogUtil;
import org.cuieney.videolife.entity.kaiyanBean.DataBean;
import org.cuieney.videolife.ui.video.OnTransitionListener;
import org.cuieney.videolife.ui.video.SampleVideo;
import org.cuieney.videolife.ui.video.SwitchVideoModel;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* 单独的视频播放页面
* Created by cuieney on 2016/11/11.
*/
public class PlayActivity extends AppCompatActivity {
public final static String IMG_TRANSITION = "IMG_TRANSITION";
public final static String TRANSITION = "TRANSITION";
public final static String DATA = "DATA";
@BindView(R.id.video_player)
SampleVideo videoPlayer;
OrientationUtils orientationUtils;
private boolean isTransition;
private Transition transition;
private DataBean dataBean;
private List<SwitchVideoModel> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
ButterKnife.bind(this);
isTransition = getIntent().getBooleanExtra(TRANSITION, false);
dataBean = getIntent().getExtras().getParcelable(DATA);
init();
}
private void init() {
list = new ArrayList<>();
new Thread(() -> {
try {
list.add(new SwitchVideoModel("普通",getRedirectUrl(dataBean.getPlayUrl())));
list.add(new SwitchVideoModel("高清",getRedirectUrl(dataBean.getPlayInfo().get(1).getUrl())));
videoPlayer.setUp(list, true, "");
} catch (Exception e) {
e.printStackTrace();
}
}).start();
//增加封面
ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// imageView.setImageResource(R.mipmap.xxx1);
videoPlayer.setThumbImageView(imageView);
//增加title
videoPlayer.getTitleTextView().setVisibility(View.VISIBLE);
videoPlayer.getTitleTextView().setText(dataBean.getTitle());
//设置返回键
videoPlayer.getBackButton().setVisibility(View.VISIBLE);
//设置旋转
orientationUtils = new OrientationUtils(this, videoPlayer);
//设置全屏按键功能
videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
orientationUtils.resolveByClick();
}
});
//videoPlayer.setBottomProgressBarDrawable(getResources().getDrawable(R.drawable.video_new_progress));
//videoPlayer.setDialogVolumeProgressBar(getResources().getDrawable(R.drawable.video_new_volume_progress_bg));
//videoPlayer.setDialogProgressBar(getResources().getDrawable(R.drawable.video_new_progress));
//videoPlayer.setBottomShowProgressBarDrawable(getResources().getDrawable(R.drawable.video_new_seekbar_progress),
//getResources().getDrawable(R.drawable.video_new_seekbar_thumb));
//videoPlayer.setDialogProgressColor(getResources().getColor(R.color.colorAccent), -11);
//是否可以滑动调整
videoPlayer.setIsTouchWiget(true);
//设置返回按键功能
videoPlayer.getBackButton().setOnClickListener(v -> onBackPressed());
//过渡动画
initTransition();
}
@Override
protected void onPause() {
super.onPause();
videoPlayer.onVideoPause();
}
@Override
protected void onResume() {
super.onResume();
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onDestroy() {
super.onDestroy();
if (orientationUtils != null)
orientationUtils.releaseListener();
}
@Override
public void onBackPressed() {
//先返回正常状态
if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
videoPlayer.getFullscreenButton().performClick();
return;
}
//释放所有
videoPlayer.setStandardVideoAllCallBack(null);
GSYVideoPlayer.releaseAllVideos();
if (isTransition && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
super.onBackPressed();
} else {
finish();
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
}
}
private void initTransition() {
if (isTransition && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
postponeEnterTransition();
ViewCompat.setTransitionName(videoPlayer, IMG_TRANSITION);
addTransitionListener();
startPostponedEnterTransition();
} else {
videoPlayer.startPlayLogic();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private boolean addTransitionListener() {
transition = getWindow().getSharedElementEnterTransition();
if (transition != null) {
transition.addListener(new OnTransitionListener(){
@Override
public void onTransitionEnd(Transition transition) {
super.onTransitionEnd(transition);
videoPlayer.startPlayLogic();
transition.removeListener(this);
}
});
return true;
}
return false;
}
private String getRedirectUrl(String path) throws Exception {
HttpURLConnection conn = (HttpURLConnection) new URL(path)
.openConnection();
conn.setInstanceFollowRedirects(false);
conn.setConnectTimeout(5000);
String headerField = conn.getHeaderField("Location");
Log.e("oye",headerField);
return headerField;
}
}