feat: shutter sound
This commit is contained in:
1 parent
e263b58f0c
commit
2cbd99d168
5 files changed
+107
No files matched your search
Binary file not shown.
@@ -64,6 +64,9 @@ class Camera2Controller(private val context: Context) {
|
||||
|
||||
// 图片拍摄回调(携带 CaptureInfo)
|
||||
var onImageCaptured: ((Image, CaptureInfo) -> Unit)? = null
|
||||
|
||||
// 快门音效播放回调
|
||||
var onPlayShutterSound: (() -> Unit)? = null
|
||||
|
||||
private val previewCallback = object : CameraCaptureSession.CaptureCallback() {
|
||||
override fun onCaptureCompleted(
|
||||
@@ -991,6 +994,9 @@ class Camera2Controller(private val context: Context) {
|
||||
val device = cameraDevice ?: return
|
||||
val reader = imageReader ?: return
|
||||
|
||||
// 播放快门音效
|
||||
onPlayShutterSound?.invoke()
|
||||
|
||||
_state.value = _state.value.copy(isCapturing = true)
|
||||
|
||||
try {
|
||||
|
||||
@@ -47,6 +47,7 @@ fun SettingsScreen(
|
||||
modifier = modifier
|
||||
.fillMaxSize()
|
||||
.background(backgroundColor)
|
||||
.padding(top = 32.dp)
|
||||
) {
|
||||
// 顶部标题栏
|
||||
TopAppBar(
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.hinnka.mycamera.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.media.MediaPlayer
|
||||
import android.util.Log
|
||||
import java.io.IOException
|
||||
|
||||
/**
|
||||
* 快门音效播放器
|
||||
*
|
||||
* 负责播放拍照时的快门音效
|
||||
*/
|
||||
class ShutterSoundPlayer(private val context: Context) {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "ShutterSoundPlayer"
|
||||
private const val SHUTTER_SOUND_PATH = "shutter.mp3"
|
||||
}
|
||||
|
||||
private var mediaPlayer: MediaPlayer? = null
|
||||
private var isInitialized = false
|
||||
|
||||
init {
|
||||
initializePlayer()
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化 MediaPlayer
|
||||
*/
|
||||
private fun initializePlayer() {
|
||||
try {
|
||||
mediaPlayer = MediaPlayer().apply {
|
||||
val afd = context.assets.openFd(SHUTTER_SOUND_PATH)
|
||||
setDataSource(afd.fileDescriptor, afd.startOffset, afd.length)
|
||||
afd.close()
|
||||
prepare()
|
||||
isInitialized = true
|
||||
}
|
||||
Log.d(TAG, "Shutter sound player initialized")
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Failed to initialize shutter sound player", e)
|
||||
isInitialized = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放快门音效
|
||||
*/
|
||||
fun play() {
|
||||
if (!isInitialized) {
|
||||
Log.w(TAG, "Player not initialized, skipping playback")
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
mediaPlayer?.let { player ->
|
||||
if (player.isPlaying) {
|
||||
// 如果正在播放,重新开始
|
||||
player.seekTo(0)
|
||||
} else {
|
||||
player.start()
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to play shutter sound", e)
|
||||
// 尝试重新初始化
|
||||
release()
|
||||
initializePlayer()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放资源
|
||||
*/
|
||||
fun release() {
|
||||
try {
|
||||
mediaPlayer?.release()
|
||||
mediaPlayer = null
|
||||
isInitialized = false
|
||||
Log.d(TAG, "Shutter sound player released")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to release shutter sound player", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ import com.hinnka.mycamera.lut.LutInfo
|
||||
import com.hinnka.mycamera.lut.LutManager
|
||||
import com.hinnka.mycamera.utils.BitmapUtils.toByteArray
|
||||
import com.hinnka.mycamera.utils.OrientationObserver
|
||||
import com.hinnka.mycamera.utils.ShutterSoundPlayer
|
||||
import com.hinnka.mycamera.utils.YuvProcessor
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.*
|
||||
@@ -62,6 +63,9 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
private val frameManager = FrameManager(application)
|
||||
private val frameRenderer = FrameRenderer(application)
|
||||
private val photoProcessor = PhotoProcessor(lutManager, lutImageProcessor, frameManager, frameRenderer)
|
||||
|
||||
// 快门音效播放器
|
||||
private val shutterSoundPlayer = ShutterSoundPlayer(application)
|
||||
|
||||
val state: StateFlow<CameraState> = cameraController.state
|
||||
|
||||
@@ -111,6 +115,16 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
}
|
||||
|
||||
// 设置快门音效回调
|
||||
cameraController.onPlayShutterSound = {
|
||||
viewModelScope.launch {
|
||||
val enabled = shutterSoundEnabled.firstOrNull() ?: true
|
||||
if (enabled) {
|
||||
shutterSoundPlayer.play()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化 LUT 管理器
|
||||
lutManager.initialize()
|
||||
availableLutList = lutManager.getAvailableLuts()
|
||||
@@ -561,5 +575,6 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
lutManager.clearCache()
|
||||
lutImageProcessor.release()
|
||||
frameManager.clearCache()
|
||||
shutterSoundPlayer.release()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user