CoursePlayActivity.kt 27.7 KB
Newer Older
严久程 committed
1 2 3
package com.yidianling.course.coursePlay

import android.annotation.SuppressLint
严久程 committed
4
import android.app.AlertDialog
严久程 committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import android.content.Intent
import android.content.res.Configuration
import android.graphics.Color
import android.os.Build
import android.os.Handler
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.OrientationHelper
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
严久程 committed
21 22
import com.dou361.ijkplayer.widget.PlayStateParams
import com.dou361.ijkplayer.widget.PlayerView
严久程 committed
23
import com.lzf.easyfloat.enums.ShowPattern
严久程 committed
24
import com.lzf.easyfloat.permission.PermissionUtils
严久程 committed
25 26 27 28
import com.ydl.media.audio.AudioPlayer
import com.ydl.media.audio.utils.PlayProgressUtil
import com.ydl.media.view.PlayTypeEnum
import com.ydl.media.view.PlayerFloatHelper
严久程 committed
29 30
import com.ydl.ydl_image.module.GlideApp
import com.ydl.ydl_router.manager.YDLRouterManager
严久程 committed
31 32 33
import com.ydl.ydlcommon.adapter.CommonRecyclerAdapter
import com.ydl.ydlcommon.base.BaseActivity
import com.ydl.ydlcommon.bean.ShareData
34
import com.ydl.ydlcommon.data.PlatformDataManager
严久程 committed
35
import com.ydl.ydlcommon.modular.ModularServiceManager
严久程 committed
36 37 38 39 40 41 42 43
import com.ydl.ydlcommon.router.IYDLRouterConstant
import com.ydl.ydlcommon.router.YdlCommonRouterManager
import com.ydl.ydlcommon.utils.ScreenUtil
import com.ydl.ydlcommon.utils.StatusBarUtils
import com.ydl.ydlcommon.utils.Utils
import com.ydl.ydlcommon.view.BaseViewHolder
import com.ydl.ydlcommon.view.dialog.CommonDialog
import com.ydl.ydlcommon.view.dialog.YDLShareDialog
严久程 committed
44 45
import com.yidianling.common.tools.RxImageTool
import com.yidianling.common.tools.ToastUtil
46
import com.yidianling.course.BuildConfig
严久程 committed
47
import com.yidianling.course.R
严久程 committed
48 49
import com.yidianling.course.bean.CourseMediaBean
import com.yidianling.course.bean.CourseMediaDetailBean
严久程 committed
50
import com.yidianling.course.coursePlay.presenter.CoursePlayPresenter
严久程 committed
51
import com.yidianling.course.router.CourseIn
严久程 committed
52 53
import com.yidianling.course.widget.VideoFloatHelper
import com.yidianling.ydl_pay.CommonPayDialog
严久程 committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
import kotlinx.android.synthetic.main.activity_course_play.*
import org.json.JSONException
import org.json.JSONObject
import java.util.*

/**
 * 课程播放页面
 */
@Route(path = "/course/play")
class CoursePlayActivity : BaseActivity() {

    private val ROUTER_PARAMS = "routerParam"

    /*******************播放器模块*******************/
    //音视频模块
69 70
    var playAudioView: PlayViewInterface? = null
    var playVideoView: PlayViewInterface? = null
严久程 committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

    //当前播放模式  0音频播放  1视频播放
    var play_type = 0

    /**********************中间内容模块************************/
    var headview: CourseHeadView? = null

    var adapter: CommonRecyclerAdapter<CourseMediaBean>? = null
    //播放列表
    var playList: ArrayList<CourseMediaBean> = ArrayList()

    var presenter: CoursePlayPresenter? = null

    var index = 0 //当前播放下标

    //课程id
    var course_id = 0
    //是否全屏 0:否  1 全屏
    var fullScreen = 0
    var courPlayBean: CourseMediaDetailBean? = null
    //外部选中要播放的音/视频文件地址
    private var receiverPlayUrl: String? = ""

    var from: Int = 0
    private var handler = Handler()

    //视频播放器view
    var videoView: PlayerView? = null

    //视频是否在播放
    var isVideoPlay = false
    //TODO  播放状态统一
    var isShowAnim = true

    //是否是悬浮窗进入
    private var isFromFloatView = false

    private var isCancelNetCheck = false

    override fun layoutResId(): Int {
        return R.layout.activity_course_play
    }

    override fun initDataAndEvent() {
        //获取参数
        getParam()
        if (course_id == 0) {
            ToastUtil.toastShort("参数错误")
            finish()
        }

        presenter = CoursePlayPresenter(this)

        initView()
        initWindows()

        initData()

        nestedScrollView.scrollTo(0, 0)
    }

    fun initView() {
        iv_back.setOnClickListener {
            onBackPressed()
        }
        iv_share.setOnClickListener {
            //分享
            share(courPlayBean?.courseExtra?.shareData)
        }
        StatusBarUtils.setWindowStatusBarColor(mContext, R.color.transparent)

        tv_write_comment.setOnClickListener {
            if (!Utils.isFastClick()) {
                showVideoFloatView()
                CourseCommentActivity.startActivity(this@CoursePlayActivity, course_id.toString())
            }
        }
        text_price?.setOnClickListener {
            //参加课程
            addCourseOrder()
        }

        recy_list?.isNestedScrollingEnabled = false
        recy_list?.layoutManager = LinearLayoutManager(this, OrientationHelper.VERTICAL, false)
严久程 committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        adapter =
            CommonRecyclerAdapter<CourseMediaBean>(recy_list, R.layout.item_course_playlist_item)
                .setBindDataCallback(object :
                    CommonRecyclerAdapter.BindDataCallback<CourseMediaBean> {
                    override fun bindDatabindData(
                        holder: BaseViewHolder,
                        bean: CourseMediaBean,
                        position: Int
                    ) {
                        var gifView: ImageView = holder.getView(R.id.gifView)
                        var playView: ImageView = holder.getView(R.id.ic_play_view)
                        var bodyView: LinearLayout = holder.getView(R.id.lin_body)

                        if (index == position) {
                            gifView.visibility = View.VISIBLE
                            playView.visibility = View.GONE
                            bodyView.setBackgroundColor(this@CoursePlayActivity.resources.getColor(R.color.course_color_F8FCFF))
                            if (isShowAnim) {
                                Glide.with(this@CoursePlayActivity).asGif()
                                    .load(R.drawable.course_music).into(gifView)
                            } else {
                                Glide.with(this@CoursePlayActivity).asBitmap()
                                    .load(R.drawable.course_music).into(gifView)
                            }
                            holder.setTextColor(
                                R.id.text_title,
                                this@CoursePlayActivity.resources.getColor(R.color.main_theme)
                            )
严久程 committed
183
                        } else {
严久程 committed
184 185 186 187 188 189
                            gifView.visibility = View.GONE
                            playView.visibility = View.VISIBLE
                            bodyView.setBackgroundColor(this@CoursePlayActivity.resources.getColor(R.color.white))
                            if (courPlayBean?.courseExtra?.isBuy!!) {
                                GlideApp.with(this@CoursePlayActivity)
                                    .load(R.drawable.course_ico_kcml_play).into(playView)
严久程 committed
190
                            } else {
严久程 committed
191 192 193 194 195 196 197 198 199
                                if (bean.isDemo) {
                                    GlideApp.with(this@CoursePlayActivity)
                                        .load(R.drawable.course_ico_kcml_play)
                                        .into(playView)
                                } else {
                                    GlideApp.with(this@CoursePlayActivity)
                                        .load(R.drawable.course_ico_kcml_lock)
                                        .into(playView)
                                }
严久程 committed
200
                            }
严久程 committed
201 202 203 204
                            holder.setTextColor(
                                R.id.text_title,
                                this@CoursePlayActivity.resources.getColor(R.color.course_color_FF000000)
                            )
严久程 committed
205 206
                        }

严久程 committed
207 208 209 210 211 212
                        var test: TextView = holder.getView(R.id.text_test)
                        //试听标签
                        if (bean.isDemo) {
                            test.visibility = View.VISIBLE
                        } else {
                            test.visibility = View.GONE
严久程 committed
213
                        }
严久程 committed
214
                        holder.setText(R.id.text_title, bean.title)
严久程 committed
215 216


严久程 committed
217 218
                        holder.setOnClickListener(R.id.lin_body) {
                            if (position < 0) {
严久程 committed
219 220 221
                                return@setOnClickListener
                            }

严久程 committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
                            if (index == position) {
                            } else {
                                if (!bean.isDemo && !courPlayBean!!.courseExtra.isBuy) {
                                    CommonDialog(this@CoursePlayActivity)
                                        .setMessage("\n购买课程,获取完整课程内容\n")
                                        .setLeftOnclick("放弃") {

                                        }
                                        .setRightClick("购买") {
                                            //跳转支付页
                                            addCourseOrder()
                                        }
                                        .setCancelAble(false)
                                        .show()
                                    return@setOnClickListener
                                }
                                PlayProgressUtil.saveProgress(
                                    this@CoursePlayActivity,
                                    playList[index].url,
                                    0
                                )

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
                                updatePlayingListStatus(position)
//                                if (playList[index].mediaType == 2) {
//                                    play_type = 1
//                                    (playVideoView as CoursePlayItemViewVideo)?.play(position)
//                                }
//                                if (playList[index].mediaType == 1) {
//                                    play_type = 0
//                                    if (RxNetTool.isWifi(this@CoursePlayActivity)) {
//                                        (playAudioView as CoursePlayItemViewAudio).playView!!.play(
//                                            position
//                                        )
//                                    } else {
//                                        if ((playAudioView as CoursePlayItemViewAudio).hasEnsureNetStatus) {
//                                            (playAudioView as CoursePlayItemViewAudio).playView!!.play(
//                                                position
//                                            )
//                                        } else {
//                                            (playAudioView as CoursePlayItemViewAudio).setData(
//                                                position,
//                                                playList,
//                                                courPlayBean!!.courseExtra,
//                                                from
//                                            )
//                                        }
//                                    }
//                                }
//                                adapter?.notifyDataSetChanged()
严久程 committed
271 272 273
                            }
                        }
                    }
严久程 committed
274
                })
严久程 committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
        headview = CourseHeadView(this)
        //添加头部和底部view
        adapter!!.addHeadView(headview)
        recy_list.adapter = adapter
    }

    override fun onNewIntent(intent: Intent?) {
        var new_course_id = 0
        if (intent != null) {
            new_course_id = intent.getIntExtra("course_id", 0)
        }
        if (new_course_id == 0) {
            ToastUtil.toastShort("参数错误")
            finish()
        }
        if (new_course_id != course_id) {
            course_id = new_course_id
            initData()
        }
    }

    /**
     * 获取路由传递的参数
     */
    private fun getParam() {
        if (null == intent) {
            return
        }
严久程 committed
303
        if (intent.hasExtra(ROUTER_PARAMS)) {
严久程 committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
            //路由传递的入参
            val json = intent.getStringExtra(ROUTER_PARAMS)
            try {
                val jsonObject = JSONObject(json)
                //课程ID
                course_id = jsonObject.getInt(IYDLRouterConstant.EXTRA_ID)
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            return
        }
        //正常跳转所传参数
        course_id = intent.getIntExtra("course_id", 0)
        //mediaType是服务端新定义的媒体类型,1.音频 2.视频 ,与本地定义类型不同,先不改本地定义数据类型
        var mediaType = intent.getStringExtra("mediaType")
        if (TextUtils.equals(mediaType, "1")) {
            play_type = 0
        } else if (TextUtils.equals(mediaType, "2")) {
            play_type = 1
        }

        fullScreen = intent.getIntExtra("fullScreen", 0)
        receiverPlayUrl = intent.getStringExtra("coursePlayUrl")
        isFromFloatView = intent.getBooleanExtra("isFromFloatView", false)

        if (!TextUtils.isEmpty(receiverPlayUrl) && isFromFloatView) {
            isCancelNetCheck = true
        }

        try {
严久程 committed
334 335 336 337 338 339

            if (PlayerFloatHelper.playingType == PlayTypeEnum.PLAY_TYPE_COURSE
                && AudioPlayer.get().isPlaying
                && TextUtils.isEmpty(receiverPlayUrl)
            ) {
                receiverPlayUrl = AudioPlayer.get().playMusic?.path
严久程 committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353
            }
        } catch (e: Exception) {

        }

        from = intent.getIntExtra("from", 0)
    }

    private fun initWindows() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val window = window
            //获取状态栏高度
            var statusBarheight: Int = ScreenUtil.getStatusBarHeight(mContext)
            //设置边距
严久程 committed
354 355 356 357 358 359 360
            setMargins(
                title_bar_layout,
                RxImageTool.dp2px(15f),
                statusBarheight,
                RxImageTool.dp2px(15f),
                0
            )
严久程 committed
361 362 363
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.statusBarColor = Color.TRANSPARENT
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
严久程 committed
364 365
                window.decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
严久程 committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            } else {
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            }
        }
    }

    private fun setMargins(v: View?, l: Int, t: Int, r: Int, b: Int) {
        if (v != null) {
            if (v.layoutParams is ViewGroup.MarginLayoutParams) {
                val p = v.layoutParams as ViewGroup.MarginLayoutParams
                p.setMargins(l, t, r, b)
                v.requestLayout()
            }
        }
    }


    private fun initData() {
        //获取数据
        presenter?.getInitData(course_id)
    }

    //跟新view显示
    @SuppressLint("SetTextI18n")
    fun updateView(bean: CourseMediaDetailBean) {
        courPlayBean = bean
        playList.clear()
        setPlayList(bean)
        if (!TextUtils.isEmpty(receiverPlayUrl)) {
            for ((index, element) in playList.withIndex()) {
严久程 committed
397 398 399 400 401 402 403
                if (TextUtils.equals(receiverPlayUrl, element.url) || TextUtils.equals(
                        receiverPlayUrl!!.replace(
                            "http",
                            "https"
                        ), element.url
                    )
                ) {
严久程 committed
404 405 406 407 408 409 410 411 412 413 414 415 416
                    this@CoursePlayActivity.index = index
                }
            }
        }
        play_type = if (playList[index].mediaType == 1) {
            0
        } else {
            1
        }

        try {
            if (play_type == 1) {
                //视频播放销毁音频服务
严久程 committed
417
                AudioPlayer.get().stopPlayer()
严久程 committed
418
            }
严久程 committed
419 420 421 422 423

            if (PlayerFloatHelper.playingType == PlayTypeEnum.PLAY_TYPE_CONFIDE || PlayerFloatHelper.playingType == PlayTypeEnum.PLAY_TYPE_FM) {
                if (AudioPlayer.get().isPlaying) {
                    AudioPlayer.get().stopPlayer()
                }
严久程 committed
424 425 426 427 428
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
429 430 431 432 433 434 435 436 437 438
//        if (play_type == 1) {//视频
//            playVideoView = CoursePlayItemViewVideo.create(this, play_type)
//            (playVideoView as CoursePlayItemViewVideo).hasEnsureNetStatus = isCancelNetCheck
//            frame_video_play.addView(playVideoView as CoursePlayItemViewVideo)
//        } else {//音频
//            playAudioView = CoursePlayItemViewAudio.create(this, play_type)
//            (playAudioView as CoursePlayItemViewAudio).hasEnsureNetStatus = isCancelNetCheck
//            frame_audio_play.addView(playAudioView as CoursePlayItemViewVideo)
//        }

439 440 441
        //专家简介部分
        headview?.setData(bean.courseExtra)

442 443 444 445 446 447 448 449
        //视频
        playVideoView = CoursePlayItemViewVideo.create(this, play_type)
        (playVideoView as CoursePlayItemViewVideo).hasEnsureNetStatus = isCancelNetCheck
        frame_video_play.addView(playVideoView as CoursePlayItemViewVideo)

        //音频
        playAudioView = CoursePlayItemViewAudio.create(this, play_type)
        (playAudioView as CoursePlayItemViewAudio).hasEnsureNetStatus = isCancelNetCheck
450
        frame_audio_play.addView(playAudioView as CoursePlayItemViewAudio)
严久程 committed
451 452

        if (play_type == 0) {
453
            frame_audio_play.visibility = View.VISIBLE
454
            frame_video_play.visibility = View.GONE
严久程 committed
455
        } else {
456
            frame_video_play.visibility = View.VISIBLE
457
            frame_audio_play.visibility = View.GONE
严久程 committed
458 459
        }

460 461 462 463
        playAudioView?.setData(index, playList, bean.courseExtra, from, play_type == 0)
        playVideoView?.setData(index, playList, bean.courseExtra, from, play_type == 1)


严久程 committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
        if (videoView != null && play_type == 1 && fullScreen == 1) {
            videoView!!.toggleFullScreen()
            videoView!!.setNetWorkTypeTie(false)
            videoView!!.startPlay()
        }

        tv_title_full.text = bean.courseExtra.title
        text_price.text = "立即购买 ¥" + bean.courseExtra.applyFee

        if (bean.courseExtra.isBuy) {
            //已报名
            text_price.visibility = View.GONE
            tv_write_comment.visibility = View.VISIBLE
            //评论数量是大于 1,表示已经已经评论过,不再显示底部的评论区域
            if (bean.courseExtra.userCommentNum >= 1) {
                rl_write_bottom.visibility = View.GONE
            }
        } else {
            //未报名
            text_price.visibility = View.VISIBLE
            tv_write_comment.visibility = View.GONE
        }
    }

    //设置播放列表数据
    private fun setPlayList(bean: CourseMediaDetailBean) {
490
        if (bean.voiceSample.courseMedia.isNotEmpty()) {
严久程 committed
491 492 493 494 495
            for (voiceSampleData in bean.voiceSample.courseMedia) {
                voiceSampleData.isDemo = true
                playList.add(voiceSampleData)
            }
        }
496
        if (bean.voiceWhole.courseMedia.isNotEmpty()) {
严久程 committed
497 498 499 500 501 502 503 504 505 506
            for (voiceCourseData in bean.voiceWhole.courseMedia) {
                playList.add(voiceCourseData)
            }
        }
        adapter!!.update(playList)
    }


    fun updatePlayingListStatus(index: Int) {
        if (index < playList.size && this.index != index) {
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
            if (!playList[index].isDemo && !courPlayBean!!.courseExtra.isBuy) {
                if (AudioPlayer.get().isPlaying) {
                    AudioPlayer.get().playPause()
                }

                if (videoView != null) {
                    videoView?.pausePlay()
                }

                CommonDialog(this@CoursePlayActivity)
                    .setMessage("\n购买课程,获取完整课程内容\n")
                    .setLeftOnclick("放弃") {

                    }
                    .setRightClick("购买") {
                        //跳转支付页
                        addCourseOrder()
                    }
                    .setCancelAble(false)
                    .show()
                return
            }
529
            if (playList[index].mediaType == 1) {
530
                play_type = 0
531 532 533 534 535
                frame_video_play.visibility = View.GONE
                frame_audio_play.visibility = View.VISIBLE
                (playAudioView as CoursePlayItemViewAudio).playView?.play(
                    index
                )
536 537 538
                if (videoView != null) {
                    videoView?.pausePlay()
                }
539 540 541
            }

            if (playList[index].mediaType == 2) {
542
                play_type = 1
543 544 545
                frame_video_play.visibility = View.VISIBLE
                frame_audio_play.visibility = View.GONE
                (playVideoView as CoursePlayItemViewVideo)?.play(index)
546 547 548 549

                if (AudioPlayer.get().isPlaying) {
                    AudioPlayer.get().playPause()
                }
550 551
            }

严久程 committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
            this.index = index
            adapter?.notifyDataSetChanged()
        }
    }


    fun showLoadingDialog() {
        showProgressDialog(null)
    }

    fun dismissLoadingDialog() {
        dismissProgressDialog()
    }


    //参加课程
    fun addCourseOrder() {
        if (course_id == 0) {
            ToastUtil.toastShort("请退出页面重试")
            return
        }
        val userInfo = YdlCommonRouterManager.getYdlCommonRoute().getUserInfo()
        if (userInfo == null || TextUtils.isEmpty(userInfo.userId)) {
            YDLRouterManager.router(IYDLRouterConstant.ROUTER_MINE_LOGIN)
            return
        }

        val build = CommonPayDialog.Build(mContext)
严久程 committed
580 581 582
            .setCourseId(course_id.toString())
            .setToken(userInfo.token)
            .setUid(userInfo.userId)
583
            .setFfrom(PlatformDataManager.getRam().getChannelName())
严久程 committed
584 585 586 587 588
            .setListener(object : CommonPayDialog.OnPayResultListener {
                override fun onSuccesed() {
                    //刷新界面
                    initData()
                }
严久程 committed
589

严久程 committed
590
                override fun onFailed() {
严久程 committed
591

严久程 committed
592 593 594
                }
            })
            .setIsTestEnvironment(BuildConfig.DEBUG)
严久程 committed
595 596 597 598 599 600 601 602 603 604 605
        build.build().show()
    }

    /**
     * 分享
     */
    private fun share(share: ShareData?) {
        if (share == null) {
            ToastUtil.toastShort("缺少参数")
            return
        }
严久程 committed
606 607 608 609 610 611 612 613 614
        val dialog = YDLShareDialog.style1(
            this,
            share.title,
            share.share_url,
            share.desc,
            share.cover,
            "",
            ""
        )
严久程 committed
615 616 617
        dialog.setCallBack(object : YDLShareDialog.ICallBack {
            override fun callBack(type: Int) {
                if (1 == type) {
严久程 committed
618 619 620 621
                    ModularServiceManager.getPlatformUserService()

                    if (CourseIn.isLogin()) {
                        CourseIn.loginWayIntent(this@CoursePlayActivity)
严久程 committed
622 623 624 625 626 627 628 629 630 631 632 633
                    } else {
                        //分享到心事动态
                        toTrend(share)
                    }
                }
            }
        })
        dialog.show(fragmentManager, "lose")
    }

    private fun toTrend(share: ShareData) {
        var url = if (TextUtils.isEmpty(share.url)) share.share_url else share.url
634 635
        CourseIn.getDynamicService()
            .publishArticleToTrend(this@CoursePlayActivity, url!!, share.cover!!, share.title!!)
严久程 committed
636 637 638 639 640
    }

    override fun onPause() {
        super.onPause()
        videoView?.onPause()
641 642
        playVideoView?.onPause()
        playAudioView?.onPause()
严久程 committed
643 644 645 646
    }

    override fun onResume() {
        super.onResume()
647 648
        playVideoView?.onResume()
        playAudioView?.onResume()
严久程 committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

        if (PlayerFloatHelper.isShow(this)) {
            PlayerFloatHelper.removeView(this)
        }
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        if (play_type == 1) {
            if (newConfig.screenHeightDp > newConfig.screenWidthDp) {
                //显示底部评论按钮
                rl_write_bottom.visibility = View.VISIBLE
                tv_title_full.visibility = View.GONE
                nestedScrollView.isVerticalFadingEdgeEnabled = true
                nestedScrollView.isVerticalScrollBarEnabled = true
                recy_list.visibility = View.VISIBLE
                videoView?.hideBack(true)
            } else {
                rl_write_bottom.visibility = View.GONE
                tv_title_full.visibility = View.VISIBLE
                nestedScrollView.isVerticalFadingEdgeEnabled = false
                nestedScrollView.isVerticalScrollBarEnabled = false
                recy_list.visibility = View.GONE
                videoView?.hideBack(false)
            }
            videoView?.onConfigurationChanged(newConfig)
            handler.postDelayed({
                if (newConfig.screenHeightDp > newConfig.screenWidthDp) {
严久程 committed
677
                    videoView?.fullScreenView!!.setImageResource(R.drawable.course_ico_play_float_full)
严久程 committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
                } else {
                    videoView?.fullScreenView!!.setImageResource(R.drawable.ico_play_minify)
                }
            }, 300)
        }
    }

    override fun onBackPressed() {
        if (showVideoFloatView()) {
            super.onBackPressed()
        }
    }

    fun showVideoFloatView(): Boolean {
        if (play_type == 0) {
严久程 committed
693 694

            if (AudioPlayer.get().isPlaying && PlayerFloatHelper.playingType == PlayTypeEnum.PLAY_TYPE_COURSE) {
严久程 committed
695 696 697
                AudioPlayer.get().playMusic?.coverPath = courPlayBean?.courseExtra?.pic
                AudioPlayer.get().playMusic?.artist = courPlayBean?.courseExtra?.doctorName

严久程 committed
698 699
                PlayerFloatHelper.playTempData.clear()

严久程 committed
700 701
                val hashMap = HashMap<String, String>()
                hashMap["course_id"] = course_id.toString()
严久程 committed
702
                PlayerFloatHelper.playTempData.putAll(hashMap)
严久程 committed
703
            }
严久程 committed
704 705


严久程 committed
706 707 708 709 710 711 712 713 714
        } else {
            if (videoView?.onBackPressed() == true) {
                return false
            }
            if (isVideoPlay) {
                var status = Utils.getValue(videoView, "status")
                //视频课程正在播放中
                if (status == PlayStateParams.STATE_PLAYING) {

严久程 committed
715 716
                    VideoFloatHelper.setVideoInfo(
                        course_id.toString(),
717
                        (playVideoView as CoursePlayItemViewVideo).videoUrl!!,
严久程 committed
718 719
                        true
                    )
严久程 committed
720
                    VideoFloatHelper.defaultShowPattern = ShowPattern.FOREGROUND
严久程 committed
721
                    checkPermission()
严久程 committed
722 723 724 725 726 727
                }
            }
        }
        return true
    }

严久程 committed
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

    /**
     * 检测浮窗权限是否开启,若没有给与申请提示框(非必须,申请依旧是EasyFloat内部内保进行)
     */
    private fun checkPermission() {
        if (PermissionUtils.checkPermission(this)) {

            VideoFloatHelper.showVideoFloat(this)
        } else {
            AlertDialog.Builder(this)
                .setMessage("使用浮窗功能,需要您授权悬浮窗权限。")
                .setPositiveButton("去开启") { _, _ ->
                    VideoFloatHelper.showVideoFloat(this)
                }
                .setNegativeButton("取消") { _, _ ->
                    ToastUtil.toastLong(
                        this,
                        "App正常工作需要内部存储使用权限,请开启"
                    )
                }
                .show()
        }
    }

严久程 committed
752
    override fun onDestroy() {
严久程 committed
753
        super.onDestroy()
严久程 committed
754
        videoView?.onDestroy()
755 756 757 758
        playAudioView?.onDestroy()
        playVideoView?.onDestroy()
        playAudioView = null
        playVideoView = null
严久程 committed
759 760
    }
}