@@ -56,9 +56,9 @@ class AudioPlayer(private val context: Context) {
5656
5757 /* *
5858 * 从字节数组播放音频
59- *
59+ *
6060 * 此方法内部自动在后台线程处理 IO 操作,无需在协程中调用
61- *
61+ *
6262 * @param audioData 音频数据
6363 * @param fileExtension 文件扩展名(如 "mp3", "wav")
6464 * @param callback 播放回调
@@ -90,7 +90,7 @@ class AudioPlayer(private val context: Context) {
9090 playFromFile(tempFile.absolutePath)
9191
9292 } catch (e: Exception ) {
93- " 播放失败: ${e.message} " .logE(TAG , " 播放失败: ${e.message} " )
93+ " 播放失败: ${e.message} " .logE(TAG )
9494 callback?.onError(" 播放失败: ${e.message} " , e)
9595 }
9696 }.start()
@@ -115,18 +115,18 @@ class AudioPlayer(private val context: Context) {
115115 )
116116
117117 setOnPreparedListener {
118- " 播放器准备完成" .logD(TAG , " 播放器准备完成 " )
118+ " 播放器准备完成" .logD(TAG )
119119 callback?.onPrepared()
120120 }
121121
122122 setOnCompletionListener {
123- " 播放完成" .logD(TAG , " 播放完成 " )
123+ " 播放完成" .logD(TAG )
124124 callback?.onCompletion()
125125 cleanup()
126126 }
127127
128128 setOnErrorListener { mp, what, extra ->
129- " 播放器错误: what=$what , extra=$extra " .logE(TAG , " 播放器错误: what= $what , extra= $extra " )
129+ " 播放器错误: what=$what , extra=$extra " .logE(TAG )
130130 callback?.onError(" 播放器错误: what=$what , extra=$extra " , null )
131131 cleanup()
132132 true
@@ -137,14 +137,14 @@ class AudioPlayer(private val context: Context) {
137137 start()
138138
139139 callback?.onStart()
140- " 开始播放: $filePath " .logD(TAG , " 开始播放: $filePath " )
140+ " 开始播放: $filePath " .logD(TAG )
141141 }
142142
143143 } catch (e: IOException ) {
144- " 播放文件失败: ${e.message} " .logE(TAG , " 播放文件失败: ${e.message} " )
144+ " 播放文件失败: ${e.message} " .logE(TAG )
145145 callback?.onError(" 播放文件失败: ${e.message} " , e)
146146 } catch (e: IllegalStateException ) {
147- " 播放器状态异常: ${e.message} " .logE(TAG , " 播放器状态异常: ${e.message} " )
147+ " 播放器状态异常: ${e.message} " .logE(TAG )
148148 callback?.onError(" 播放器状态异常: ${e.message} " , e)
149149 }
150150 }
@@ -157,11 +157,11 @@ class AudioPlayer(private val context: Context) {
157157 mediaPlayer?.let {
158158 if (it.isPlaying) {
159159 it.pause()
160- " 播放已暂停" .logD(TAG , " 播放已暂停 " )
160+ " 播放已暂停" .logD(TAG )
161161 }
162162 }
163163 } catch (e: IllegalStateException ) {
164- " 暂停失败: ${e.message} " .logE(TAG , " 暂停失败: ${e.message} " )
164+ " 暂停失败: ${e.message} " .logE(TAG )
165165 }
166166 }
167167
@@ -173,11 +173,11 @@ class AudioPlayer(private val context: Context) {
173173 mediaPlayer?.let {
174174 if (! it.isPlaying) {
175175 it.start()
176- " 播放已恢复" .logD(TAG , " 播放已恢复 " )
176+ " 播放已恢复" .logD(TAG )
177177 }
178178 }
179179 } catch (e: IllegalStateException ) {
180- " 恢复播放失败: ${e.message} " .logE(TAG , " 恢复播放失败: ${e.message} " )
180+ " 恢复播放失败: ${e.message} " .logE(TAG )
181181 }
182182 }
183183
@@ -189,12 +189,12 @@ class AudioPlayer(private val context: Context) {
189189 mediaPlayer?.let {
190190 if (it.isPlaying) {
191191 it.stop()
192- " 播放已停止" .logD(TAG , " 播放已停止 " )
192+ " 播放已停止" .logD(TAG )
193193 }
194194 }
195195 cleanup()
196196 } catch (e: IllegalStateException ) {
197- " 停止失败: ${e.message} " .logE(TAG , " 停止失败: ${e.message} " )
197+ " 停止失败: ${e.message} " .logE(TAG )
198198 }
199199 }
200200
@@ -206,7 +206,7 @@ class AudioPlayer(private val context: Context) {
206206 try {
207207 mediaPlayer?.seekTo(position)
208208 } catch (e: IllegalStateException ) {
209- " 跳转失败: ${e.message} " .logE(TAG , " 跳转失败: ${e.message} " )
209+ " 跳转失败: ${e.message} " .logE(TAG )
210210 }
211211 }
212212
@@ -254,7 +254,7 @@ class AudioPlayer(private val context: Context) {
254254 try {
255255 mediaPlayer?.setVolume(leftVolume, rightVolume)
256256 } catch (e: IllegalStateException ) {
257- " 设置音量失败: ${e.message} " .logE(TAG , " 设置音量失败: ${e.message} " )
257+ " 设置音量失败: ${e.message} " .logE(TAG )
258258 }
259259 }
260260
@@ -266,7 +266,7 @@ class AudioPlayer(private val context: Context) {
266266 try {
267267 mediaPlayer?.isLooping = looping
268268 } catch (e: IllegalStateException ) {
269- " 设置循环失败: ${e.message} " .logE(TAG , " 设置循环失败: ${e.message} " )
269+ " 设置循环失败: ${e.message} " .logE(TAG )
270270 }
271271 }
272272
@@ -276,7 +276,7 @@ class AudioPlayer(private val context: Context) {
276276 fun release () {
277277 cleanup()
278278 callback = null
279- " 播放器已释放" .logD(TAG , " 播放器已释放 " )
279+ " 播放器已释放" .logD(TAG )
280280 }
281281
282282 /* *
@@ -301,7 +301,7 @@ class AudioPlayer(private val context: Context) {
301301 }
302302 mediaPlayer = null
303303 } catch (e: Exception ) {
304- " 释放播放器失败: ${e.message} " .logE(TAG , " 释放播放器失败: ${e.message} " )
304+ " 释放播放器失败: ${e.message} " .logE(TAG )
305305 }
306306 }
307307
@@ -313,30 +313,30 @@ class AudioPlayer(private val context: Context) {
313313 currentTempFile?.let {
314314 if (it.exists()) {
315315 it.delete()
316- " 临时文件已删除: ${it.absolutePath} " .logD(TAG , " 临时文件已删除: ${it.absolutePath} " )
316+ " 临时文件已删除: ${it.absolutePath} " .logD(TAG )
317317 }
318318 }
319319 currentTempFile = null
320320 } catch (e: Exception ) {
321- " 删除临时文件失败: ${e.message} " .logE(TAG , " 删除临时文件失败: ${e.message} " )
321+ " 删除临时文件失败: ${e.message} " .logE(TAG )
322322 }
323323 }
324324}
325325
326326/* *
327327 * 音频播放器管理器(单例)
328328 * 提供全局的音频播放器实例
329- *
329+ *
330330 * 注意:
331331 * 1. 单例播放器会在应用进程结束时自动被系统回收,通常不需要手动释放
332332 * 2. 如需在低内存时释放资源,可在 Application.onLowMemory() 或 onTrimMemory() 中调用 release()
333333 * 3. Activity 中使用的 AudioPlayer 实例应该在 onDestroy() 中手动释放
334334 */
335335object AudioPlayerManager {
336-
336+
337337 @Volatile
338338 private var instance: AudioPlayer ? = null
339-
339+
340340 /* *
341341 * 获取音频播放器实例
342342 * @param context 上下文
@@ -348,10 +348,10 @@ object AudioPlayerManager {
348348 }
349349 }
350350 }
351-
351+
352352 /* *
353353 * 释放全局播放器
354- *
354+ *
355355 * 使用场景:
356356 * - 在 Application.onLowMemory() 中调用以释放内存
357357 * - 在 Application.onTrimMemory() 中根据内存级别调用
@@ -363,14 +363,14 @@ object AudioPlayerManager {
363363 instance = null
364364 }
365365 }
366-
366+
367367 /* *
368368 * 检查播放器是否已初始化
369369 */
370370 fun isInitialized (): Boolean {
371371 return instance != null
372372 }
373-
373+
374374 /* *
375375 * 停止播放但不释放播放器
376376 * 适用于需要暂时停止播放但后续可能继续使用的场景
0 commit comments