Commit d207dc32 authored by jyx's avatar jyx

优化广告加载逻辑,修复部分内存泄漏问题

parent b3015b1b
......@@ -5,15 +5,12 @@ import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.TTAdNative
import com.bytedance.sdk.openadsdk.TTNativeExpressAd
import com.bytedance.sdk.openadsdk.TTRewardVideoAd
import com.mints.goodmoney.ad.video.BaseVideoAd
import com.mints.goodmoney.manager.TTNativeExpressManager
import com.mints.goodmoney.manager.TtCsjAdManager
import com.mints.goodmoney.utils.LogUtil
import com.mints.goodmoney.utils.ToastUtil
import com.mints.goodmoney.utils.UIUtils
import com.mints.goodmoney.utils.Utils
class CsjDrawAdManager private constructor(activity: Activity) : BaseVideoAd(activity) {
class CsjDrawAdManager private constructor(activity: Activity) {
companion object {
private var _inst: CsjDrawAdManager? = null
......@@ -27,17 +24,20 @@ class CsjDrawAdManager private constructor(activity: Activity) : BaseVideoAd(act
}
}
private var activity: Activity? = null
private var ttAdNative: TTAdNative? = null
private var mttRewardVideoAd: TTRewardVideoAd? = null
private var csjDrawAdListener: CsjDrawAdListener? = null
init {
this.activity = activity
val ttNativeExpressManager = TTNativeExpressManager()
ttAdNative = ttNativeExpressManager.initTTAd(false)
}
fun loadDrawNativeAd() {
//step3:创建广告请求参数AdSlot,具体参数含义参考文档
fun loadDrawNativeAd(activity: Activity, adCount: Int) {
this.activity = activity
//step3:创建广告请求参数AdSlot,具体参数含义参考文档
val expressViewWidth: Float = UIUtils.getScreenWidthDp(activity)
......@@ -46,7 +46,7 @@ class CsjDrawAdManager private constructor(activity: Activity) : BaseVideoAd(act
val adSlot = AdSlot.Builder()
.setCodeId(TtCsjAdManager.TT_AD_EXPRESS_DRAW)
.setExpressViewAcceptedSize(expressViewWidth, expressViewHeight)
.setAdCount(1) //请求广告数量为1到3条
.setAdCount(adCount) //请求广告数量为1到3条
.build()
mttRewardVideoAd?.setRewardAdInteractionListener(null)
......@@ -54,28 +54,27 @@ class CsjDrawAdManager private constructor(activity: Activity) : BaseVideoAd(act
//step4:请求广告,对请求回调的广告作渲染处理
ttAdNative?.loadExpressDrawFeedAd(adSlot, object : TTAdNative.NativeExpressAdListener {
override fun onError(p0: Int, p1: String?) {
ToastUtil.show(activity, p1)
// ToastUtil.show(activity, p1)
LogUtil.d(p1)
this@CsjDrawAdManager.csjDrawAdListener?.csjDrawAdFail()
}
override fun onNativeExpressAdLoad(p0: MutableList<TTNativeExpressAd>?) {
if (p0 == null || p0.isEmpty()) {
ToastUtil.show(activity, "未请求到广告")
// ToastUtil.show(activity, "未请求到广告")
this@CsjDrawAdManager.csjDrawAdListener?.csjDrawAdFail()
LogUtil.d("未请求到广告")
return
}
this@CsjDrawAdManager.csjDrawAdListener?.csjDrawAdSuccess(p0)
}
})
}
fun setCsjDrawAdListener(csjDrawAdListener: CsjDrawAdListener) {
fun setCsjDrawAdListener(csjDrawAdListener: CsjDrawAdListener?) {
this.csjDrawAdListener = csjDrawAdListener
}
......@@ -84,9 +83,8 @@ class CsjDrawAdManager private constructor(activity: Activity) : BaseVideoAd(act
fun csjDrawAdSuccess(data: MutableList<TTNativeExpressAd>?)
}
override fun onDestroy() {
super.onDestroy()
fun onDestroy() {
this.csjDrawAdListener = null
mttRewardVideoAd?.setRewardAdInteractionListener(null)
activity = null
......
......@@ -32,7 +32,6 @@ import com.mints.goodmoney.mvp.presenters.KylVideoPresenter
import com.mints.goodmoney.mvp.views.KylVideoView
import com.mints.goodmoney.ui.activitys.base.BaseActivity
import com.mints.goodmoney.ui.adapter.DrawVideoAdapter
import com.mints.goodmoney.utils.LogUtil
import com.mints.goodmoney.utils.MD5
import com.mints.goodmoney.utils.ToastUtil
import com.tbruyelle.rxpermissions.RxPermissions
......@@ -41,9 +40,8 @@ import kotlinx.android.synthetic.main.activity_draw_native_video.*
private const val TAG = "DrawNativeVideoActivity"
/**
* 描述:关于我们
* 作者:孟崔广
* 时间:2020/9/23 18:39
* 描述:讯飞彩铃展示
*
*/
class DrawNativeVideoActivity : BaseActivity(),
View.OnClickListener,
......@@ -58,11 +56,18 @@ class DrawNativeVideoActivity : BaseActivity(),
const val TYPE_COMMON_ITEM = 1
const val TYPE_AD_ITEM = 2
// 当前数据page
const val CURRENT_PAGE = "CURRENT_PAGE"
const val PLAY_INDEX = "PLAY_INDEX"
// 传递过来的数据
const val VIDEO_DATA = "VIDEO_DATA"
// 请求视频的target_id
const val TARGET_ID = "TARGET_ID"
// 广告插入数值
const val AD_INSERT_INDEX = 3
// 来电秀跳转地址
const val WEB_VIDEO_URL = "https://vring.kuyin123.com/friend/94aa83142f7f5c98?videoId="
}
......@@ -76,6 +81,9 @@ class DrawNativeVideoActivity : BaseActivity(),
// 当前播放视频数据源
private val drawDatas: MutableList<DrawVideoBean> = mutableListOf()
// 广告请求次数
private var mAdPage = 0
// targerId 视频类别
private var targerId = ""
......@@ -88,11 +96,11 @@ class DrawNativeVideoActivity : BaseActivity(),
private var mDrawVideoAdapter: DrawVideoAdapter? = null
private var mLayoutManager: ViewPagerLayoutManager? = null
private var mCsjDrawAdManager: CsjDrawAdManager? = null
override fun getBundleExtras(extras: Bundle?) {
super.getBundleExtras(extras)
mCurrentPage = extras?.getInt(CURRENT_PAGE, 0)!!
mCurrentPlayIndex = extras.getInt(PLAY_INDEX, 0)
targerId = extras.getString(TARGET_ID, "")
val parcelableArrayList = extras.getParcelableArrayList<DrawVideoBean>(VIDEO_DATA)
if (parcelableArrayList != null) {
......@@ -103,8 +111,8 @@ class DrawNativeVideoActivity : BaseActivity(),
override fun initViewsAndEvents() {
kylVideoPresenter.attachView(this)
CsjDrawAdManager.getInstance(this)?.loadDrawNativeAd()
CsjDrawAdManager.getInstance(this)?.setCsjDrawAdListener(this)
mCsjDrawAdManager = CsjDrawAdManager.getInstance(this)
mCsjDrawAdManager?.setCsjDrawAdListener(this)
mLayoutManager = ViewPagerLayoutManager(this, OrientationHelper.VERTICAL)
mDrawVideoAdapter = DrawVideoAdapter(this, drawDatas)
......@@ -113,6 +121,13 @@ class DrawNativeVideoActivity : BaseActivity(),
videoAdingManager = VideoAdingManager.getInstance(this)
// 第一次加载广告
if (drawDatas.size > AD_INSERT_INDEX) {
mCsjDrawAdManager?.loadDrawNativeAd(this, 2)
} else {
mCsjDrawAdManager?.loadDrawNativeAd(this, 1)
}
initListener()
}
......@@ -122,7 +137,7 @@ class DrawNativeVideoActivity : BaseActivity(),
R.id.ivBack -> finish()
R.id.ivVedio -> {
val bundle = Bundle()
bundle.putString(WebActivity.WEB_TITLE, "视频彩铃")
bundle.putString(WebActivity.WEB_TITLE, "开通视频彩铃")
bundle.putString(WebActivity.WEB_URL, WEB_VIDEO_URL + drawDatas[mCurrentPlayIndex].videoId)
readyGo(WebActivity::class.java, bundle)
}
......@@ -135,7 +150,7 @@ class DrawNativeVideoActivity : BaseActivity(),
.request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe { granted: Boolean ->
if (granted) {
downloadVedioToCacheFile()
downloadVideoToCacheFile()
awardVideo()
} else {
showMissingPermissionDialog("存储")
......@@ -169,7 +184,7 @@ class DrawNativeVideoActivity : BaseActivity(),
false
}
videoView.setOnPreparedListener { Log.e(TAG, "onPrepared") }
imgPlay.setOnClickListener(object : View.OnClickListener {
videoView.setOnClickListener(object : View.OnClickListener {
var isPlaying = true
override fun onClick(v: View) {
isPlaying = if (videoView.isPlaying) {
......@@ -187,7 +202,7 @@ class DrawNativeVideoActivity : BaseActivity(),
})
}
private fun downloadVedioToCacheFile() {
private fun downloadVideoToCacheFile() {
downloadVedioSucFlag = false
val videoPath: String = try {
getExternalFilesDir(null)!!.absolutePath + "/video/"
......@@ -285,13 +300,19 @@ class DrawNativeVideoActivity : BaseActivity(),
}
override fun onDestroy() {
mCsjDrawAdManager?.onDestroy()
videoAdingManager.setVideoAdingListener(null)
videoAdingManager.onDestory()
releaseVideo(mCurrentPlayIndex)
super.onDestroy()
if (downloadId != -1) {
PRDownloader.cancel(downloadId)
}
videoAdingManager.setVideoAdingListener(null)
videoAdingManager.onDestory()
kylVideoPresenter.detachView()
if (mLayoutManager != null) {
mLayoutManager!!.setOnViewPagerListener(null)
}
......@@ -365,8 +386,8 @@ class DrawNativeVideoActivity : BaseActivity(),
}
override fun csjDrawAdFail() {
LogUtil.d("csjDrawAdFail")
showToast("加载失败,请稍候再试")
finish()
}
override fun csjDrawAdSuccess(ads: MutableList<TTNativeExpressAd>?) {
......@@ -375,12 +396,6 @@ class DrawNativeVideoActivity : BaseActivity(),
return
}
// for (i in 0 until drawDatas.size) {
// val random = (Math.random() * 100).toInt()
// val index: Int = random % drawDatas.size
// drawDatas.add(DrawVideoBean(TYPE_COMMON_ITEM, null, drawDatas[index].ImgId, drawDatas[index].videoUrl, drawDatas[index].videoId))
// }
for (ad in ads) {
//点击监听器必须在getAdView之前调
ad.setVideoAdListener(object : TTNativeExpressAd.ExpressVideoAdListener {
......@@ -424,22 +439,18 @@ class DrawNativeVideoActivity : BaseActivity(),
}
override fun onRenderSuccess(p0: View?, p1: Float, p2: Float) {
// val random = (Math.random() * 100).toInt()
// var index = random % drawDatas.size
// if (index == 0) {
// index++
// }
val index = drawDatas.size / AD_INSERT_INDEX + mAdPage * AD_INSERT_INDEX
val index = drawDatas.size
drawDatas.add(index, DrawVideoBean(TYPE_AD_ITEM, ad, "", "", ""))
// mDrawVideoAdapter?.notifyDataSetChanged()
mAdPage++
}
})
ad.render()
}
// 列表只展示当前数据
mDrawVideoAdapter?.notifyItemInserted(mCurrentPlayIndex + 1)
}
......@@ -447,11 +458,11 @@ class DrawNativeVideoActivity : BaseActivity(),
for (data in data.data) {
drawDatas.add(DrawVideoBean(TYPE_COMMON_ITEM, null, data.vedioId, data.url, data.pvurl))
}
CsjDrawAdManager.getInstance(this)?.loadDrawNativeAd()
// mDrawVideoAdapter?.notifyDataSetChanged()
mCsjDrawAdManager?.loadDrawNativeAd(this, 1)
}
override fun getXfPageMsgFail() {
showToast("加载失败,请稍候再试")
finish()
}
}
......@@ -12,7 +12,6 @@ import com.mints.goodmoney.mvp.model.DrawVideoBean
import com.mints.goodmoney.ui.activitys.DrawNativeVideoActivity
import com.mints.goodmoney.ui.adapter.listener.OnItemClickListener
import com.mints.goodmoney.ui.widgets.FullScreenVideoView
import com.mints.goodmoney.utils.LogUtil
import com.mints.library.utils.GlideUtils
class DrawVideoAdapter(context: Context, datas: List<DrawVideoBean>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
......@@ -39,16 +38,9 @@ class DrawVideoAdapter(context: Context, datas: List<DrawVideoBean>) : RecyclerV
if (item?.type == DrawNativeVideoActivity.TYPE_COMMON_ITEM) {
GlideUtils.loadImageView(mContext, item.ImgId, holder.img_thumb)
view = getView()
LogUtil.d(DrawNativeVideoActivity.WEB_VIDEO_URL + mDrawVideoData[position].videoId)
(view as VideoView).setVideoURI(Uri.parse(mDrawVideoData[position].videoUrl))
} else if (item?.type == DrawNativeVideoActivity.TYPE_AD_ITEM && item.ad != null) {
view = item.ad.expressAdView
// if (item.ad.icon != null && item.ad.icon.imageUrl != null) {
// GlideUtils.loadImageView(mContext,)
// } else{
// holder.img_head_icon.setImageBitmap(item.ad.adLogo)
// }
}
holder.videoLayout.removeAllViews()
if (view?.parent != null) {
......
......@@ -70,7 +70,7 @@ class KuYinYuePageFragment(private val targetid: String) : LazyLoadBaseFragment(
// readyGo(KylVideoActivity::class.java, bundle)
val dataList = arrayListOf<DrawVideoBean>()
val index = position % PAGE_SIZE// 余数
val index = position % PAGE_SIZE // 余数
for (i in position until PAGE_SIZE * curPage) {
dataList.add(DrawVideoBean(DrawNativeVideoActivity.TYPE_COMMON_ITEM, null,
......@@ -81,7 +81,6 @@ class KuYinYuePageFragment(private val targetid: String) : LazyLoadBaseFragment(
val bundle = Bundle()
bundle.putParcelableArrayList(DrawNativeVideoActivity.VIDEO_DATA, dataList)
bundle.putInt(DrawNativeVideoActivity.PLAY_INDEX, index)
bundle.putInt(DrawNativeVideoActivity.CURRENT_PAGE, curPage)
bundle.putString(DrawNativeVideoActivity.TARGET_ID, targetid)
readyGo(DrawNativeVideoActivity::class.java, bundle)
......@@ -174,7 +173,7 @@ class KuYinYuePageFragment(private val targetid: String) : LazyLoadBaseFragment(
}
override fun onFeedAdLoad(ads: List<TTFeedAd>) {
if (ads == null || ads.isEmpty()) {
if (ads.isEmpty()) {
getXfPageMsg()
return
}
......
......@@ -15,11 +15,11 @@
<ImageView
android:id="@+id/ivBack"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="5dp"
android:layout_marginTop="15dp"
android:padding="16dp"
android:layout_marginTop="20dp"
android:padding="12dp"
android:src="@mipmap/ic_left_arrow" />
<TextView
......@@ -32,7 +32,7 @@
android:layout_marginBottom="100dp"
android:background="@drawable/shape_tv_gold"
android:gravity="center"
android:text="设视频彩铃"
android:text="设视频彩铃"
android:textColor="@color/white" />
<TextView
......
......@@ -12,22 +12,22 @@
android:clickable="false"
android:focusable="false" />
<ImageView
android:id="@+id/img_thumb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:scaleType="centerCrop" />
<ImageView
android:id="@+id/img_play"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:alpha="0"
android:clickable="true"
android:focusable="true"
android:clickable="false"
android:focusable="false"
android:src="@drawable/play_arrow" />
<ImageView
android:id="@+id/img_thumb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:focusable="false"
android:scaleType="fitXY" />
</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