|
@@ -5,24 +5,23 @@ import android.media.AudioAttributes;
|
|
|
import android.media.AudioFocusRequest;
|
|
|
import android.media.AudioManager;
|
|
|
import android.media.MediaPlayer;
|
|
|
-import android.net.Uri;
|
|
|
import android.view.SurfaceHolder;
|
|
|
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+
|
|
|
import com.xplora.xpchat.ChatApplication;
|
|
|
import com.xplora.xpchat.model.MessageBean;
|
|
|
import com.xplora.xpchat.observer.Listener;
|
|
|
import com.xplora.xpchat.utils.Constant;
|
|
|
-import com.xplora.xpchat.view.ChatException;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
public class MediaPlayerUtils {
|
|
|
private MediaPlayer mMediaPlayer = null;
|
|
|
private Listener.PlayListener mPlayListener = null;
|
|
|
- private boolean mIsPlaying = false;
|
|
|
- private MessageBean mPlayingBean = new MessageBean();
|
|
|
private SurfaceHolder mSurfaceHolder = null;
|
|
|
private boolean mIsAutoRelease = true;
|
|
|
+ private MessageBean mPlayingBean = new MessageBean();
|
|
|
private AudioFocusRequest mAudioFocusRequest = null;
|
|
|
|
|
|
public MediaPlayerUtils(Listener.PlayListener listener) {
|
|
@@ -35,32 +34,9 @@ public class MediaPlayerUtils {
|
|
|
mIsAutoRelease = isAutoRelease;
|
|
|
}
|
|
|
|
|
|
- public boolean play(MessageBean bean) {
|
|
|
+ public void startPlay(MessageBean bean, boolean autoPlay) {
|
|
|
setPlayingBean(bean);
|
|
|
-
|
|
|
- //播放
|
|
|
- try {
|
|
|
- if (bean.type == Constant.MESSAGE_TYPE_VIDEO) {
|
|
|
- startPlay(bean.videoPath);
|
|
|
- } else {
|
|
|
- startPlay(bean.filePath);
|
|
|
- }
|
|
|
-
|
|
|
- //告诉view,开始
|
|
|
- if (mPlayListener != null)
|
|
|
- mPlayListener.onPlayStart(bean);
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
- catch (ChatException ex) {
|
|
|
- if (mPlayListener != null)
|
|
|
- mPlayListener.onPlayException(ex.getMessage());
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void startPlay(String filePath) throws ChatException {
|
|
|
- stopPlay();
|
|
|
+ mediaStop();
|
|
|
|
|
|
try {
|
|
|
requestFocus();
|
|
@@ -68,49 +44,71 @@ public class MediaPlayerUtils {
|
|
|
//配置
|
|
|
mMediaPlayer = new MediaPlayer();
|
|
|
mMediaPlayer.reset();
|
|
|
- mMediaPlayer.setDataSource(filePath);
|
|
|
- //mMediaPlayer.setDataSource(this, Uri.parse(mPlayingBean.videoPath));
|
|
|
+ mMediaPlayer.setDataSource(bean.type == Constant.MESSAGE_TYPE_VIDEO ? bean.videoPath : bean.filePath);
|
|
|
mMediaPlayer.setScreenOnWhilePlaying(true);
|
|
|
- if (mSurfaceHolder != null)
|
|
|
+ if (mSurfaceHolder != null) {
|
|
|
mMediaPlayer.setDisplay(mSurfaceHolder);
|
|
|
+ }
|
|
|
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
|
|
|
@Override
|
|
|
public void onCompletion(MediaPlayer mp) {
|
|
|
- finishPlay();
|
|
|
+ onMediaCompletion();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
|
|
|
+ @Override
|
|
|
+ public void onPrepared(MediaPlayer mp) {
|
|
|
+ onMediaPrepared(autoPlay);
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
- //播放
|
|
|
mMediaPlayer.prepare();
|
|
|
- mMediaPlayer.start();
|
|
|
+ } catch (IOException ex) {
|
|
|
+ if (mPlayListener != null) {
|
|
|
+ mPlayListener.onPlayException(ex.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- setIsPlaying(true);
|
|
|
- } catch (IOException ex) {
|
|
|
- setIsPlaying(false);
|
|
|
- throw new ChatException("Chat Exception:",ex);
|
|
|
+ private void onMediaPrepared(boolean autoPlay) {
|
|
|
+ mMediaPlayer.seekTo(0);
|
|
|
+ if (mPlayListener != null) {
|
|
|
+ mPlayListener.onPlayPrepared(mPlayingBean);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (autoPlay) {
|
|
|
+ mMediaPlayer.start();
|
|
|
+ if (mPlayListener != null) {
|
|
|
+ mPlayListener.onPlayStart(mPlayingBean);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//语音播放完成
|
|
|
- private void finishPlay() {
|
|
|
- stopPlay();
|
|
|
-
|
|
|
+ private void onMediaCompletion() {
|
|
|
+ mediaStop();
|
|
|
//告诉view,结束
|
|
|
if (mPlayListener != null)
|
|
|
mPlayListener.onPlayFinish(mPlayingBean);
|
|
|
}
|
|
|
|
|
|
- //语音播放完成
|
|
|
- public void cancelPlay() {
|
|
|
- stopPlay();
|
|
|
+ public void mediaStart() {
|
|
|
+ if (mMediaPlayer != null)
|
|
|
+ mMediaPlayer.start();
|
|
|
+ //告诉view,开始
|
|
|
+ if (mPlayListener != null)
|
|
|
+ mPlayListener.onPlayStart(mPlayingBean);
|
|
|
+ }
|
|
|
|
|
|
- //告诉view,取消//
|
|
|
+ public void mediaPause() {
|
|
|
+ if (mMediaPlayer != null)
|
|
|
+ mMediaPlayer.pause();
|
|
|
+ //告诉view,开始
|
|
|
if (mPlayListener != null)
|
|
|
- mPlayListener.onPlayCancel(mPlayingBean);
|
|
|
+ mPlayListener.onPlayPause(mPlayingBean);
|
|
|
}
|
|
|
|
|
|
//onDestroy 时会调用,防止闪退时,没有释放//
|
|
|
- private void stopPlay() {
|
|
|
+ private void mediaStop() {
|
|
|
if (mIsAutoRelease) {
|
|
|
if (mMediaPlayer != null) {
|
|
|
mMediaPlayer.setOnCompletionListener(null);
|
|
@@ -120,23 +118,9 @@ public class MediaPlayerUtils {
|
|
|
mMediaPlayer = null;
|
|
|
abandonFocus();
|
|
|
}
|
|
|
- setIsPlaying(false);
|
|
|
- }
|
|
|
-
|
|
|
- public void mediaStart() {
|
|
|
- setIsPlaying(true);
|
|
|
- if (mMediaPlayer != null)
|
|
|
- mMediaPlayer.start();
|
|
|
- }
|
|
|
-
|
|
|
- public void mediaPause() {
|
|
|
- setIsPlaying(false);
|
|
|
- if (mMediaPlayer != null)
|
|
|
- mMediaPlayer.pause();
|
|
|
}
|
|
|
|
|
|
public void mediaRelease() {
|
|
|
- setIsPlaying(false);
|
|
|
if (mMediaPlayer != null) {
|
|
|
mMediaPlayer.release();
|
|
|
mMediaPlayer = null;
|
|
@@ -144,6 +128,14 @@ public class MediaPlayerUtils {
|
|
|
abandonFocus();
|
|
|
}
|
|
|
|
|
|
+ //语音播放完成
|
|
|
+ public void cancelPlay() {
|
|
|
+ mediaStop();
|
|
|
+ //告诉view,取消//
|
|
|
+ if (mPlayListener != null)
|
|
|
+ mPlayListener.onPlayCancel(mPlayingBean);
|
|
|
+ }
|
|
|
+
|
|
|
public void mediaSeekTo(int value) {
|
|
|
if (mMediaPlayer != null)
|
|
|
mMediaPlayer.seekTo(value);
|
|
@@ -173,14 +165,15 @@ public class MediaPlayerUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public MessageBean getPlayingBean() {
|
|
|
- return mPlayingBean;
|
|
|
- }
|
|
|
-
|
|
|
public void setPlayingBean(MessageBean mPlayingBean) {
|
|
|
this.mPlayingBean = mPlayingBean;
|
|
|
}
|
|
|
|
|
|
- public boolean getIsPlaying() { return mIsPlaying; }
|
|
|
- public void setIsPlaying(boolean isPlaying) { mIsPlaying = isPlaying; }
|
|
|
+ public boolean getIsPlaying() {
|
|
|
+ if (mMediaPlayer != null) {
|
|
|
+ return mMediaPlayer.isPlaying();
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|