Commit e769d986 by 范玉宾

Solve the abnormal state of mediaplayer

parent cac5df85
......@@ -6,10 +6,8 @@ import android.os.Bundle
import android.widget.SeekBar
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.ydl.media.view.PlayerFloatHelper
import com.ydl.ydlcommon.base.BaseActivity
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.statusBarLightMode
import com.ydl.ydlcommon.utils.remind.ToastHelper
......@@ -32,6 +30,9 @@ class PlayMeditationActivity : BaseActivity() {
private val path =
"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 isPlaying = false
......@@ -66,21 +67,6 @@ class PlayMeditationActivity : BaseActivity() {
}
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)
.load(bgUrl)
.into(iv_bg)
......@@ -135,6 +121,8 @@ class PlayMeditationActivity : BaseActivity() {
startActivity(Intent(this,MeditationTimeOffActivity::class.java))
}
initMediaPlayer()
exo_play.setOnClickListener {
if (isPlaying) {
MediaPlayerManager.getInstance(this).pause()
......@@ -169,7 +157,7 @@ class PlayMeditationActivity : BaseActivity() {
}
exo_prev.setOnClickListener {
MediaPlayerManager.getInstance(this).setNewPath(path)
MediaPlayerManager.getInstance(this).setNewPath(newPath)
isPlaying = if (mMediaPlayer?.isPlaying == true) {
exo_play.setImageResource(R.drawable.icon_pause_play)
if (duration != null) {
......@@ -188,7 +176,7 @@ class PlayMeditationActivity : BaseActivity() {
}
exo_next.setOnClickListener {
MediaPlayerManager.getInstance(this).setNewPath(path)
MediaPlayerManager.getInstance(this).setNewPath(newPath)
isPlaying = if (mMediaPlayer?.isPlaying == true) {
exo_play.setImageResource(R.drawable.icon_pause_play)
if (duration != null) {
......@@ -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() {
super.onDestroy()
mTimer.cancel()
mMediaPlayer?.stop()
mMediaPlayer?.release()
MediaPlayerManager.getInstance(this).stop()
MediaPlayerManager.getInstance(this).release()
}
}
\ No newline at end of file
......@@ -12,7 +12,6 @@ public class MediaPlayerManager {
private MediaPlayer mMediaPlayer;
private String mPath;
private OnMediaPlayerManagerListener mListener;
private volatile static MediaPlayerManager instance;
......@@ -39,16 +38,16 @@ public class MediaPlayerManager {
public void setPath(String path) {
mPath = path;
if (mMediaPlayer.isPlaying()){
mMediaPlayer.reset();
}
try {
if (mMediaPlayer.isPlaying()){
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
}
// mMediaPlayer.start();
}
public void setNewPath(String path){
......@@ -80,20 +79,16 @@ public class MediaPlayerManager {
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(){
if (mMediaPlayer.isPlaying()){
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