| | |
| | | package com.sinata.xqmuse |
| | | |
| | | import android.content.Intent |
| | | import android.os.Handler |
| | | import android.os.Looper |
| | | import android.os.Message |
| | | import android.util.Log |
| | | import androidx.fragment.app.Fragment |
| | | import androidx.fragment.app.FragmentPagerAdapter |
| | | import cn.sinata.xldutils.gone |
| | | import cn.sinata.xldutils.utils.SPUtils |
| | | import cn.sinata.xldutils.visible |
| | | import com.flyco.tablayout.listener.CustomTabEntity |
| | | import com.flyco.tablayout.listener.OnTabSelectListener |
| | | import com.github.zackratos.ultimatebar.UltimateBar |
| | | import com.google.gson.Gson |
| | | import com.sinata.xqmuse.dialog.TipDialog |
| | | import com.sinata.xqmuse.network.HttpManager |
| | | import com.sinata.xqmuse.network.entity.UserInfo |
| | | import com.sinata.xqmuse.network.entity.VoiceDetail |
| | | import com.sinata.xqmuse.network.entity.req.ReqAnswer |
| | | import com.sinata.xqmuse.network.request |
| | | import com.sinata.xqmuse.ui.TransparentStatusBarActivity |
| | | import com.sinata.xqmuse.ui.course.CourseFragment |
| | | import com.sinata.xqmuse.ui.discovery.DiscoveryFragment |
| | | import com.sinata.xqmuse.ui.guide.GuideActivity |
| | | import com.sinata.xqmuse.ui.home.HomeFragment |
| | | import com.sinata.xqmuse.ui.login.LoginActivity |
| | | import com.sinata.xqmuse.ui.mine.MineFragment |
| | | import com.sinata.xqmuse.ui.mine.ShareActivity |
| | | import com.sinata.xqmuse.ui.tree.TreeFragment |
| | | import com.sinata.xqmuse.utils.AudioUtils |
| | | import com.sinata.xqmuse.utils.Const |
| | | import com.sinata.xqmuse.utils.event.EmptyEvent |
| | | import kotlinx.android.synthetic.main.activity_main.* |
| | | import kotlinx.android.synthetic.main.fragment_home.* |
| | | import org.greenrobot.eventbus.EventBus |
| | | import org.greenrobot.eventbus.Subscribe |
| | | import org.jetbrains.anko.startActivity |
| | | import org.jetbrains.anko.toast |
| | | import xyz.doikki.videoplayer.player.VideoView |
| | | |
| | | class MainActivity : TransparentStatusBarActivity(), OnTabSelectListener{ |
| | | class MainActivity : TransparentStatusBarActivity(), OnTabSelectListener,AudioUtils.OnAudioStatusUpdateListener { |
| | | override fun setContentView() = R.layout.activity_main |
| | | |
| | | private val fragments = arrayListOf<Fragment>() |
| | | |
| | | var teacherVideoView:VideoView? = null |
| | | private var bgPlayer:AudioUtils? = null//背景音乐播放器 |
| | | private var thinkBgPlayer:AudioUtils? = null//冥想背景音播放器 |
| | | private var thinkPlayer:AudioUtils? = null//冥想背景音播放器 |
| | | |
| | | private var guideAudio:String? = null |
| | | private var guidePlayer:AudioUtils? = null//引导音频播放器 |
| | | |
| | | private var thinkHandler:Handler? = null //冥想时间相关 |
| | | private var inGuide = false //true 引导中... |
| | | |
| | | private val isFirst by lazy { intent.getBooleanExtra("isFirst",false) } //首次安装,需要到导师引导页 |
| | | |
| | | private val MSG_PROGRESS = 0//疗愈进度+1 |
| | | private val MSG_TODAY = 1//今日疗愈更新计时 |
| | | private val MSG_COUNTDOWN = 2//疗愈倒计时 |
| | | |
| | | private var lastTodayTime = 0L //上一次今日疗愈获取的时间 |
| | | |
| | | private var startTime = 0L //开始疗愈的时间戳 |
| | | |
| | | override fun initClick() { |
| | | player_close.setOnClickListener { |
| | | TipDialog.show(supportFragmentManager,"是否关闭当前音频?",object :TipDialog.OnClickCallback{ |
| | | override fun onOk() { |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.FINISH_THINK)) |
| | | } |
| | | |
| | | override fun onCancel() { |
| | | } |
| | | },"确认","取消") |
| | | } |
| | | |
| | | cl_player.setOnClickListener { |
| | | voice?.goDetail(this) |
| | | } |
| | | |
| | | player_play.setOnClickListener { |
| | | playing = !playing |
| | | if (playing){ |
| | | player_play.setImageResource(R.mipmap.player_pause) |
| | | thinkBgPlayer?.resume() |
| | | thinkPlayer?.resume() |
| | | thinkHandler?.sendEmptyMessage(MSG_PROGRESS) |
| | | startTime = System.currentTimeMillis() |
| | | }else{ |
| | | player_play.setImageResource(R.mipmap.player_start) |
| | | thinkBgPlayer?.pause() |
| | | thinkPlayer?.pause() |
| | | thinkHandler?.removeMessages(MSG_PROGRESS) |
| | | saveThinkRecord() |
| | | } |
| | | } |
| | | } |
| | | |
| | | override fun initView() { |
| | | EventBus.getDefault().register(this) |
| | | titleBar.gone() |
| | | useWhiteTitle() |
| | | startBgm() |
| | | initTab() |
| | | if (!intent.getStringExtra("code").isNullOrEmpty()){ |
| | | startActivity<ShareActivity>("code" to intent.getStringExtra("code")) |
| | | } |
| | | if (isFirst) //解决首次启动eventbus注册慢的问题 |
| | | startActivity<GuideActivity>() |
| | | |
| | | thinkHandler = object :Handler(Looper.myLooper()!!){ |
| | | override fun handleMessage(msg: Message) { |
| | | super.handleMessage(msg) |
| | | when(msg.what){ |
| | | MSG_PROGRESS->{ |
| | | currentPosition = thinkBgPlayer?.currentPosition ?:0 |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.GOT_THINK_POSITION)) |
| | | sendEmptyMessageDelayed(MSG_PROGRESS,1000) |
| | | } |
| | | MSG_COUNTDOWN->{ |
| | | if (System.currentTimeMillis()>= finishTime) |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.FINISH_THINK)) |
| | | else |
| | | sendEmptyMessageDelayed(MSG_COUNTDOWN,1000) |
| | | } |
| | | MSG_TODAY->{ |
| | | if (System.currentTimeMillis() - lastTodayTime > 60000) { //距离上次刷新过去了1分钟 |
| | | Log.e(Const.Tag,"已经过1分钟,需要重新获取今日疗愈数据") |
| | | lastTodayTime = System.currentTimeMillis() |
| | | (fragments[0] as HomeFragment).getToday() |
| | | } |
| | | sendEmptyMessageDelayed(MSG_TODAY,5000) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | lastTodayTime = System.currentTimeMillis() |
| | | startTodayCheck() |
| | | } |
| | | |
| | | private fun startGuide() { |
| | | if (guidePlayer == null) |
| | | guidePlayer = AudioUtils() |
| | | if (guideAudio.isNullOrEmpty()) |
| | | HttpManager.getPlan().request(this){_,data-> |
| | | guideAudio = data |
| | | if (inGuide){ |
| | | guidePlayer?.setVolume(0.6f) |
| | | guidePlayer?.loopPlayMusic(this,data) |
| | | } |
| | | } |
| | | else{ |
| | | guidePlayer?.setVolume(0.6f) |
| | | guidePlayer?.loopPlayMusic(this,guideAudio) |
| | | } |
| | | } |
| | | |
| | | private fun startBgm() { |
| | | val bgm = SPUtils.instance().getString(Const.User.BGM) |
| | | if (!bgm.isNullOrEmpty()){ |
| | | if (bgPlayer == null) |
| | | bgPlayer = AudioUtils() |
| | | val volume = SPUtils.instance().getInt(Const.User.VOLUME,50) |
| | | bgPlayer?.setVolume(volume.toFloat()/100) |
| | | bgPlayer?.loopPlayMusic(this,bgm) |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 开始冥想 |
| | | */ |
| | | private fun startThink() { |
| | | bgPlayer?.pause() |
| | | index = 0 |
| | | if (voice?.meditationMusicList?.isNullOrEmpty() == false){ |
| | | if (thinkBgPlayer == null){ |
| | | thinkBgPlayer = AudioUtils() |
| | | thinkPlayer = AudioUtils() |
| | | thinkBgPlayer!!.setOnAudioStatusUpdateListener(this) |
| | | thinkPlayer!!.setOnAudioStatusUpdateListener(object :AudioUtils.OnAudioStatusUpdateListener{ |
| | | override fun onUpdate(db: Double, time: Long) { |
| | | } |
| | | |
| | | override fun onStop(filePath: String?) { |
| | | } |
| | | |
| | | override fun onStartPlay() { |
| | | } |
| | | |
| | | override fun onFinishPlay() { |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.FINISH_THINK)) |
| | | } |
| | | |
| | | override fun onGetDuration(duration: Int) { |
| | | } |
| | | }) |
| | | } |
| | | val volume = SPUtils.instance().getInt(Const.User.VOLUME_THINK,50) |
| | | thinkPlayer?.setVolume(volume.toFloat()/100) |
| | | thinkBgPlayer?.startPlayMusic(this, voice?.meditationMusicList?.get(index)) |
| | | thinkPlayer?.startPlayMusic(this, voice?.tutorAudioUrl) |
| | | currentDuration = voice?.meditationSecondList?.get(index)?:0 |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.GOT_THINK_DURATION)) |
| | | playing = true |
| | | cl_player.visible() |
| | | tv_player.text = voice?.meditationTitle |
| | | iv_player.setImageURI(voice?.coverUrl?.split(",")?.firstOrNull()) |
| | | player_play.setImageResource(R.mipmap.player_pause) |
| | | thinkHandler?.sendEmptyMessage(MSG_PROGRESS) |
| | | startTime = System.currentTimeMillis() //记录开始冥想的时间 |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 停止冥想 |
| | | */ |
| | | private fun finishThink(){ |
| | | saveThinkRecord() |
| | | voice = null |
| | | index = 0 |
| | | finishTime = 0L |
| | | thinkBgPlayer?.stopPlayMusic() |
| | | thinkPlayer?.stopPlayMusic() |
| | | playing = false |
| | | thinkHandler?.removeMessages(0) |
| | | cl_player.gone() |
| | | if (JkApplication.isForeground) |
| | | bgPlayer?.resume() |
| | | } |
| | | |
| | | /** |
| | | * 保存冥想记录 |
| | | */ |
| | | private fun saveThinkRecord() { |
| | | if (voice == null||startTime == 0L) |
| | | return |
| | | val time = ((System.currentTimeMillis() - startTime) / 1000).toInt() |
| | | startTime = 0L |
| | | HttpManager.saveViewingHistory(voice?.id?:"", time).request(this,false,{ _, _-> |
| | | Log.e(Const.Tag,"冥想记录成功:$time 秒") |
| | | }){_,_-> |
| | | Log.e(Const.Tag,"冥想记录失败:$time 秒") |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | private fun initTab() { |
| | | val titles = arrayListOf("首页","课程","","疗愈馆","我的") |
| | | val titles = arrayListOf("疗愈","课程","","疗愈馆","我的") |
| | | val iconChecked = arrayListOf( |
| | | R.mipmap.home_selected, |
| | | R.mipmap.play_selected, |
| | |
| | | } |
| | | |
| | | override fun onTabSelect(position: Int) { |
| | | if (position !=0 &&position !=3 && SPUtils.instance().getString(Const.User.TOKEN).isNullOrEmpty()){ |
| | | toast("请先登录") |
| | | startActivity<LoginActivity>() |
| | | tab_bar.currentTab = 0 |
| | | return |
| | | } |
| | | view_pager.currentItem = position |
| | | UltimateBar.with(this@MainActivity) |
| | | .statusDark(position != 0&&position!=1) |
| | | .create().immersionBar() //沉浸状态栏 |
| | | if (position == 2){ |
| | | (fragments[2] as TreeFragment).getTree() |
| | | } |
| | | if (position == 4){ |
| | | (fragments[4] as MineFragment).queryUnread() |
| | | } |
| | | if (position != 1) |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.PAUSE_TEACHER_VIDEO)) |
| | | if (position == 0) |
| | | startTodayCheck() |
| | | else |
| | | stopTodayCheck() |
| | | } |
| | | |
| | | override fun onTabReselect(position: Int) { |
| | | } |
| | | |
| | | @Subscribe |
| | | fun onSwitch(e:EmptyEvent){ |
| | | if (e.code == Const.EventCode.SWITCH_HOME){ |
| | | fun onEvent(e:EmptyEvent){ |
| | | if (e.code == Const.EventCode.CHANGE_USER){ |
| | | tab_bar.currentTab = 0 |
| | | onTabSelect(0) |
| | | }else if(e.code == Const.EventCode.CHANGE_STARTCLASS){ |
| | | (fragments[0] as HomeFragment).refreshData() |
| | | checkAnswer() |
| | | }else if (e.code == Const.EventCode.SWITCH_HOME){ |
| | | tab_bar.currentTab = 0 |
| | | onTabSelect(0) |
| | | }else if (e.code == Const.EventCode.SWITCH_COURSE){ |
| | | tab_bar.currentTab = 1 |
| | | onTabSelect(1) |
| | | }else if(e.code == Const.EventCode.SWITCH_DISCOVERY){ |
| | |
| | | }else if(e.code == Const.EventCode.CHANGE_EXPLORE){ |
| | | tab_bar.currentTab = 3 |
| | | onTabSelect(3) |
| | | }else if(e.code == Const.EventCode.APP_FOREGROUND){ |
| | | bgPlayer?.resume() |
| | | }else if(e.code == Const.EventCode.APP_BACKGROUND){ |
| | | bgPlayer?.pause() |
| | | }else if(e.code == Const.EventCode.CHANGE_BGM){ |
| | | startBgm() |
| | | (fragments[0] as HomeFragment).changeBg() |
| | | }else if(e.code == Const.EventCode.START_THINK){ |
| | | startThink() |
| | | }else if(e.code == Const.EventCode.FINISH_THINK){ |
| | | finishThink() |
| | | }else if(e.code == Const.EventCode.PAUSE_OR_RESUME_THINK){ |
| | | player_play.callOnClick() |
| | | }else if(e.code == Const.EventCode.CHANGE_THINK_VOLUME){ |
| | | val v = SPUtils.instance().getInt(Const.User.VOLUME_THINK,50) |
| | | thinkPlayer?.setVolume(v.toFloat()/100) |
| | | }else if(e.code == Const.EventCode.START_GUIDE_AUDIO){ |
| | | inGuide = true |
| | | startGuide() |
| | | bgPlayer?.pause() |
| | | thinkBgPlayer?.pause() |
| | | thinkPlayer?.pause() |
| | | }else if(e.code == Const.EventCode.FINISH_GUIDE_AUDIO){ |
| | | inGuide = false |
| | | guidePlayer?.stopPlayMusic() |
| | | bgPlayer?.resume() |
| | | if (voice!=null&& playing){ |
| | | thinkBgPlayer?.resume() |
| | | thinkPlayer?.resume() |
| | | } |
| | | }else if(e.code == Const.EventCode.REFRESH_PRIVATE){ //重新答题后,刷新私人定制 |
| | | (fragments[0] as HomeFragment).getPrivacy() |
| | | }else if(e.code == Const.EventCode.THINK_TIMER){ //开启倒计时 |
| | | thinkHandler?.sendEmptyMessage(1) |
| | | } |
| | | } |
| | | |
| | | private fun checkAnswer() { |
| | | val answer = SPUtils.instance().getString(Const.User.ANSWER) |
| | | if (!answer.isNullOrEmpty()){ //已登录并且有答案 |
| | | HttpManager.saveUserAnswers(Gson().fromJson(answer, ReqAnswer::class.java)).request(this,false,success = { _, _-> |
| | | SPUtils.instance().put(Const.User.ANSWER,"").apply() |
| | | Log.e(Const.Tag,"私人定制已保存") |
| | | (fragments[0] as HomeFragment).getPrivacy() |
| | | }){_,_-> |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | private fun startTodayCheck(){ |
| | | thinkHandler?.sendEmptyMessage(MSG_TODAY) |
| | | } |
| | | |
| | | private fun stopTodayCheck(){ |
| | | thinkHandler?.removeMessages(MSG_TODAY) |
| | | } |
| | | |
| | | override fun onResume() { |
| | | super.onResume() |
| | | if (view_pager.currentItem == 4){ |
| | | (fragments[4] as MineFragment).queryUnread() |
| | | } else if (view_pager.currentItem == 0){ |
| | | startTodayCheck() |
| | | } else if (view_pager.currentItem == 2){ |
| | | (fragments[2] as TreeFragment).getTree() |
| | | } |
| | | } |
| | | |
| | | override fun onPause() { |
| | | super.onPause() |
| | | stopTodayCheck() |
| | | } |
| | | |
| | | override fun onDestroy() { |
| | | super.onDestroy() |
| | | bgPlayer?.stopPlayMusic() |
| | | EventBus.getDefault().unregister(this) |
| | | } |
| | | |
| | | override fun onUpdate(db: Double, time: Long) { |
| | | } |
| | | |
| | | override fun onStop(filePath: String?) { |
| | | } |
| | | |
| | | override fun onStartPlay() { |
| | | } |
| | | |
| | | override fun onFinishPlay() { |
| | | if (voice == null) //说明是手动关闭的,不需要处理下一步播放逻辑 |
| | | return |
| | | if (isRecycle){ //单曲 |
| | | if (playing) |
| | | thinkBgPlayer?.startPlayMusic(this, voice?.meditationMusicList?.get(index)) |
| | | }else{//顺序 |
| | | index++ |
| | | if (index>=voice?.meditationMusicList?.size?:0)//列表已播完 |
| | | index = 0 |
| | | currentDuration = voice?.meditationSecondList?.get(index)?:0 |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.GOT_THINK_DURATION)) |
| | | if (playing) |
| | | thinkBgPlayer?.startPlayMusic(this, voice?.meditationMusicList?.get(index)) |
| | | } |
| | | } |
| | | |
| | | override fun onGetDuration(duration: Int) { |
| | | |
| | | } |
| | | |
| | | override fun onBackPressed() { |
| | | if (teacherVideoView?.onBackPressed() != true) |
| | | super.onBackPressed() |
| | | } |
| | | |
| | | companion object{ //冥想播放相关参数 |
| | | var playing = false //true播放中 |
| | | var isRecycle = false //true单曲循环播放 |
| | | var voice: VoiceDetail? = null //冥想详情 |
| | | var index = 0 //当前播放序号 |
| | | var currentDuration = 0 //当前音频长度(秒) |
| | | var currentPosition = 0 //当前音频进度(秒) |
| | | var finishTime = 0L //自动结束的时间戳 |
| | | } |
| | | } |