| | |
| | | package com.sinata.xqmuse.ui.home |
| | | |
| | | import android.os.CountDownTimer |
| | | import android.util.Log |
| | | import android.widget.SeekBar |
| | | import androidx.core.os.bundleOf |
| | | import cn.sinata.xldutils.gone |
| | | import cn.sinata.xldutils.utils.SPUtils |
| | | import cn.sinata.xldutils.utils.showAllowingStateLoss |
| | | import cn.sinata.xldutils.utils.whiteToast |
| | | import cn.sinata.xldutils.visible |
| | | import com.share.utils.ShareUtils |
| | | import com.sinata.xqmuse.MainActivity |
| | | import com.sinata.xqmuse.R |
| | | import com.sinata.xqmuse.ThinkAudioService |
| | | import com.sinata.xqmuse.dialog.CommentDialog |
| | | import com.sinata.xqmuse.dialog.ShareDialog |
| | | import com.sinata.xqmuse.dialog.TimeSettingDialog |
| | | import com.sinata.xqmuse.dialog.VolumeDialog |
| | | import com.sinata.xqmuse.network.Apis |
| | | import com.sinata.xqmuse.network.HttpManager |
| | | import com.sinata.xqmuse.network.entity.VoiceDetail |
| | | import com.sinata.xqmuse.network.request |
| | | import com.sinata.xqmuse.ui.TransparentStatusBarActivity |
| | | 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 com.sinata.xqmuse.utils.interfaces.StringCallback |
| | | import com.umeng.socialize.bean.SHARE_MEDIA |
| | | import kotlinx.android.synthetic.main.activity_voice_detail.* |
| | | import org.jetbrains.anko.toast |
| | | import org.greenrobot.eventbus.EventBus |
| | | import org.greenrobot.eventbus.Subscribe |
| | | |
| | | class VoiceDetailActivity:TransparentStatusBarActivity() { |
| | | override fun setContentView() = R.layout.activity_voice_detail |
| | | |
| | | private val id by lazy { intent.getStringExtra("id")?:"" } |
| | | private var voiceDetail:VoiceDetail? = null |
| | | private val player by lazy { |
| | | AudioUtils() |
| | | } |
| | | private var index = 0 |
| | | private var isRecycle = true //true列表循环播放 |
| | | private val voiceDetail by lazy { intent.getParcelableExtra<VoiceDetail>("data") } |
| | | |
| | | private var countDownTimer:CountDownTimer? = null |
| | | |
| | | override fun initClick() { |
| | | iv_back.setOnClickListener { finish() } |
| | |
| | | } |
| | | |
| | | iv_play.setOnClickListener { |
| | | if (voiceDetail!=null) |
| | | player.startPlayMusic(this,voiceDetail?.meditationMusicList!![index]) |
| | | if (ThinkAudioService.voice?.id == voiceDetail?.id){ |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.PAUSE_OR_RESUME_THINK)) |
| | | if (ThinkAudioService.playing) //播放中 |
| | | iv_play.setImageResource(R.mipmap.player_pause) |
| | | else |
| | | iv_play.setImageResource(R.mipmap.play_detail) |
| | | }else{ |
| | | if (ThinkAudioService.playing) |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.FINISH_THINK)) |
| | | ThinkAudioService.voice = voiceDetail |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.START_THINK)) |
| | | iv_play.setImageResource(R.mipmap.player_pause) |
| | | } |
| | | } |
| | | |
| | | iv_share.setOnClickListener { |
| | |
| | | shareDialog.callback = object :StringCallback{ |
| | | override fun onResult(rst: String) { |
| | | if (rst == "wx"){ |
| | | toast("微信分享") |
| | | ShareUtils.share(this@VoiceDetailActivity, |
| | | SHARE_MEDIA.WEIXIN,voiceDetail?.detailDescription,voiceDetail?.meditationTitle, |
| | | Apis.SHARE_VOICE.format(voiceDetail?.id),null,"") |
| | | }else{ |
| | | toast("朋友圈分享") |
| | | ShareUtils.share(this@VoiceDetailActivity, |
| | | SHARE_MEDIA.WEIXIN_CIRCLE,voiceDetail?.detailDescription,voiceDetail?.meditationTitle, |
| | | Apis.SHARE_VOICE.format(voiceDetail?.id),null,"") |
| | | } |
| | | } |
| | | } |
| | |
| | | val timeSettingDialog = TimeSettingDialog() |
| | | timeSettingDialog.callback = object :StringCallback{ |
| | | override fun onResult(rst: String) { |
| | | toast("设置倒计时$rst") |
| | | Log.e(Const.Tag,"设置倒计时$rst") |
| | | ThinkAudioService.finishTime = System.currentTimeMillis()+rst.toLong()*60*1000 |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.THINK_TIMER)) |
| | | startTimer() |
| | | } |
| | | } |
| | | timeSettingDialog.showAllowingStateLoss(supportFragmentManager,"time") |
| | | } |
| | | |
| | | iv_recycle.setOnClickListener { |
| | | isRecycle = !isRecycle |
| | | iv_recycle.setImageResource(if (isRecycle) R.mipmap.ic_recycle else R.mipmap.danquxunhuan) |
| | | toast(if (isRecycle) "当前播放模式已设置为顺序播放" else "当前播放模式已设置为单曲循环") |
| | | ThinkAudioService.isRecycle = !ThinkAudioService.isRecycle |
| | | iv_recycle.setImageResource(if (ThinkAudioService.isRecycle) R.mipmap.danquxunhuan else R.mipmap.ic_recycle) |
| | | whiteToast(if (ThinkAudioService.isRecycle) "当前播放模式已设置为单曲循环" else "当前播放模式已设置为顺序播放") |
| | | SPUtils.instance().put(Const.User.IS_RECYCLE,ThinkAudioService.isRecycle).apply() |
| | | } |
| | | tv_comment.setOnClickListener { |
| | | val commentDialog = CommentDialog() |
| | | commentDialog.arguments = bundleOf("id" to id) |
| | | commentDialog.arguments = bundleOf("id" to voiceDetail?.id) |
| | | commentDialog.showAllowingStateLoss(supportFragmentManager,"comment") |
| | | } |
| | | iv_volume.setOnClickListener { |
| | | val volumeDialog = VolumeDialog() |
| | | volumeDialog.showAllowingStateLoss(supportFragmentManager,"vol") |
| | | } |
| | | |
| | | sb_voice.setOnSeekBarChangeListener(object :SeekBar.OnSeekBarChangeListener{ |
| | | override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { |
| | | } |
| | | |
| | | override fun onStartTrackingTouch(seekBar: SeekBar?) { |
| | | if (ThinkAudioService.playing){ //播放中,让其暂停,并让按钮无法点击 |
| | | iv_play.isEnabled = false |
| | | EventBus.getDefault().post(EmptyEvent(Const.EventCode.PAUSE_OR_RESUME_THINK)) |
| | | } |
| | | } |
| | | |
| | | override fun onStopTrackingTouch(seekBar: SeekBar?) { |
| | | if (!ThinkAudioService.playing){//暂停时,让其播放并让按钮恢复可点击 |
| | | EventBus.getDefault().post(IntEvent(Const.EventCode.THINK_SEEK_PROGRESS,seekBar?.progress?:0)) |
| | | iv_play.isEnabled = true |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | |
| | | override fun initView() { |
| | | titleBar.gone() |
| | | isRecycle = SPUtils.instance().getBoolean(Const.User.IS_RECYCLE,true) |
| | | getDetail() |
| | | } |
| | | |
| | | private fun getDetail() { |
| | | HttpManager.getMeditationDetails(id).request(this){_,data-> |
| | | this.voiceDetail = data |
| | | data?.apply { |
| | | iv_collect.setImageResource(if (favorite == 1) R.mipmap.collected else R.mipmap.uncollect) |
| | | iv_bg.setImageURI(backgroundUrl) |
| | | tv_name.text = meditationTitle |
| | | tv_subtitle.text = detailDescription |
| | | tv_comment.text = questionCount |
| | | ThinkAudioService.isRecycle = SPUtils.instance().getBoolean(Const.User.IS_RECYCLE,false) |
| | | iv_recycle.setImageResource(if (ThinkAudioService.isRecycle) R.mipmap.danquxunhuan else R.mipmap.ic_recycle) |
| | | voiceDetail?.apply { |
| | | iv_collect.setImageResource(if (favorite == 1) R.mipmap.collected else R.mipmap.uncollect) |
| | | iv_bg.setImageURI(backgroundUrl) |
| | | tv_name.text = meditationTitle |
| | | tv_subtitle.text = detailDescription |
| | | tv_comment.text = questionCount |
| | | val seconds = (if (ThinkAudioService.voice?.id == id) meditationSecondList?.getOrNull(ThinkAudioService.index) else meditationSecondList?.firstOrNull() )?: 0 |
| | | tv_total.text = "%02d:%02d".format(seconds/60,seconds%60) |
| | | sb_voice.max = seconds |
| | | if (ThinkAudioService.voice?.id == id){ //主页播放的正是本音频 |
| | | if (ThinkAudioService.finishTime!=0L)//有倒计时存在 |
| | | startTimer() |
| | | if (ThinkAudioService.playing) //如果在播放中,按钮变为暂停 |
| | | iv_play.setImageResource(R.mipmap.player_pause) |
| | | //恢复进度 |
| | | tv_progress.text = "%02d:%02d".format(ThinkAudioService.currentPosition/1000/60,ThinkAudioService.currentPosition/1000%60) |
| | | sb_voice.progress = (ThinkAudioService.currentPosition/1000).toInt() |
| | | } |
| | | } |
| | | EventBus.getDefault().register(this) |
| | | iv_play.keepScreenOn = true |
| | | } |
| | | |
| | | private fun startTimer(){ |
| | | if (countDownTimer!=null) |
| | | countDownTimer!!.cancel() |
| | | tv_timer.visible() |
| | | val offset = ThinkAudioService.finishTime - System.currentTimeMillis() |
| | | countDownTimer = object :CountDownTimer(offset,1000){ |
| | | override fun onTick(millisUntilFinished: Long) { |
| | | tv_timer.text = "%02d:%02d".format(millisUntilFinished/1000/60,millisUntilFinished/1000%60) |
| | | } |
| | | |
| | | override fun onFinish() { |
| | | tv_timer.gone() |
| | | } |
| | | } |
| | | countDownTimer?.start() |
| | | } |
| | | |
| | | private fun collect(){ |
| | | HttpManager.favorite(id).request(this){_,data-> |
| | | HttpManager.favorite(voiceDetail?.id?:"").request(this){_,data-> |
| | | voiceDetail?.favorite = if (voiceDetail?.favorite == 1) 2 else 1 |
| | | iv_collect.setImageResource(if (voiceDetail?.favorite == 1) R.mipmap.collected else R.mipmap.uncollect) |
| | | } |
| | | } |
| | | |
| | | @Subscribe |
| | | fun onEvent(e:EmptyEvent){ |
| | | if (e.code == Const.EventCode.FINISH_THINK){ |
| | | iv_play.setImageResource(R.mipmap.play_detail) |
| | | sb_voice.progress = 0 |
| | | tv_progress.text = "00:00" |
| | | tv_timer.gone() |
| | | countDownTimer?.cancel() |
| | | }else if (e.code == Const.EventCode.GOT_THINK_DURATION&&ThinkAudioService.voice?.id == voiceDetail?.id){ |
| | | tv_total.text = "%02d:%02d".format(ThinkAudioService.currentDuration/60,ThinkAudioService.currentDuration%60) |
| | | sb_voice.max = ThinkAudioService.currentDuration |
| | | }else if (e.code == Const.EventCode.GOT_THINK_POSITION&&ThinkAudioService.voice?.id == voiceDetail?.id){ |
| | | tv_progress.text = "%02d:%02d".format(ThinkAudioService.currentPosition/1000/60,ThinkAudioService.currentPosition/1000%60) |
| | | sb_voice.progress = (ThinkAudioService.currentPosition/1000).toInt() |
| | | } |
| | | } |
| | | |
| | | override fun onDestroy() { |
| | | super.onDestroy() |
| | | EventBus.getDefault().unregister(this) |
| | | countDownTimer?.cancel() |
| | | } |
| | | } |