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
import android.content.Intent
import android.content.res.Configuration
import android.graphics.Color
import android.os.Build
import android.os.Handler
YKai committed
10 11
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.OrientationHelper
严久程 committed
12 13 14 15 16 17 18 19 20
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
import com.yidianling.course.widget.VideoFloatHelper
53
import com.yidianling.ydl_pay.common.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

    //当前播放模式  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
YKai committed
154 155 156
        recy_list?.layoutManager =
            LinearLayoutManager(
                this,
霍志良 committed
157
                LinearLayoutManager.VERTICAL,
YKai committed
158 159
                false
            )
严久程 committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        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,
186
                                this@CoursePlayActivity.resources.getColor(R.color.platform_main_theme)
严久程 committed
187
                            )
严久程 committed
188
                        } else {
严久程 committed
189 190 191 192 193 194
                            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
195
                            } else {
严久程 committed
196 197 198 199 200 201 202 203 204
                                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
205
                            }
严久程 committed
206 207 208 209
                            holder.setTextColor(
                                R.id.text_title,
                                this@CoursePlayActivity.resources.getColor(R.color.course_color_FF000000)
                            )
严久程 committed
210 211
                        }

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


严久程 committed
222 223
                        holder.setOnClickListener(R.id.lin_body) {
                            if (position < 0) {
严久程 committed
224 225 226
                                return@setOnClickListener
                            }

严久程 committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
                            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
                                )

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
                                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
276 277 278
                            }
                        }
                    }
严久程 committed
279
                })
严久程 committed
280 281 282 283 284 285 286
        headview = CourseHeadView(this)
        //添加头部和底部view
        adapter!!.addHeadView(headview)
        recy_list.adapter = adapter
    }

    override fun onNewIntent(intent: Intent?) {
霍志良 committed
287
        super.onNewIntent(intent)
严久程 committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
        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
309
        if (intent.hasExtra(ROUTER_PARAMS)) {
严久程 committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
            //路由传递的入参
            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
340 341 342 343 344 345

            if (PlayerFloatHelper.playingType == PlayTypeEnum.PLAY_TYPE_COURSE
                && AudioPlayer.get().isPlaying
                && TextUtils.isEmpty(receiverPlayUrl)
            ) {
                receiverPlayUrl = AudioPlayer.get().playMusic?.path
严久程 committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359
            }
        } 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
360 361 362 363 364 365 366
            setMargins(
                title_bar_layout,
                RxImageTool.dp2px(15f),
                statusBarheight,
                RxImageTool.dp2px(15f),
                0
            )
严久程 committed
367 368 369
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.statusBarColor = Color.TRANSPARENT
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
严久程 committed
370 371
                window.decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
严久程 committed
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 397 398 399 400 401 402
                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
403 404 405 406 407 408 409
                if (TextUtils.equals(receiverPlayUrl, element.url) || TextUtils.equals(
                        receiverPlayUrl!!.replace(
                            "http",
                            "https"
                        ), element.url
                    )
                ) {
严久程 committed
410 411 412 413 414 415 416 417 418 419 420 421 422
                    this@CoursePlayActivity.index = index
                }
            }
        }
        play_type = if (playList[index].mediaType == 1) {
            0
        } else {
            1
        }

        try {
            if (play_type == 1) {
                //视频播放销毁音频服务
严久程 committed
423
                AudioPlayer.get().stopPlayer()
严久程 committed
424
            }
严久程 committed
425 426 427 428 429

            if (PlayerFloatHelper.playingType == PlayTypeEnum.PLAY_TYPE_CONFIDE || PlayerFloatHelper.playingType == PlayTypeEnum.PLAY_TYPE_FM) {
                if (AudioPlayer.get().isPlaying) {
                    AudioPlayer.get().stopPlayer()
                }
严久程 committed
430 431 432 433 434
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
435 436 437 438 439 440 441 442 443 444
//        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)
//        }

445 446 447
        //专家简介部分
        headview?.setData(bean.courseExtra)

448 449 450 451 452 453 454 455
        //视频
        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
456
        frame_audio_play.addView(playAudioView as CoursePlayItemViewAudio)
严久程 committed
457 458

        if (play_type == 0) {
459
            frame_audio_play.visibility = View.VISIBLE
460
            frame_video_play.visibility = View.GONE
严久程 committed
461
        } else {
462
            frame_video_play.visibility = View.VISIBLE
463
            frame_audio_play.visibility = View.GONE
严久程 committed
464 465
        }

466 467 468 469
        playAudioView?.setData(index, playList, bean.courseExtra, from, play_type == 0)
        playVideoView?.setData(index, playList, bean.courseExtra, from, play_type == 1)


严久程 committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
        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) {
496
        if (bean.voiceSample.courseMedia.isNotEmpty()) {
严久程 committed
497 498 499 500 501
            for (voiceSampleData in bean.voiceSample.courseMedia) {
                voiceSampleData.isDemo = true
                playList.add(voiceSampleData)
            }
        }
502
        if (bean.voiceWhole.courseMedia.isNotEmpty()) {
严久程 committed
503 504 505 506 507 508 509 510 511 512
            for (voiceCourseData in bean.voiceWhole.courseMedia) {
                playList.add(voiceCourseData)
            }
        }
        adapter!!.update(playList)
    }


    fun updatePlayingListStatus(index: Int) {
        if (index < playList.size && this.index != index) {
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
            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
            }
535
            if (playList[index].mediaType == 1) {
536
                play_type = 0
537 538 539 540 541
                frame_video_play.visibility = View.GONE
                frame_audio_play.visibility = View.VISIBLE
                (playAudioView as CoursePlayItemViewAudio).playView?.play(
                    index
                )
542 543 544
                if (videoView != null) {
                    videoView?.pausePlay()
                }
545 546 547
            }

            if (playList[index].mediaType == 2) {
548
                play_type = 1
549 550
                frame_video_play.visibility = View.VISIBLE
                frame_audio_play.visibility = View.GONE
551 552 553
                if (AudioPlayer.get().isPlaying) {
                    AudioPlayer.get().playPause()
                }
554
                (playVideoView as CoursePlayItemViewVideo)?.play(index)
555 556
            }

严久程 committed
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)) {
580
            CourseIn.loginByOneKeyLogin(mContext,true)
严久程 committed
581 582 583 584
            return
        }

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

