From 718f31c92e2029d05260810435a2c70cef6e6ce5 Mon Sep 17 00:00:00 2001 From: lmw <125975490@qq.com> Date: 星期四, 24 四月 2025 10:45:55 +0800 Subject: [PATCH] save --- app/src/main/java/com/sinata/xqmuse/utils/AudioUtils.java | 187 +++++++++++++++++++++++++++++++++++----------- 1 files changed, 140 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/com/sinata/xqmuse/utils/AudioUtils.java b/app/src/main/java/com/sinata/xqmuse/utils/AudioUtils.java index 53ed751..35baa80 100644 --- a/app/src/main/java/com/sinata/xqmuse/utils/AudioUtils.java +++ b/app/src/main/java/com/sinata/xqmuse/utils/AudioUtils.java @@ -1,6 +1,7 @@ package com.sinata.xqmuse.utils; import android.content.Context; +import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.net.Uri; @@ -8,8 +9,24 @@ import android.os.Handler; import android.util.Log; +import com.danikula.videocache.HttpProxyCacheServer; +import com.google.android.exoplayer2.ExoPlayer; +import com.google.android.exoplayer2.MediaItem; +import com.google.android.exoplayer2.PlaybackException; +import com.google.android.exoplayer2.Player; +import com.google.android.exoplayer2.SimpleExoPlayer; +import com.google.android.exoplayer2.source.MediaSource; +import com.sinata.xqmuse.utils.cache.PreloadManager; +import com.sinata.xqmuse.utils.cache.ProxyVideoCacheManager; +import com.sinata.xqmuse.utils.exo.ExoMediaPlayer; +import com.sinata.xqmuse.utils.exo.ExoMediaPlayerFactory; + import java.io.File; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import xyz.doikki.videoplayer.player.AbstractPlayer; public class AudioUtils { @@ -23,9 +40,13 @@ public static final int MAX_LENGTH = 1000 * 600;// 最大录音时长十分钟,实际行程设置为5分钟上传一次 private OnAudioStatusUpdateListener audioStatusUpdateListener; - private MediaPlayer mMediaPlayer; + private SimpleExoPlayer mMediaPlayer; private float volume = 0.5f; //播放音量 + + private String audio = "";//音源 + + private boolean isPause = false; //是否暂停,解决循环播放时资源文件prepare异步产生的暂停失败问题 /** * 文件存储默认sdcard/record @@ -204,60 +225,132 @@ public void onStartPlay(); public void onFinishPlay(); + + public void onGetDuration(int duration); } - //开始播放录音 + + /** + * 开始播放音频 + * @param context + * @param filePath + */ public void startPlayMusic(Context context, String filePath) { if (mMediaPlayer != null) { - stopPlayMusic(); + stopPlayMusic(false); } - mMediaPlayer = new MediaPlayer(); - mMediaPlayer.setVolume(volume,volume); + mMediaPlayer = new SimpleExoPlayer.Builder(context).build(); + mMediaPlayer.setVolume(volume); + isPause = false; try { - Log.e("mmp", "path:" + filePath); - mMediaPlayer.setDataSource(context, Uri.parse(filePath)); - mMediaPlayer.prepareAsync(); - mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { + Log.e("mmp", "开始播放 path:" + filePath); + HttpProxyCacheServer cacheServer = ProxyVideoCacheManager.getProxy(context); + String proxyUrl = cacheServer.getProxyUrl(filePath); + MediaItem mediaItem = MediaItem.fromUri(Uri.parse(proxyUrl)); + mMediaPlayer.setMediaItem(mediaItem); + mMediaPlayer.prepare(); + mMediaPlayer.play(); + mMediaPlayer.addListener(new Player.Listener(){ @Override - public void onPrepared(MediaPlayer mp) { - // 装载完毕回调 - mMediaPlayer.start(); - if (audioStatusUpdateListener != null) - audioStatusUpdateListener.onStartPlay(); + public void onPlayerError(PlaybackException error) { + Log.e("mmp","播放错误:"+error.getMessage()); } - }); - mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { - @Override - public void onCompletion(MediaPlayer mp) { - stopPlayMusic(); -// BaseEvent baseEvent = new BaseEvent(BaseEvent.SEND_AODIO_PLAY_OVER); -// baseEvent.setMsg(filePath); -// EventBus.getDefault().post(baseEvent); - } - }); + @Override + public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { + if (playbackState == Player.STATE_BUFFERING) + Log.e("mmp","缓冲中...STATE_BUFFERING"); + else if (playbackState == Player.STATE_READY){ + Log.e("mmp","缓冲完成,音频准备就绪:STATE_READY"); + + } else if (playbackState == Player.STATE_ENDED){ + Log.e("mmp","播放完成:STATE_ENDED"); + stopPlayMusic(true); + } else if (playbackState == Player.STATE_IDLE){ + Log.e("mmp","播放器空闲:STATE_IDLE"); + } + } + }); } catch (Exception e) { e.printStackTrace(); + } + } + + + /** + * 循环播放音频 + * @param context + * @param filePath + */ + public void loopPlayMusic(Context context, String filePath) { + if (mMediaPlayer != null) { + stopPlayMusic(false); + } + mMediaPlayer = new SimpleExoPlayer.Builder(context).build(); + mMediaPlayer.setVolume(volume); + isPause = false; + try { + Log.e("mmp", "循环播放 path:" + filePath); + HttpProxyCacheServer cacheServer = ProxyVideoCacheManager.getProxy(context); + String proxyUrl = cacheServer.getProxyUrl(filePath); + MediaItem mediaItem = MediaItem.fromUri(Uri.parse(proxyUrl)); + mMediaPlayer.setMediaItem(mediaItem); + mMediaPlayer.prepare(); + mMediaPlayer.setRepeatMode(Player.REPEAT_MODE_ONE); + mMediaPlayer.play(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void pause(){ + if (mMediaPlayer != null) { + mMediaPlayer.pause(); + isPause = true; + } + } + + public void resume(){ + if (mMediaPlayer != null) { + mMediaPlayer.play(); + isPause = false; + } + } + + public void seekTo(int now){ + if (mMediaPlayer != null) { + mMediaPlayer.seekTo(now*1000); } } public void setVolume(float volume){ this.volume = volume; if (mMediaPlayer!=null){ - mMediaPlayer.setVolume(volume,volume); + mMediaPlayer.setVolume(volume); } } - //结束录音播放 - public void stopPlayMusic() { + public long getCurrentPosition(){ + if (mMediaPlayer!=null){ + return mMediaPlayer.getCurrentPosition(); + } + return 0; + } + + + /** + * 结束录音播放 + * @param needCallback true真实结束 才走结束回调,为了切换下一首而结束的 不调结束回调 + */ + public void stopPlayMusic(boolean needCallback) { if (mMediaPlayer == null) return; try { mMediaPlayer.stop(); mMediaPlayer.release(); - if (audioStatusUpdateListener != null) - audioStatusUpdateListener.onFinishPlay(); mMediaPlayer = null; + if (audioStatusUpdateListener != null&&needCallback) + audioStatusUpdateListener.onFinishPlay(); } catch (IllegalStateException e) { e.printStackTrace(); } @@ -270,23 +363,23 @@ * @param filePath * @return */ - public int getMusicTime(Context context, String filePath) { - if (mMediaPlayer != null) { - stopPlayMusic(); - } - mMediaPlayer = new MediaPlayer(); - try { - mMediaPlayer.setDataSource(context, Uri.parse(filePath)); - mMediaPlayer.prepare(); - int duration = mMediaPlayer.getDuration(); - if (0 != duration) { - mMediaPlayer.release(); - return duration / 1000; - } - } catch (IOException e) { - e.printStackTrace(); - } - return 0; - } +// public int getMusicTime(Context context, String filePath) { +// if (mMediaPlayer != null) { +// stopPlayMusic(false); +// } +// mMediaPlayer = new MediaPlayer(); +// try { +// mMediaPlayer.setDataSource(context, Uri.parse(filePath)); +// mMediaPlayer.prepare(); +// int duration = mMediaPlayer.getDuration(); +// if (0 != duration) { +// mMediaPlayer.release(); +// return duration / 1000; +// } +// } catch (IOException e) { +// e.printStackTrace(); +// } +// return 0; +// } } \ No newline at end of file -- Gitblit v1.7.1