DebugUtil

package gn.com.android.theme.utils;



import android.content.Context;

import android.util.Log;

import android.widget.Toast;


public class DebugUtil {

    public static final boolean OPEN_STRICT_MODE = true;

    public static final String TAG = "DebugUtil";

    public static final String HUM = "HUM";

    public static final boolean DEBUG = true;



    public static void toast(Context context, String content) {

        Toast.makeText(context, content, Toast.LENGTH_SHORT).show();

    }



    public static void debug(String tag, String msg, Throwable tr) {

        if (DEBUG) {

            Log.d(tag, msg, tr);

        }

    }



    public static void debug(String tag, String msg) {

        if (DEBUG) {

            Log.d(tag, msg);

        }

    }



    public static void debug(String msg) {

        if (DEBUG) {

            Log.d(TAG, msg);

        }

    }



    public static void error(String tag, String msg, Throwable tr) {

        if (DEBUG) {

            Log.e(tag, msg, tr);

        }

    }



    public static void error(String tag, String error) {

        Log.e(tag, error);

    }



    public static void error(String error) {

        Log.e(TAG, error);

    }

}



object AmplitudeDecodeUtil { private const val TAG = "AmplitudeDecodeUtil" /** * The same path corresponds to the same amplitudeListUtil. */ @JvmStatic private var ampTaskMap = HashMap<String, ArrayList<AmpTaskData>>() /** * getAmplitudeByType */ @JvmStatic fun getAmplitudeByType( path: String, playUri: Uri, isRecycle: Boolean, ampTypeOrSegments: String, decodeFinishCallback: (waveDataList: List<Int>?) -> Unit ) { val isExistTask = addGetAmplitudeTask( path, playUri, isRecycle, ampTypeOrSegments, decodeFinishCallback = { decodeFinishCallback } ) if (isExistTask) { DebugUtil.d(TAG, "getAmplitudeByType exist task path:$path") return } CoroutineUtils.doInIOThread({ // It will be time-consuming. ampTaskMap.getAmplitudeListUtilByPath(path)?.apply { val currentTime = System.currentTimeMillis() ampList.also { val getAmpListTime = System.currentTimeMillis() DebugUtil.d(TAG, "getAmplitudeByType getAmpList cost ${getAmpListTime - currentTime}ms") if (it.isNullOrEmpty().not()) { /** * ->source one: json file or mp3 file customTag. * If the data is cached, directly return the amplitude data. */ if (ampStringFromMp3 != null) { // amplitude data from customTag. Asynchronously update amp_file_path, amp file asyncUpdateAmpFileAndDB(path, playUri, convertStringToInt(ampStringFromMp3)) } dispatchReadyOrFinishCallBack(path = path, soundFile = null, waveDataMap = it) } else { /** * ->source two: decode file. * Use audio file samples to parse the recorded waveform and assemble the data. */ decodeFile(this, path, playUri) } releaseAmplitudeListUtilByPath(path) } } }, CoroutineScope(Dispatchers.IO)) } /** * Decode the file * @param amplitudeListUtil */ @JvmStatic fun decodeFile( amplitudeListUtil: AmplitudeListUtil, path: String, playUri: Uri, ) { amplitudeListUtil.apply { releaseMp3() DebugUtil.d(TAG, "xak2 getAmplitudeByType s") val nowTime = System.currentTimeMillis() setDecodeReady { _, soundFile -> DebugUtil.w( TAG, "xak2 getAmplitudeByType setDecodeReady soundFile.amplitudeList.size:${soundFile.amplitudeList.size} cost:${System.currentTimeMillis() - nowTime}!" ) dispatchReadyOrFinishCallBack(false,path, soundFile,null) } setDecodeFinish { ampString -> DebugUtil.w(TAG, "xak2 getAmplitudeByType setDecodeFinish! length:${convertStringToInt(ampString).size} cost:${System.currentTimeMillis() - nowTime}") if (ampString.isNullOrEmpty()) { DebugUtil.w(TAG, "getAmplitudeByType ampString is NullOrEmpty !") dispatchReadyOrFinishCallBack(path = path, soundFile = null, waveDataMap = null) } else { handlerAmp(path, playUri, ampString) } release() } getAmpFromSoundFile() } } @JvmStatic fun updateAmpFileAndDB( path: String, uri: Uri, amplitudeList: List<Int> ) { if (!AmpFileUtil.ampFileIsExists(BaseApplication.getAppContext(), path)) { DebugUtil.d(TAG, "updateAmpFileAndDB start path:$path") AmplitudeListUtil.writeAmpData( BaseApplication.getAppContext(), path, uri, amplitudeList ) } else { DebugUtil.d(TAG, "updateAmpFileAndDB ampFile exists") } } /** * handlerAmp */ @JvmStatic fun handlerAmp( path: String, playUri: Uri, ampString: String, ) { val originalAmpIntList = convertStringToInt(ampString) val waveDataMap = WaveDataMap(originalAmpIntList) dispatchReadyOrFinishCallBack(path = path, soundFile = null, waveDataMap = waveDataMap) updateAmpFileAndDB( path, playUri, originalAmpIntList ) } /** * asyncUpdateAmpFileAndDB */ @JvmStatic fun asyncUpdateAmpFileAndDB( path: String, uri: Uri, amplitudeList: List<Int> ) { CoroutineUtils.doInIOThread({ updateAmpFileAndDB( path, uri, amplitudeList ) }, CoroutineScope(Dispatchers.IO)) } /** * To dispatch CallBack according to path and remove task,or dispatch CallBack according to path and remove task */ @JvmStatic fun dispatchReadyOrFinishCallBack( isFinishCallBack: Boolean = true, path: String, soundFile: SoundFile?, waveDataMap: WaveDataMap? ) { if (isFinishCallBack) { ampTaskMap[path]?.forEach { ampTaskData -> ampTaskData.apply { if (typeOrSegments.isNullOrEmpty()) { DebugUtil.e(TAG, "dispatchFinishCallBack typeOrSegments is null !") } else { ampFinishCallBack?.invoke(waveDataMap?.getAmpByTypeOrSegments(typeOrSegments)) } } } ampTaskMap.remove(path) } else { ampTaskMap[path]?.forEach { ampTaskData -> ampTaskData.apply { if (typeOrSegments.isNullOrEmpty()) { DebugUtil.e(TAG, "dispatchReadyCallBack typeOrSegments is null !") } else { ampReadyCallBack?.invoke(soundFile?.amplitudeList) } } } } } /** * getMarkStringByPath */ fun getMarkStringByPath( path: String? ): String? { if (path.isNullOrEmpty()) { DebugUtil.e(TAG, "getMarkStringByPath path is null !") return null } return ampTaskMap.getAmplitudeListUtilByPath(path)?.markString } /** * getAmpFormDbOrCustomTagByPath (Initialization is required -> initAmplitudeListUtil()) */ fun getAmpFormDbOrCustomTagByPath( path: String?, ampTypeOrSegments: String ): List<Int>? { if (path.isNullOrEmpty()) { DebugUtil.e(TAG, "getAmpFormDbOrCustomTagByPath path is null !") return null } return ampTaskMap.getAmplitudeListUtilByPath(path)?.ampList?.getAmpByTypeOrSegments(ampTypeOrSegments) } fun releaseMp3ByPath( path: String? ) { if (path.isNullOrEmpty()) { DebugUtil.e(TAG, "releaseMp3ByPath path is null !") return } ampTaskMap.getAmplitudeListUtilByPath(path)?.releaseMp3() } fun genAmpFromSoundFileAndSetFunc( path: String?, playUri: Uri?, isRecycle: Boolean, decodeReadyCallback: (List<Int>) -> Unit, decodeFinishCallback: (String) -> Unit ) { if (path.isNullOrEmpty() || playUri == null) { DebugUtil.e(TAG, "genAmpFromSoundFileAndSetFunc path:$path or playUri:$playUri is null !") return } ampTaskMap.getAmplitudeListUtilByPath(path)?.apply { val isExistTask = addGetAmplitudeTask( path, playUri, isRecycle, WaveConstant.FULL_AMP_TYPE, { decodeReadyCallback } , { decodeFinishCallback } ) decodeFile(this@apply, path, playUri) /*if (isExistTask.not()) { decodeFile(this@apply, path, playUri) } else { DebugUtil.d(TAG, "genAmpFromSoundFileAndSetFunc exist task path:$path") }*/ /* { setDecodeReady { _, soundFile -> decodeReadyCallback.invoke(soundFile.amplitudeList) } }, { setDecodeFinish { ampString -> decodeFinishCallback.invoke(ampString) } }*/ } } fun releaseSoundByPath( path: String? ) { if (path.isNullOrEmpty()) { DebugUtil.e(TAG, "releaseSoundByPath path is null !") return } ampTaskMap.getAmplitudeListUtilByPath(path)?.releaseSound() } fun releaseAmplitudeListUtilByPath( path: String? ) { if (path.isNullOrEmpty()) { DebugUtil.e(TAG, "genAmpFromSoundFileAndSetFun path is null !") return } ampTaskMap.getAmplitudeListUtilByPath(path)?.apply { release() remove(path) } } fun addGetAmplitudeTask( path: String?, playUri: Uri?, isRecycle: Boolean, ampTypeOrSegments: String, decodeReadyCallback: ((waveDataList: List<Int>?) -> Unit)? = null, decodeFinishCallback: ((fullWaveData: String?) -> Unit)? = null ) : Boolean { if (path.isNullOrEmpty()|| playUri == null) { DebugUtil.e(TAG, "addGetAmplitudeTask path:$path or playUri:$playUri is null !") return false } if (ampTaskMap.containsKey(path)) { DebugUtil.d(TAG, "getAmplitudeByType ampTaskMap.containsKey(path) path:$path") val amplitudeListUtil = ampTaskMap.getAmplitudeListUtilByPath(path) ampTaskMap[path]?.apply { add(AmpTaskData(amplitudeListUtil, { decodeReadyCallback DebugUtil.d(TAG,"xak2 amplitudeListUtil decode READY")}, { decodeFinishCallback }, ampTypeOrSegments)) } return true } else { val amplitudeListUtil = AmplitudeListUtil( BaseApplication.getAppContext(), path, playUri, isRecycle ) ampTaskMap.put( path, arrayListOf( AmpTaskData( amplitudeListUtil, { decodeReadyCallback }, { decodeFinishCallback }, ampTypeOrSegments ) ) ) return false } } fun HashMap<String, ArrayList<AmpTaskData>>.getAmplitudeListUtilByPath(path: String?): AmplitudeListUtil? { if (path.isNullOrEmpty()) { DebugUtil.e(TAG, "getAmplitudeListUtilByPath path is null !") return null } return this.get(path)?.get(0)?.amplitudeListUtil } } data class AmpTaskData( val amplitudeListUtil: AmplitudeListUtil? = null, val ampReadyCallBack: ((waveReadyDataList: List<Int>?) -> Unit)? = null, val ampFinishCallBack: ((waveDataList: List<Int>?) -> Unit)? = null, val typeOrSegments: String? ) DebugUtil.d(TAG,"xak2 amplitudeListUtil decode READY") 这个打印有,但是前面的decodeReadyCallback没执行呢?
07-14
waveMarkApi?.genAmpFromSoundFileAndSetFunc(path,playUri,isRecycle,{ ampList -> DebugUtil.i(TAG, "xak2 amplitudeListUtil decode ready spend time=${System.currentTimeMillis() - currentTime} ") amplitudeList = ampList mIsDecodeReady.postValue(true) Handler(Looper.getMainLooper()).post { startPlayForAuto() } }, { ampString -> DebugUtil.i(TAG, "xak2 amplitudeListUtil decode finish length:${AmpFileUtil.convertStringToInt(ampString).size} ,spend time=${System.currentTimeMillis() - currentTime}") mIsDecodeFinish.postValue(true) ampList.value?.addAll(AmpFileUtil.convertStringToInt(ampString) ?: emptyList()) ampList.postValue(correctAmplitudeList(defaultDuration)) writeAmpData(path) }) fun genAmpFromSoundFileAndSetFunc( path: String?, playUri: Uri?, isRecycle: Boolean, decodeReadyCallback: (List<Int>) -> Unit, decodeFinishCallback: (String) -> Unit ) { if (path.isNullOrEmpty() || playUri == null) { DebugUtil.e(TAG, "genAmpFromSoundFileAndSetFunc path:$path or playUri:$playUri is null !") return } ampTaskMap.getAmplitudeListUtilByPath(path)?.apply { val isExistTask = addGetAmplitudeTask( path, playUri, isRecycle, WaveConstant.FULL_AMP_TYPE, decodeReadyCallback , decodeFinishCallback ) decodeFile(this@apply, path, playUri) } } fun dispatchReadyOrFinishCallBack( isFinishCallBack: Boolean = true, path: String, soundFile: SoundFile?, waveDataMap: WaveDataMap? ) { ampTaskMap[path]?.forEach { ampTaskData -> ampTaskData.apply { if (typeOrSegments.isNullOrEmpty()) { DebugUtil.e(TAG, "dispatchReadyCallBack typeOrSegments is null !") } else { ampReadyCallBack?.invoke(soundFile?.amplitudeList) } } } } ampReadyCallBack?.invoke(soundFile?.amplitudeList)为什么没有效果?
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flying_vip_521

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值