[宣传视频] refactor: 宣传视频播放重构,B2乘客屏使用common中组件,同时activity因为涉及网络请求不适合放入单独进程,先放入主进程;
This commit is contained in:
@@ -7,7 +7,6 @@
|
||||
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|screenLayout|fontScale|uiMode|orientation|screenSize|smallestScreenSize"
|
||||
android:enabled="true"
|
||||
android:exported="true"
|
||||
android:process=":media"
|
||||
android:resizeableActivity="false"
|
||||
android:resumeWhilePausing="true"
|
||||
android:screenOrientation="landscape"
|
||||
|
||||
@@ -48,8 +48,6 @@ object MediaDataSourceManager {
|
||||
|
||||
private val driverSn: String
|
||||
get() {
|
||||
//TODO
|
||||
// return "202305107384MW6"
|
||||
val serverToken = CallerTelematicManager.getServerToken()
|
||||
if (serverToken != driverSnCache && serverToken.isNotEmpty()) {
|
||||
driverSnCache = serverToken
|
||||
@@ -116,7 +114,7 @@ object MediaDataSourceManager {
|
||||
override fun onSuccess(data: MediaDataResp?) {
|
||||
mHasEverGetMediaDataFromMis = true
|
||||
CallerLogger.e(TAG,
|
||||
"startGetMediaDataSourceLoop:success, 从管理后台获取到数据,AdData=${
|
||||
"startGetMediaDataSourceLoop:success, 从管理后台获取到数据,MediaData=${
|
||||
GsonUtils.toJson(
|
||||
data
|
||||
)
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.mogo.och.bus.passenger.presenter
|
||||
|
||||
import com.mogo.commons.mvp.Presenter
|
||||
import com.mogo.och.bus.passenger.ui.video.PM2VideoFragment
|
||||
|
||||
class PM2VideoPresenter(view: PM2VideoFragment?) :
|
||||
Presenter<PM2VideoFragment?>(view)
|
||||
@@ -1,11 +1,9 @@
|
||||
package com.mogo.och.bus.passenger.ui
|
||||
|
||||
import android.provider.Settings
|
||||
import android.view.Surface
|
||||
import com.mogo.commons.mvp.MvpFragment
|
||||
import com.mogo.och.bus.passenger.R
|
||||
import com.mogo.och.bus.passenger.presenter.PM2Presenter
|
||||
import com.mogo.och.bus.passenger.ui.video.PM2VideoFragment
|
||||
import com.mogo.och.common.module.wigets.media.MediaPlayerFragment
|
||||
|
||||
/**
|
||||
* @author: wangmingjun
|
||||
@@ -16,7 +14,7 @@ class PM2BaseFragment :
|
||||
|
||||
private var drivingFragment : PM2DrivingInfoFragment? = null
|
||||
private var hdMapFragment : PM2HPMapFragment? = null
|
||||
private var videoFragment : PM2VideoFragment? = null
|
||||
private var mediaFragment : MediaPlayerFragment? = null
|
||||
|
||||
override fun getLayoutId(): Int {
|
||||
return R.layout.p_m2_fragment
|
||||
@@ -57,9 +55,9 @@ class PM2BaseFragment :
|
||||
childFragmentManager.beginTransaction().add(R.id.hd_map_fragment, hdMapFragment!!)
|
||||
.show(hdMapFragment!!).commitAllowingStateLoss()
|
||||
|
||||
if (videoFragment == null) videoFragment = PM2VideoFragment()
|
||||
childFragmentManager.beginTransaction().add(R.id.video_fragment, videoFragment!!)
|
||||
.show(videoFragment!!).commitAllowingStateLoss()
|
||||
if (mediaFragment == null) mediaFragment = MediaPlayerFragment()
|
||||
childFragmentManager.beginTransaction().add(R.id.video_fragment, mediaFragment!!)
|
||||
.show(mediaFragment!!).commitAllowingStateLoss()
|
||||
}
|
||||
|
||||
override fun createPresenter(): PM2Presenter {
|
||||
@@ -67,6 +65,6 @@ class PM2BaseFragment :
|
||||
}
|
||||
|
||||
companion object {
|
||||
public val TAG = PM2BaseFragment::class.java.simpleName
|
||||
val TAG = PM2BaseFragment::class.java.simpleName
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package com.mogo.och.bus.passenger.ui.video
|
||||
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import com.mogo.commons.mvp.MvpFragment
|
||||
import com.mogo.eagle.core.data.config.FunctionBuildConfig
|
||||
import com.mogo.eagle.core.utilcode.util.GsonUtils
|
||||
import com.mogo.och.bus.passenger.R
|
||||
import com.mogo.och.bus.passenger.presenter.PM2VideoPresenter
|
||||
import com.mogo.och.common.module.wigets.media.MediaDataList
|
||||
import com.mogo.och.common.module.wigets.media.MediaItem
|
||||
import kotlinx.android.synthetic.m2.p_m2_video_fragment.*
|
||||
|
||||
/**
|
||||
* @author: wangmingjun
|
||||
* @date: 2022/4/12
|
||||
*/
|
||||
class PM2VideoFragment :
|
||||
MvpFragment<PM2VideoFragment?, PM2VideoPresenter?>() {
|
||||
|
||||
private var arrayListOf = mutableListOf<MediaItem>()
|
||||
|
||||
override fun getLayoutId(): Int {
|
||||
return R.layout.p_m2_video_fragment
|
||||
}
|
||||
|
||||
|
||||
override fun createPresenter(): PM2VideoPresenter {
|
||||
return PM2VideoPresenter(this)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val TAG = PM2VideoFragment::class.java.simpleName
|
||||
}
|
||||
|
||||
override fun getTagName(): String {
|
||||
return TAG
|
||||
}
|
||||
|
||||
override fun initViews() {
|
||||
initResourceData()
|
||||
imageVideoRotationView.setData(arrayListOf)
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
imageVideoRotationView.setPause()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
imageVideoRotationView.setResume()
|
||||
}
|
||||
|
||||
private fun initResourceData() {
|
||||
|
||||
try {
|
||||
arrayListOf.clear()
|
||||
var datas: MediaDataList = GsonUtils.fromJson(FunctionBuildConfig.mediaUrlConfig,object : TypeToken<MediaDataList>() {}.type)
|
||||
arrayListOf.addAll(datas.medias)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
package com.mogo.och.bus.passenger.ui.widget.video
|
||||
|
||||
import android.content.Context
|
||||
import android.media.AudioManager
|
||||
import android.util.AttributeSet
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.Logger
|
||||
import com.mogo.eagle.core.widget.media.video.TextureVideoViewOutlineProvider
|
||||
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
|
||||
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
|
||||
import me.jessyan.autosize.utils.AutoSizeUtils
|
||||
|
||||
/**
|
||||
* @author: wangmingjun
|
||||
* @date: 2023/2/17
|
||||
* 隐藏所有控件的player
|
||||
*/
|
||||
class AdvanceGSYVideoPlayer: StandardGSYVideoPlayer {
|
||||
constructor(context: Context?) : super(context)
|
||||
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
|
||||
|
||||
init {
|
||||
hideWidget()
|
||||
GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_16_9)
|
||||
GSYVideoType.setRenderType(GSYVideoType.GLSURFACE)
|
||||
}
|
||||
|
||||
override fun hideAllWidget() {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "hideAllWidget")
|
||||
// hideWidget()
|
||||
}
|
||||
|
||||
override fun changeUiToNormal() {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "changeUiToNormal-hide")
|
||||
hideWidget()
|
||||
}
|
||||
|
||||
override fun changeUiToPreparingShow() {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "changeUiToPreparingShow-hide")
|
||||
hideWidget()
|
||||
}
|
||||
|
||||
override fun changeUiToPlayingShow() {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "changeUiToPlayingShow")
|
||||
setCacheImageViewGone()
|
||||
}
|
||||
|
||||
override fun changeUiToPauseShow() {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "changeUiToPauseShow-hide")
|
||||
startPlayLogic()
|
||||
// hideWidget()
|
||||
}
|
||||
|
||||
override fun changeUiToCompleteShow() {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "changeUiToCompleteShow")
|
||||
setCacheImageViewGone()
|
||||
}
|
||||
|
||||
override fun changeUiToPlayingBufferingShow() {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "changeUiToPlayingBufferingShow -hide")
|
||||
hideWidget()
|
||||
}
|
||||
|
||||
override fun changeUiToError() {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "changeUiToError-hide")
|
||||
hideWidget()
|
||||
}
|
||||
|
||||
private fun hideWidget(){
|
||||
setViewShowState(mBottomContainer, INVISIBLE)
|
||||
setViewShowState(mProgressBar, INVISIBLE)
|
||||
setViewShowState(mCurrentTimeTextView, INVISIBLE)
|
||||
setViewShowState(mTotalTimeTextView, INVISIBLE)
|
||||
setViewShowState(mBottomProgressBar, INVISIBLE)
|
||||
setViewShowState(mBackButton, INVISIBLE)
|
||||
setViewShowState(mStartButton, INVISIBLE)
|
||||
|
||||
setViewShowState(mThumbImageViewLayout, VISIBLE)
|
||||
setViewShowState(mThumbImageView, VISIBLE)
|
||||
|
||||
setViewShowState(mTopContainer, INVISIBLE)
|
||||
|
||||
setViewShowState(mLoadingProgressBar, INVISIBLE)
|
||||
setViewShowState(
|
||||
mLockScreen, INVISIBLE
|
||||
)
|
||||
|
||||
setIsTouchWiget(false)
|
||||
isFocusableInTouchMode = false
|
||||
}
|
||||
|
||||
fun setCacheImageViewVisible() {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "CacheImageViewVISIBLE")
|
||||
setViewShowState(mThumbImageViewLayout, VISIBLE)
|
||||
// setViewShowState(mThumbImageView, VISIBLE)
|
||||
}
|
||||
|
||||
fun setCacheImageViewGone() {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "CacheImageViewGONE")
|
||||
setViewShowState(mThumbImageViewLayout, INVISIBLE)
|
||||
// setViewShowState(mThumbImageView, INVISIBLE)
|
||||
}
|
||||
|
||||
//失去焦点声音压低
|
||||
override fun onLossTransientCanDuck() {
|
||||
// setStreamVolume(0.2f)
|
||||
setNeedMute(true)
|
||||
}
|
||||
|
||||
//获取焦点声音恢复
|
||||
override fun onGankAudio() {
|
||||
// setStreamVolume(5.0f)
|
||||
setNeedMute(false)
|
||||
}
|
||||
|
||||
private fun setStreamVolume(percent: Float){
|
||||
var mAudioManager = mContext?.getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
||||
var maxVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
|
||||
var volume = (percent * maxVolume).toInt()
|
||||
if (volume < 0 ){
|
||||
volume = 0
|
||||
}
|
||||
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,volume,0)
|
||||
}
|
||||
|
||||
private fun setNeedMute(isMute: Boolean){
|
||||
gsyVideoManager?.player?.setNeedMute(isMute)
|
||||
}
|
||||
|
||||
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
|
||||
super.onSizeChanged(w, h, oldw, oldh)
|
||||
if (!mIfCurrentIsFullscreen) {
|
||||
val dp2px = AutoSizeUtils.dp2px(context, 16f)
|
||||
this.outlineProvider = TextureVideoViewOutlineProvider(dp2px.toFloat())
|
||||
this.clipToOutline = true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package com.mogo.och.bus.passenger.ui.widget.video
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.widget.ImageView
|
||||
import android.widget.RelativeLayout
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.request.RequestOptions
|
||||
import com.mogo.och.bus.passenger.R
|
||||
|
||||
/**
|
||||
* @author: wangmingjun
|
||||
* @date: 2023/2/6
|
||||
*/
|
||||
class AdvanceImageView @JvmOverloads constructor(
|
||||
context: Context, attrs: AttributeSet? = null
|
||||
) : RelativeLayout(context, attrs) {
|
||||
|
||||
private var imageView: ImageView? = null
|
||||
|
||||
init {
|
||||
initView()
|
||||
}
|
||||
|
||||
private fun initView() {
|
||||
imageView = ImageView(context)
|
||||
imageView?.scaleType = ImageView.ScaleType.FIT_XY
|
||||
addView(imageView, LayoutParams(-1, -1))
|
||||
}
|
||||
|
||||
@SuppressLint("CheckResult")
|
||||
fun setImagePath(path: String){
|
||||
imageView?.setImageResource(R.drawable.m2_p_video_holder)
|
||||
imageView?.let { Glide.with(context).asBitmap().load(path)
|
||||
.apply(
|
||||
RequestOptions().useUnlimitedSourceGeneratorsPool(true)
|
||||
.placeholder(R.drawable.m2_p_video_holder)
|
||||
.error(R.drawable.m2_p_video_holder)
|
||||
.fallback(R.drawable.m2_p_video_holder)
|
||||
.centerCrop()
|
||||
)
|
||||
.into(it) }
|
||||
}
|
||||
}
|
||||
@@ -1,211 +0,0 @@
|
||||
package com.mogo.och.bus.passenger.ui.widget.video
|
||||
|
||||
import android.content.Context
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.viewpager.widget.PagerAdapter
|
||||
import androidx.viewpager.widget.ViewPager
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger.d
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.Logger
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant
|
||||
import com.mogo.eagle.core.utilcode.util.CountDownTimer
|
||||
import com.mogo.och.common.module.wigets.media.MediaItem
|
||||
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
|
||||
|
||||
/**
|
||||
* @author: wangmingjun
|
||||
* @date: 2023/2/6
|
||||
*/
|
||||
class AdvancePagerAdapter(context: Context, viewPager: ViewPager) : PagerAdapter(),
|
||||
ViewPager.OnPageChangeListener {
|
||||
|
||||
private val mContext: Context = context
|
||||
private val mViewPager: ViewPager = viewPager
|
||||
|
||||
private var dataList = mutableListOf<MediaItem>()
|
||||
private var viewList = mutableListOf<View>()
|
||||
|
||||
private var lastPosition = -1
|
||||
|
||||
private var current = 0
|
||||
private val time = 5000
|
||||
private var pause = false
|
||||
private var countDownTimer: CountDownTimer? = null
|
||||
|
||||
fun setData(list: MutableList<MediaItem>) {
|
||||
if (list.isEmpty()) return
|
||||
dataList.addAll(list)
|
||||
|
||||
viewList.clear()
|
||||
|
||||
list.forEach {
|
||||
addView(it)
|
||||
}
|
||||
|
||||
mViewPager.addOnPageChangeListener(this)
|
||||
|
||||
notifyDataSetChanged()
|
||||
|
||||
mViewPager.currentItem = 0
|
||||
|
||||
if (viewList.size > 0) {
|
||||
if (viewList[mViewPager.currentItem] is AdvanceVideoView) {//有人反应第一个是视频不播放这边优化了一下
|
||||
Logger.d(ImageAndVideoRotation.TAG, "第一个是视频")
|
||||
val video = viewList[mViewPager.currentItem] as AdvanceVideoView
|
||||
video.setVideo(gsySampleCallBack)
|
||||
|
||||
} else if (viewList[mViewPager.currentItem] is AdvanceImageView) {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "startTimer()_1")
|
||||
current = 0//换页重新计算时间
|
||||
startTimer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getCount(): Int {
|
||||
return dataList.size
|
||||
}
|
||||
|
||||
override fun isViewFromObject(view: View, `object`: Any): Boolean {
|
||||
return view === `object`
|
||||
}
|
||||
|
||||
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
|
||||
container.removeView(viewList[position])
|
||||
}
|
||||
|
||||
override fun instantiateItem(container: ViewGroup, position: Int): Any {
|
||||
val view: View = viewList[position]
|
||||
container.addView(view)
|
||||
return view
|
||||
|
||||
}
|
||||
|
||||
override fun getItemPosition(`object`: Any): Int {
|
||||
return POSITION_NONE
|
||||
}
|
||||
|
||||
private fun addView(item: MediaItem) {
|
||||
if (item.fileType == 1) { // 表示视频
|
||||
val videoView = AdvanceVideoView(mContext)
|
||||
videoView.setVideoPath(item.fileUrl,item.coverImageUrl)
|
||||
viewList.add(videoView)
|
||||
} else { // 表示图片
|
||||
val imageView = AdvanceImageView(mContext)
|
||||
imageView.setImagePath(item.fileUrl)
|
||||
viewList.add(imageView)
|
||||
}
|
||||
}
|
||||
|
||||
fun setPause() {
|
||||
pause = true
|
||||
if (viewList.size > 0 && viewList[mViewPager.currentItem] is AdvanceVideoView) {
|
||||
val videoView = viewList[mViewPager.currentItem] as AdvanceVideoView
|
||||
videoView.setPause()
|
||||
}
|
||||
}
|
||||
|
||||
fun setResume() {
|
||||
pause = false
|
||||
if (viewList.size > 0 && viewList[mViewPager.currentItem] is AdvanceVideoView) {
|
||||
val videoView = viewList[mViewPager.currentItem] as AdvanceVideoView
|
||||
videoView.setResume()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
|
||||
}
|
||||
|
||||
override fun onPageSelected(position: Int) {
|
||||
}
|
||||
|
||||
override fun onPageScrollStateChanged(state: Int) {
|
||||
// 由于viewpager的预加载机制onPageSelected这里面加载videoview 放的跟玩一样 等操作完成后再播放videoview就香了 很丝滑
|
||||
if (state == 0) { //静止,什么都没做
|
||||
val currentItem = mViewPager.currentItem
|
||||
Logger.d(ImageAndVideoRotation.TAG,
|
||||
"state = $state currentItem = $currentItem lastPosition = $lastPosition")
|
||||
|
||||
if (viewList.size > 1) { //多于1,才会循环跳转
|
||||
|
||||
if (viewList[mViewPager.currentItem] is AdvanceVideoView) {
|
||||
|
||||
val videoView = (viewList[mViewPager.currentItem] as AdvanceVideoView)
|
||||
videoView.setCacheImageViewVisible()
|
||||
videoView.setVideo(gsySampleCallBack)
|
||||
|
||||
} else if (viewList[mViewPager.currentItem] is AdvanceImageView) {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "startTimer()")
|
||||
current = 0//换页重新计算时间
|
||||
startTimer()
|
||||
}
|
||||
lastPosition = mViewPager.currentItem
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var gsySampleCallBack = object : GSYSampleCallBack() {
|
||||
|
||||
override fun onPrepared(url: String?, vararg objects: Any?) {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "onPrepared--$url")
|
||||
}
|
||||
|
||||
override fun onAutoComplete(url: String?, vararg objects: Any?) {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "onAutoComplete()-$url")
|
||||
if (viewList[mViewPager.currentItem] is AdvanceVideoView){
|
||||
val videoView = (viewList[mViewPager.currentItem] as AdvanceVideoView)
|
||||
if (viewList.size == 1){
|
||||
videoView.startPlay(url)
|
||||
}else{
|
||||
videoView.onVideoReset()
|
||||
goNextItemView()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPlayError(url: String?, vararg objects: Any?) {
|
||||
super.onPlayError(url, *objects)
|
||||
Logger.d(ImageAndVideoRotation.TAG, "onPlayError()-$url")
|
||||
if (viewList[mViewPager.currentItem] is AdvanceVideoView){
|
||||
val videoView = (viewList[mViewPager.currentItem] as AdvanceVideoView)
|
||||
videoView.onVideoReset()
|
||||
// videoView.setCacheImageViewVisible()
|
||||
videoView.clearLocalErrorVideo()
|
||||
goNextItemView()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun startTimer() {
|
||||
if (countDownTimer != null){
|
||||
countDownTimer?.cancel()
|
||||
countDownTimer = null
|
||||
}
|
||||
countDownTimer = object : CountDownTimer(5000,1000){
|
||||
override fun onTick(millisUntilFinished: Long) {
|
||||
d(SceneConstant.M_BUS_P + "startTimer", "倒计时秒 = ${millisUntilFinished/1000}" )
|
||||
}
|
||||
|
||||
override fun onFinish() {
|
||||
d(ImageAndVideoRotation.TAG+ "startTimer", "5s到,跳转")
|
||||
goNextItemView()
|
||||
}
|
||||
|
||||
}.start()
|
||||
}
|
||||
|
||||
/**
|
||||
* view 跳转
|
||||
*/
|
||||
private fun goNextItemView() {
|
||||
if (mViewPager.currentItem == viewList.size - 1) {//已经到最后一个
|
||||
mViewPager.post {
|
||||
mViewPager.setCurrentItem(0, true)
|
||||
}
|
||||
} else {
|
||||
mViewPager.post {
|
||||
mViewPager.setCurrentItem(mViewPager.currentItem + 1, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,254 +0,0 @@
|
||||
package com.mogo.och.bus.passenger.ui.widget.video
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.util.AttributeSet
|
||||
import android.widget.ImageView
|
||||
import android.widget.RelativeLayout
|
||||
import com.mogo.eagle.core.utilcode.download.*
|
||||
import com.mogo.eagle.core.utilcode.download.callback.*
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.Logger
|
||||
import com.mogo.eagle.core.utilcode.util.FileUtils
|
||||
import com.mogo.eagle.core.utilcode.util.ThreadUtils
|
||||
import com.mogo.eagle.core.utilcode.util.UiThreadHandler
|
||||
import com.mogo.och.bus.passenger.R
|
||||
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
|
||||
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* @author: wangmingjun
|
||||
* @date: 2023/2/8
|
||||
*/
|
||||
class AdvanceVideoView @JvmOverloads constructor(
|
||||
context: Context, attrs: AttributeSet? = null
|
||||
) : RelativeLayout(context, attrs) {
|
||||
|
||||
private var videoRelativeLayout: RelativeLayout? = null
|
||||
private var cacheImage: ImageView? = null
|
||||
|
||||
private var videoViewPlayer: AdvanceGSYVideoPlayer? = null
|
||||
private var gsyVideoOptionBuilder: GSYVideoOptionBuilder? = null
|
||||
private var mOnCompletionListener: GSYSampleCallBack? = null
|
||||
private var downloadVideoName = ""
|
||||
private var fileNetPath: String? = ""
|
||||
private var cacheImageUrl: String? = ""
|
||||
private var mVideoDirPath: String? = ""
|
||||
|
||||
init {
|
||||
mVideoDirPath = context.filesDir.absolutePath + File.separator + "video" + File.separator
|
||||
// mVideoDirPath = Config.downLoadPath
|
||||
initView()
|
||||
}
|
||||
|
||||
private fun initView() {
|
||||
initVideoView()
|
||||
initCacheImgView()
|
||||
}
|
||||
|
||||
private fun initCacheImgView() {
|
||||
cacheImage = ImageView(context)
|
||||
cacheImage?.scaleType = ImageView.ScaleType.FIT_XY
|
||||
// addView(cacheImage, LayoutParams(-1, -1))
|
||||
}
|
||||
|
||||
private fun initVideoView() {
|
||||
videoRelativeLayout = RelativeLayout(context)
|
||||
val outLayout = LayoutParams(-1, -1)
|
||||
addView(videoRelativeLayout, outLayout)
|
||||
|
||||
if (videoViewPlayer === null) {
|
||||
//视频播放控件
|
||||
videoViewPlayer = AdvanceGSYVideoPlayer(context)
|
||||
}
|
||||
|
||||
val layoutParams = LayoutParams(-1, -1)
|
||||
//设置videoview占满父view播放
|
||||
layoutParams.addRule(ALIGN_PARENT_LEFT)
|
||||
layoutParams.addRule(ALIGN_PARENT_RIGHT)
|
||||
layoutParams.addRule(ALIGN_PARENT_TOP)
|
||||
layoutParams.addRule(ALIGN_PARENT_BOTTOM)
|
||||
|
||||
videoRelativeLayout?.addView(videoViewPlayer, layoutParams)
|
||||
}
|
||||
|
||||
fun setVideoPath(path: String, cacheImageUrl: String) {
|
||||
// https://img.zhidaohulian.com/fileServer/online_car_hailing/1676357834634/5.m4v
|
||||
// https://img.zhidaohulian.com/fileServer/online_car_hailing/1676360274126/10.mp4
|
||||
this.fileNetPath = path
|
||||
this.cacheImageUrl = cacheImageUrl
|
||||
val pathList = path.split("/")
|
||||
if (pathList.isNotEmpty()) {
|
||||
this.downloadVideoName = pathList[pathList.size - 1]
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadCacheImg() {
|
||||
// BitmapHelper.getVideoThumbnail(path) /*获取第一帧图*
|
||||
// OCHThreadPoolManager.getsInstance().execute {
|
||||
// var bitmap = BitmapHelper.getVideoThumbnail(fileNetPath)
|
||||
Logger.d(ImageAndVideoRotation.TAG, "setVideoPath")
|
||||
// ThreadUtils.runOnUiThread {
|
||||
// Logger.d(ImageAndVideoRotation.TAG, "bitmap加载")
|
||||
cacheImage?.setImageResource(R.drawable.m2_p_video_holder)
|
||||
// cacheImage?.let { //暂时去掉加载首帧图,加载视频时,用本地默认图
|
||||
// Glide.with(context).asBitmap().load(cacheImageUrl)
|
||||
// .apply(
|
||||
// RequestOptions().useUnlimitedSourceGeneratorsPool(true)
|
||||
// .placeholder(R.drawable.m2_p_video_holder)
|
||||
// .error(R.drawable.m2_p_video_holder)
|
||||
// .fallback(R.drawable.m2_p_video_holder)
|
||||
// .centerCrop()
|
||||
// )
|
||||
// .into(it)
|
||||
// }
|
||||
videoViewPlayer?.thumbImageView = cacheImage
|
||||
// setCacheImageViewVisible()
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
fun clearLocalErrorVideo() {
|
||||
if (downloadVideoName.isNotEmpty()
|
||||
&& FileUtils.isFileExists(mVideoDirPath + downloadVideoName)
|
||||
) {
|
||||
FileUtils.delete(mVideoDirPath + downloadVideoName)
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("CheckResult")
|
||||
fun setCacheImageViewVisible() {
|
||||
UiThreadHandler.post {
|
||||
// cacheImage?.visibility = VISIBLE
|
||||
videoViewPlayer?.setCacheImageViewVisible()
|
||||
}
|
||||
}
|
||||
|
||||
fun setCacheImageViewGone() {
|
||||
UiThreadHandler.post {
|
||||
// cacheImage?.visibility = GONE
|
||||
videoViewPlayer?.setCacheImageViewGone()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun setVideo(onCompletionListener: GSYSampleCallBack) {
|
||||
loadCacheImg()
|
||||
Logger.d(ImageAndVideoRotation.TAG, "setVideo")
|
||||
mOnCompletionListener = onCompletionListener
|
||||
//判断是否已经下载
|
||||
if (downloadVideoName.isNotEmpty()) {
|
||||
Logger.d(
|
||||
ImageAndVideoRotation.TAG,
|
||||
"video local url = $mVideoDirPath$downloadVideoName"
|
||||
)
|
||||
if (FileUtils.isFileExists(mVideoDirPath + downloadVideoName)) {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "have cache startPlay")
|
||||
startPlay(Uri.fromFile(File(mVideoDirPath + downloadVideoName)).toString())
|
||||
return
|
||||
}
|
||||
startDownLoadVideo()
|
||||
}
|
||||
}
|
||||
|
||||
private fun startDownLoadVideo() {
|
||||
//下载视频, 下载成功后再播放
|
||||
Logger.d(ImageAndVideoRotation.TAG, "startDownLoadVideo")
|
||||
FileUtils.createFileDir(mVideoDirPath)
|
||||
val downloadUrl = fileNetPath
|
||||
val downloadDir = mVideoDirPath
|
||||
if (downloadUrl != null && downloadDir != null) {
|
||||
DownloadUtils.downLoad(
|
||||
context, downloadUrl, downloadDir, downloadVideoName, downListener
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun startPlay(localVideoPath: String?) {
|
||||
if (localVideoPath === "") return
|
||||
try {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "startPlay")
|
||||
gsyVideoOptionBuilder = GSYVideoOptionBuilder()
|
||||
gsyVideoOptionBuilder
|
||||
// ?.setUrl("file:///mnt/sdcard/downloads/$downloadVideoName")
|
||||
?.setUrl(localVideoPath) // "/data/user/0/com.mogo.launcher.f/files/video/"
|
||||
?.setPlayTag(downloadVideoName)
|
||||
?.setCacheWithPlay(false)
|
||||
?.setThumbPlay(false)
|
||||
?.build(videoViewPlayer)
|
||||
|
||||
videoViewPlayer?.isFocusableInTouchMode = false
|
||||
videoViewPlayer?.setVideoAllCallBack(mOnCompletionListener)
|
||||
videoViewPlayer?.startPlayLogic()
|
||||
} catch (e: Exception) {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "startPlay e = ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
fun onVideoReset() {
|
||||
videoViewPlayer?.onVideoReset()
|
||||
mOnCompletionListener = null
|
||||
}
|
||||
|
||||
fun setPause() {
|
||||
if (videoViewPlayer !== null) {
|
||||
videoViewPlayer?.onVideoPause()
|
||||
}
|
||||
}
|
||||
|
||||
fun setResume() {
|
||||
if (videoViewPlayer !== null) {
|
||||
videoViewPlayer?.onVideoResume()
|
||||
}
|
||||
}
|
||||
|
||||
private val downListener = object : IDownloadListener {
|
||||
override fun onStart(url: String) {
|
||||
setCacheImageViewVisible()
|
||||
Logger.d(ImageAndVideoRotation.TAG, "download-onStart")
|
||||
}
|
||||
|
||||
// override fun onPause(url: String, threadBean: ThreadBean?) {
|
||||
// Logger.d(ImageAndVideoRotation.TAG, "download-onPause")
|
||||
//// UiThreadHandler.postDelayed(Runnable {
|
||||
//// startDownLoadVideo()
|
||||
//// },DOWNLOAD_DELAY)
|
||||
// // todo 测试下网络断掉是否会走onpause,且网络回复也不会继续下载
|
||||
// }
|
||||
|
||||
override fun onProgress(url: String, downloaded: Long, total: Long) {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "download-onProgress== ${ (downloaded * 1.0f * 100/total).toInt() }")
|
||||
}
|
||||
|
||||
override fun onFinished(url: String, path: String) {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "download-onFinished = $url")
|
||||
if (url.equals(fileNetPath)) { //发现下载工具在断网又连网后,已完成的任务又都下载,跳转播放出现问题
|
||||
//下载完成
|
||||
ThreadUtils.runOnUiThread {
|
||||
startPlay(Uri.fromFile(File(path)).toString())
|
||||
}
|
||||
} else {//如果当前文件不存在再次去下载当前的
|
||||
Logger.d(
|
||||
ImageAndVideoRotation.TAG, "download-onFinished = not current" +
|
||||
",currentUrl = $fileNetPath "
|
||||
)
|
||||
if (FileUtils.isFileExists(path)) {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "have download startPlay")
|
||||
ThreadUtils.runOnUiThread {
|
||||
startPlay(Uri.fromFile(File(path)).toString())
|
||||
}
|
||||
return
|
||||
} else {
|
||||
startDownLoadVideo()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onError(url: String, error: String?) {
|
||||
Logger.d(ImageAndVideoRotation.TAG, "download-onError-$error")
|
||||
//出错再次下载
|
||||
startDownLoadVideo()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.mogo.och.bus.passenger.ui.widget.video
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.MotionEvent
|
||||
import androidx.viewpager.widget.ViewPager
|
||||
|
||||
/**
|
||||
* @author: wangmingjun
|
||||
* @date: 2023/2/21
|
||||
*/
|
||||
class AdvanceViewPager: ViewPager{
|
||||
|
||||
constructor(context: Context) : super(context)
|
||||
constructor(context: Context,attrs: AttributeSet?) : super(context,attrs)
|
||||
|
||||
override fun onTouchEvent(ev: MotionEvent?): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package com.mogo.och.bus.passenger.ui.widget.video
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.widget.RelativeLayout
|
||||
import com.mogo.och.common.module.wigets.media.MediaItem
|
||||
|
||||
/**
|
||||
* @author: wangmingjun
|
||||
* @date: 2023/2/6
|
||||
*/
|
||||
class ImageAndVideoRotation @JvmOverloads constructor(
|
||||
context: Context, attrs: AttributeSet? = null
|
||||
) : RelativeLayout(context, attrs) {
|
||||
|
||||
private var viewPager: AdvanceViewPager? = null
|
||||
private var pagerAdapter: AdvancePagerAdapter? = null
|
||||
|
||||
companion object {
|
||||
const val TAG = "ImageAndVideoRotation"
|
||||
}
|
||||
|
||||
init {
|
||||
initView()
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
private fun initView() {
|
||||
viewPager = AdvanceViewPager(context)
|
||||
pagerAdapter = AdvancePagerAdapter(context, viewPager!!)
|
||||
viewPager?.adapter = pagerAdapter
|
||||
|
||||
addView(viewPager, LayoutParams(-1, -1))
|
||||
}
|
||||
|
||||
fun setData(list: MutableList<MediaItem>){
|
||||
pagerAdapter?.setData(list)
|
||||
}
|
||||
|
||||
fun setPause(){
|
||||
pagerAdapter?.setPause()
|
||||
}
|
||||
|
||||
fun setResume(){
|
||||
pagerAdapter?.setResume()
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<!-- 图片或视频广告-->
|
||||
|
||||
<com.mogo.och.bus.passenger.ui.widget.video.ImageAndVideoRotation
|
||||
android:id="@+id/imageVideoRotationView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
Reference in New Issue
Block a user