lmw
2024-12-06 40237ce34f0754495e5ac6a7b291fa96508160fd
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,16 @@
import android.os.Handler;
import android.util.Log;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.sinata.xqmuse.utils.exo.ExoMediaPlayer;
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 {
@@ -28,6 +37,8 @@
    private float volume = 0.5f; //播放音量
    private String audio = "";//音源
    private boolean isPause = false; //是否暂停,解决循环播放时资源文件prepare异步产生的暂停失败问题
    /**
     * 文件存储默认sdcard/record
@@ -218,12 +229,15 @@
     */
    public void startPlayMusic(Context context, String filePath) {
        if (mMediaPlayer != null) {
            stopPlayMusic();
            stopPlayMusic(false);
        }
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setVolume(volume,volume);
        isPause = false;
        try {
            Log.e("mmp", "path:" + filePath);
            Log.e("mmp", "开始播放 path:" + filePath);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource(context, Uri.parse(filePath));
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@@ -242,8 +256,7 @@
            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayMusic();
//                    BaseEvent baseEvent = new BaseEvent(BaseEvent.SEND_AODIO_PLAY_OVER);
                    stopPlayMusic(true);
//                    baseEvent.setMsg(filePath);
//                    EventBus.getDefault().post(baseEvent);
                }
@@ -261,12 +274,13 @@
     */
    public void loopPlayMusic(Context context, String filePath) {
        if (mMediaPlayer != null) {
            stopPlayMusic();
            stopPlayMusic(false);
        }
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setVolume(volume,volume);
        isPause = false;
        try {
            Log.e("mmp", "path:" + filePath);
            Log.e("mmp", "循环播放 path:" + filePath);
            mMediaPlayer.setDataSource(context, Uri.parse(filePath));
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@@ -281,7 +295,12 @@
            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
//                    Log.e("mmp", "播放完成 从新循环 path:" + filePath);
                    mMediaPlayer.start();
                    if (isPause){
//                        Log.e("mmp", "循环播放异步暂停");
                        mMediaPlayer.pause();
                    }
                }
            });
@@ -293,12 +312,20 @@
    public void pause(){
        if (mMediaPlayer != null) {
            mMediaPlayer.pause();
            isPause = true;
        }
    }
    public void resume(){
        if (mMediaPlayer != null) {
            mMediaPlayer.start();
            isPause = false;
        }
    }
    public void seekTo(int now){
        if (mMediaPlayer != null) {
            mMediaPlayer.seekTo(now*1000);
        }
    }
@@ -316,15 +343,19 @@
        return 0;
    }
    //结束录音播放
    public void stopPlayMusic() {
    /**
     * 结束录音播放
     * @param needCallback  true真实结束 才走结束回调,为了切换下一首而结束的 不调结束回调
     */
    public void stopPlayMusic(boolean needCallback) {
        if (mMediaPlayer == null)
            return;
        try {
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
            if (audioStatusUpdateListener != null)
            if (audioStatusUpdateListener != null&&needCallback)
                audioStatusUpdateListener.onFinishPlay();
        } catch (IllegalStateException e) {
            e.printStackTrace();
@@ -340,7 +371,7 @@
     */
    public int getMusicTime(Context context, String filePath) {
        if (mMediaPlayer != null) {
            stopPlayMusic();
            stopPlayMusic(false);
        }
        mMediaPlayer = new MediaPlayer();
        try {