| | |
| | | package com.sinata.xqmuse |
| | | |
| | | import android.annotation.SuppressLint |
| | | import android.content.Intent |
| | | import android.os.Handler |
| | | import android.os.Looper |
| | | import android.os.Message |
| | | import android.provider.Settings |
| | | import android.util.Log |
| | | import android.view.View |
| | | import androidx.fragment.app.Fragment |
| | | import androidx.fragment.app.FragmentPagerAdapter |
| | | import cn.sinata.xldutils.gone |
| | |
| | | import com.sinata.xqmuse.utils.AudioUtils |
| | | import com.sinata.xqmuse.utils.Const |
| | | import com.sinata.xqmuse.utils.event.EmptyEvent |
| | | import com.sinata.xqmuse.utils.event.IntEvent |
| | | import kotlinx.android.synthetic.main.activity_main.* |
| | | import org.greenrobot.eventbus.EventBus |
| | | import org.greenrobot.eventbus.Subscribe |
| | |
| | | var teacherVideoView:VideoView? = null |
| | | private var bgPlayer:AudioUtils? = null//背景音乐播放器 |
| | | private var thinkBgPlayer:AudioUtils? = null//冥想背景音播放器 |
| | | private var thinkPlayer:AudioUtils? = null//冥想背景音播放器 |
| | | // private var thinkPlayer:AudioUtils? = null//冥想背景音播放器 |
| | | |
| | | private var guideAudio:String? = null |
| | | private var guidePlayer:AudioUtils? = null//引导音频播放器 |
| | |
| | | private var lastTodayTime = 0L //上一次今日疗愈获取的时间 |
| | | |
| | | private var startTime = 0L //开始疗愈的时间戳 |
| | | |
| | | var hasTreeFirstShow = false //此字段用来判断 树苗的首次弹窗是否已经触发,和isFirst字段配合使用 |
| | | var isBGMChanged = false //此字段用来判断 在疗愈播放中,修改了背景音乐,在疗愈结束后 需要更新BGM音源 |
| | | |
| | | override fun initClick() { |
| | | player_close.setOnClickListener { |
| | |
| | | if (playing){ |
| | | player_play.setImageResource(R.mipmap.player_pause) |
| | | thinkBgPlayer?.resume() |
| | | thinkPlayer?.resume() |
| | | // thinkPlayer?.resume() |
| | | thinkHandler?.sendEmptyMessage(MSG_PROGRESS) |
| | | startTime = System.currentTimeMillis() |
| | | }else{ |
| | | player_play.setImageResource(R.mipmap.player_start) |
| | | thinkBgPlayer?.pause() |
| | | thinkPlayer?.pause() |
| | | // thinkPlayer?.pause() |
| | | thinkHandler?.removeMessages(MSG_PROGRESS) |
| | | saveThinkRecord() |
| | | } |
| | | (fragments[0] as HomeFragment).refreshTodayPlayingState() |
| | | } |
| | | } |
| | | |
| | |
| | | if (!intent.getStringExtra("code").isNullOrEmpty()){ |
| | | startActivity<ShareActivity>("code" to intent.getStringExtra("code")) |
| | | } |
| | | if (isFirst) //解决首次启动eventbus注册慢的问题 |
| | | if (isFirst) //解决首次安装启动eventbus注册慢的问题 |
| | | startActivity<GuideActivity>() |
| | | |
| | | thinkHandler = object :Handler(Looper.myLooper()!!){ |
| | |
| | | } |
| | | |
| | | 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) |
| | | HttpManager.getHomeBackgroundMusicByUserId().request(this){_,data-> |
| | | if (!data?.audioFile.isNullOrEmpty()){ |
| | | if (bgPlayer == null) |
| | | bgPlayer = AudioUtils() |
| | | val volume = SPUtils.instance().getInt(Const.User.VOLUME,50) |
| | | bgPlayer?.setVolume(volume.toFloat()/100) |
| | | bgPlayer?.loopPlayMusic(this,data?.audioFile) |
| | | } |
| | | (fragments[0] as HomeFragment).changeBg(data?.imageUrl?:"",data?.backUrl?:"") |
| | | } |
| | | } |
| | | |
| | |
| | | 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?.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 |
| | |
| | | player_play.setImageResource(R.mipmap.player_pause) |
| | | thinkHandler?.sendEmptyMessage(MSG_PROGRESS) |
| | | startTime = System.currentTimeMillis() //记录开始冥想的时间 |
| | | (fragments[0] as HomeFragment).refreshTodayPlayingState() //对比当前音频是否是每日疗愈 |
| | | } |
| | | } |
| | | |
| | |
| | | voice = null |
| | | index = 0 |
| | | finishTime = 0L |
| | | thinkBgPlayer?.stopPlayMusic() |
| | | thinkPlayer?.stopPlayMusic() |
| | | thinkBgPlayer?.stopPlayMusic(true) |
| | | playing = false |
| | | thinkHandler?.removeMessages(0) |
| | | cl_player.gone() |
| | | if (JkApplication.isForeground) |
| | | bgPlayer?.resume() |
| | | (fragments[0] as HomeFragment).refreshTodayPlayingState() //对比当前音频是否是每日疗愈 |
| | | if (XQApplication.isForeground){ |
| | | if (isBGMChanged) //BGM已经被切换,重新播放新BGM |
| | | startBgm() |
| | | else //BGM未改变,直接续播 |
| | | bgPlayer?.resume() |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 保存冥想记录 |
| | | */ |
| | | private fun saveThinkRecord() { |
| | | if (voice == null||startTime == 0L) |
| | | if (voice == null||startTime == 0L||SPUtils.instance().getString(Const.User.TOKEN).isNullOrEmpty()) |
| | | return |
| | | val time = ((System.currentTimeMillis() - startTime) / 1000).toInt() |
| | | startTime = 0L |
| | |
| | | } |
| | | |
| | | override fun onTabSelect(position: Int) { |
| | | if (position !=0 &&position !=3 && SPUtils.instance().getString(Const.User.TOKEN).isNullOrEmpty()){ |
| | | if (position == 4 && SPUtils.instance().getString(Const.User.TOKEN).isNullOrEmpty()){ |
| | | toast("请先登录") |
| | | startActivity<LoginActivity>() |
| | | tab_bar.currentTab = 0 |
| | |
| | | view_pager.currentItem = position |
| | | if (position == 2){ |
| | | (fragments[2] as TreeFragment).getTree() |
| | | if (cl_player.visibility == View.VISIBLE){ |
| | | cl_player.alpha = 0f |
| | | cl_player.gone() |
| | | } |
| | | }else{ |
| | | if (cl_player.alpha == 0f){ |
| | | cl_player.visible() |
| | | cl_player.alpha = 1f |
| | | } |
| | | } |
| | | if (position == 4){ |
| | | (fragments[4] as MineFragment).queryUnread() |
| | |
| | | }else if (e.code == Const.EventCode.SWITCH_COURSE){ |
| | | tab_bar.currentTab = 1 |
| | | onTabSelect(1) |
| | | }else if(e.code == Const.EventCode.SWITCH_DISCOVERY){ |
| | | tab_bar.currentTab = 2 |
| | | onTabSelect(2) |
| | | }else if(e.code == Const.EventCode.CHANGE_EXPLORE){ |
| | | tab_bar.currentTab = 3 |
| | | onTabSelect(3) |
| | | }else if(e.code == Const.EventCode.APP_FOREGROUND){ |
| | | bgPlayer?.resume() |
| | | if (voice==null) |
| | | 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() |
| | | if (voice == null) |
| | | startBgm() |
| | | else |
| | | isBGMChanged = true //正在播放疗愈,无法立即切换背景音乐 |
| | | }else if(e.code == Const.EventCode.START_THINK){ |
| | | startThink() |
| | | }else if(e.code == Const.EventCode.FINISH_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) |
| | | thinkBgPlayer?.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() |
| | | guidePlayer?.stopPlayMusic(false) |
| | | if (voice!=null&& playing){ |
| | | thinkBgPlayer?.resume() |
| | | thinkPlayer?.resume() |
| | | } |
| | | if (voice == null) |
| | | bgPlayer?.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) |
| | | thinkHandler?.sendEmptyMessage(MSG_COUNTDOWN) |
| | | } |
| | | } |
| | | |
| | | @Subscribe |
| | | fun onIntEvent(e:IntEvent){ |
| | | if (e.code == Const.EventCode.THINK_SEEK_PROGRESS){ |
| | | thinkBgPlayer?.seekTo(e.i) |
| | | player_play.callOnClick() |
| | | } |
| | | } |
| | | |
| | | @SuppressLint("HardwareIds") |
| | | 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 = { _, _-> |
| | | val reqAnswer = Gson().fromJson(answer, ReqAnswer::class.java) |
| | | reqAnswer?.device = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID) |
| | | HttpManager.saveUserAnswers(reqAnswer).request(this,false,success = { _, _-> |
| | | SPUtils.instance().put(Const.User.ANSWER,"").apply() |
| | | Log.e(Const.Tag,"私人定制已保存") |
| | | (fragments[0] as HomeFragment).getPrivacy() |
| | |
| | | } else if (view_pager.currentItem == 2){ |
| | | (fragments[2] as TreeFragment).getTree() |
| | | } |
| | | if (isFirst&&!hasTreeFirstShow&&!SPUtils.instance().getString(Const.User.ANSWER).isNullOrEmpty()){ //如果是第一次安装并且还没显示树苗打卡引导并且答完题了 就去树苗引导 |
| | | tab_bar.currentTab = 2 |
| | | onTabSelect(2) |
| | | (fragments[2] as TreeFragment).showFirst() |
| | | hasTreeFirstShow = true |
| | | } |
| | | if (XQApplication.finishAnswer){ |
| | | XQApplication.finishAnswer = false |
| | | tab_bar.currentTab = 2 |
| | | onTabSelect(2) |
| | | } |
| | | } |
| | | |
| | | override fun onPause() { |
| | |
| | | |
| | | override fun onDestroy() { |
| | | super.onDestroy() |
| | | bgPlayer?.stopPlayMusic() |
| | | bgPlayer?.stopPlayMusic(false) |
| | | EventBus.getDefault().unregister(this) |
| | | } |
| | | |
| | |
| | | }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)) |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.FINISH_THINK)) |
| | | else{ |
| | | currentDuration = voice?.meditationSecondList?.get(index)?:0 |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.GOT_THINK_DURATION)) |
| | | if (playing) |
| | | thinkBgPlayer?.startPlayMusic(this, voice?.meditationMusicList?.get(index)) |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | var voice: VoiceDetail? = null //冥想详情 |
| | | var index = 0 //当前播放序号 |
| | | var currentDuration = 0 //当前音频长度(秒) |
| | | var currentPosition = 0 //当前音频进度(秒) |
| | | var currentPosition = 0L //当前音频进度(毫秒) |
| | | var finishTime = 0L //自动结束的时间戳 |
| | | } |
| | | } |