严久程 committed
595
                override fun onFailed() {
严久程 committed
596

严久程 committed
597 598 599
                }
            })
            .setIsTestEnvironment(BuildConfig.DEBUG)
严久程 committed
600 601 602 603 604 605 606 607 608 609 610
        build.build().show()
    }

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

625
                    if (CourseIn.loginByOneKeyLogin(this@CoursePlayActivity,true)) {
严久程 committed
626 627 628 629 630 631 632 633 634 635 636
                        //分享到心事动态
                        toTrend(share)
                    }
                }
            }
        })
        dialog.show(fragmentManager, "lose")
    }

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

    override fun onPause() {
        super.onPause()
        videoView?.onPause()
644 645
        playVideoView?.onPause()
        playAudioView?.onPause()
严久程 committed
646 647 648 649
    }

    override fun onResume() {
        super.onResume()
650 651
        playVideoView?.onResume()
        playAudioView?.onResume()
严久程 committed
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 677 678 679

        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
680
                    videoView?.fullScreenView!!.setImageResource(R.drawable.course_ico_play_float_full)
严久程 committed
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
                } 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
696 697

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

严久程 committed
701 702
                PlayerFloatHelper.playTempData.clear()

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


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

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

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

    /**
     * 检测浮窗权限是否开启,若没有给与申请提示框(非必须,申请依旧是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
755
    override fun onDestroy() {
严久程 committed
756
        super.onDestroy()
严久程 committed
757
        videoView?.onDestroy()
758 759 760 761
        playAudioView?.onDestroy()
        playVideoView?.onDestroy()
        playAudioView = null
        playVideoView = null
严久程 committed
762 763
    }
}