Commit e769d986 by 范玉宾

Solve the abnormal state of mediaplayer

parent cac5df85
...@@ -6,10 +6,8 @@ import android.os.Bundle ...@@ -6,10 +6,8 @@ import android.os.Bundle
import android.widget.SeekBar import android.widget.SeekBar
import com.alibaba.android.arouter.facade.annotation.Route import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.ydl.media.view.PlayerFloatHelper
import com.ydl.ydlcommon.base.BaseActivity import com.ydl.ydlcommon.base.BaseActivity
import com.ydl.ydlcommon.bean.StatusBarOptions import com.ydl.ydlcommon.bean.StatusBarOptions
import com.ydl.ydlcommon.utils.LogUtil
import com.ydl.ydlcommon.utils.StatusBarUtils.Companion.setTransparentForImageView import com.ydl.ydlcommon.utils.StatusBarUtils.Companion.setTransparentForImageView
import com.ydl.ydlcommon.utils.StatusBarUtils.Companion.statusBarLightMode import com.ydl.ydlcommon.utils.StatusBarUtils.Companion.statusBarLightMode
import com.ydl.ydlcommon.utils.remind.ToastHelper import com.ydl.ydlcommon.utils.remind.ToastHelper
...@@ -32,6 +30,9 @@ class PlayMeditationActivity : BaseActivity() { ...@@ -32,6 +30,9 @@ class PlayMeditationActivity : BaseActivity() {
private val path = private val path =
"http://file.kuyinyun.com/group1/M00/90/B7/rBBGdFPXJNeAM-nhABeMElAM6bY151.mp3" "http://file.kuyinyun.com/group1/M00/90/B7/rBBGdFPXJNeAM-nhABeMElAM6bY151.mp3"
private val newPath =
"http://file.diyring.cc/UserRingWorksFile/0/50284096.mp3"
private var collected = false private var collected = false
private var isPlaying = false private var isPlaying = false
...@@ -66,21 +67,6 @@ class PlayMeditationActivity : BaseActivity() { ...@@ -66,21 +67,6 @@ class PlayMeditationActivity : BaseActivity() {
} }
private fun initView() { private fun initView() {
mMediaPlayer = MediaPlayerManager.getInstance(this).mediaPlayer
MediaPlayerManager.getInstance(this).path = path
val currentPosition = mMediaPlayer?.currentPosition
duration = mMediaPlayer?.duration?:0
if (currentPosition!=null){
exo_position.text = MediaPlayerTimeUtil.calculateTime(currentPosition/1000)
}
if (duration!=null){
exo_duration.text = MediaPlayerTimeUtil.calculateTime(duration/1000)
}
Glide.with(this) Glide.with(this)
.load(bgUrl) .load(bgUrl)
.into(iv_bg) .into(iv_bg)
...@@ -135,6 +121,8 @@ class PlayMeditationActivity : BaseActivity() { ...@@ -135,6 +121,8 @@ class PlayMeditationActivity : BaseActivity() {
startActivity(Intent(this,MeditationTimeOffActivity::class.java)) startActivity(Intent(this,MeditationTimeOffActivity::class.java))
} }
initMediaPlayer()
exo_play.setOnClickListener { exo_play.setOnClickListener {
if (isPlaying) { if (isPlaying) {
MediaPlayerManager.getInstance(this).pause() MediaPlayerManager.getInstance(this).pause()
...@@ -169,7 +157,7 @@ class PlayMeditationActivity : BaseActivity() { ...@@ -169,7 +157,7 @@ class PlayMeditationActivity : BaseActivity() {
} }
exo_prev.setOnClickListener { exo_prev.setOnClickListener {
MediaPlayerManager.getInstance(this).setNewPath(path) MediaPlayerManager.getInstance(this).setNewPath(newPath)
isPlaying = if (mMediaPlayer?.isPlaying == true) { isPlaying = if (mMediaPlayer?.isPlaying == true) {
exo_play.setImageResource(R.drawable.icon_pause_play) exo_play.setImageResource(R.drawable.icon_pause_play)
if (duration != null) { if (duration != null) {
...@@ -188,7 +176,7 @@ class PlayMeditationActivity : BaseActivity() { ...@@ -188,7 +176,7 @@ class PlayMeditationActivity : BaseActivity() {
} }
exo_next.setOnClickListener { exo_next.setOnClickListener {
MediaPlayerManager.getInstance(this).setNewPath(path) MediaPlayerManager.getInstance(this).setNewPath(newPath)
isPlaying = if (mMediaPlayer?.isPlaying == true) { isPlaying = if (mMediaPlayer?.isPlaying == true) {
exo_play.setImageResource(R.drawable.icon_pause_play) exo_play.setImageResource(R.drawable.icon_pause_play)
if (duration != null) { if (duration != null) {
...@@ -268,12 +256,24 @@ class PlayMeditationActivity : BaseActivity() { ...@@ -268,12 +256,24 @@ class PlayMeditationActivity : BaseActivity() {
} }
private fun initMediaPlayer(){
mMediaPlayer = MediaPlayerManager.getInstance(this).mediaPlayer
MediaPlayerManager.getInstance(this).path = path
val currentPosition = mMediaPlayer?.currentPosition
duration = mMediaPlayer?.duration?:0
if (currentPosition!=null){
exo_position.text = MediaPlayerTimeUtil.calculateTime(currentPosition/1000)
}
if (duration!=null){
exo_duration.text = MediaPlayerTimeUtil.calculateTime(duration/1000)
}
}
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
mTimer.cancel() mTimer.cancel()
mMediaPlayer?.stop() MediaPlayerManager.getInstance(this).stop()
mMediaPlayer?.release() MediaPlayerManager.getInstance(this).release()
} }
} }
\ No newline at end of file
...@@ -12,7 +12,6 @@ public class MediaPlayerManager { ...@@ -12,7 +12,6 @@ public class MediaPlayerManager {
private MediaPlayer mMediaPlayer; private MediaPlayer mMediaPlayer;
private String mPath; private String mPath;
private OnMediaPlayerManagerListener mListener;
private volatile static MediaPlayerManager instance; private volatile static MediaPlayerManager instance;
...@@ -39,16 +38,16 @@ public class MediaPlayerManager { ...@@ -39,16 +38,16 @@ public class MediaPlayerManager {
public void setPath(String path) { public void setPath(String path) {
mPath = path; mPath = path;
if (mMediaPlayer.isPlaying()){
mMediaPlayer.reset();
}
try { try {
if (mMediaPlayer.isPlaying()){
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(path); mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare(); mMediaPlayer.prepare();
} catch (IOException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
// mMediaPlayer.start();
} }
public void setNewPath(String path){ public void setNewPath(String path){
...@@ -80,20 +79,16 @@ public class MediaPlayerManager { ...@@ -80,20 +79,16 @@ public class MediaPlayerManager {
mMediaPlayer.seekTo(position); mMediaPlayer.seekTo(position);
} }
public void setOnPreparedListener(OnMediaPlayerManagerListener listener){
mListener = listener;
}
public interface OnMediaPlayerManagerListener{
default void onPrepared(MediaPlayer mediaPlayer){};
default void onProgress(MediaPlayer mediaPlayer){};
}
public void stop(){ public void stop(){
if (mMediaPlayer.isPlaying()){ if (mMediaPlayer.isPlaying()){
mMediaPlayer.reset(); mMediaPlayer.reset();
} }
} }
public void release(){
if (mMediaPlayer!=null) {
mMediaPlayer.reset();
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment