크리에이터 커뮤니티 게시글 등록- 오디오 녹음

- 녹음 시간 : centiseconds 까지 표시
- 음질 조정 - sampleRate: 48k, bitrate: 256k
This commit is contained in:
klaus 2024-08-05 17:53:50 +09:00
parent e6e3df701d
commit 66fecf1509
2 changed files with 41 additions and 45 deletions

View File

@ -7,7 +7,8 @@ import android.media.MediaPlayer
import android.media.MediaRecorder import android.media.MediaRecorder
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.CountDownTimer import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -21,21 +22,20 @@ import kr.co.vividnext.sodalive.databinding.FragmentRecordingVoiceBinding
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.util.Locale import java.util.Locale
import kotlin.math.roundToInt
class RecordingVoiceFragment : BottomSheetDialogFragment() { class RecordingVoiceFragment : BottomSheetDialogFragment() {
private var listener: OnAudioRecordedListener? = null private var listener: OnAudioRecordedListener? = null
private var countDownTimer: CountDownTimer? = null
private var mediaRecorder: MediaRecorder? = null private var mediaRecorder: MediaRecorder? = null
private var mediaPlayer: MediaPlayer? = null private var mediaPlayer: MediaPlayer? = null
private var fileNameMedia = "" private var fileNameMedia = ""
private var second = -1
private var minute = 0
private var hour = 0
private lateinit var binding: FragmentRecordingVoiceBinding private lateinit var binding: FragmentRecordingVoiceBinding
private var startTime: Long = 0L
private val handler = Handler(Looper.getMainLooper())
interface OnAudioRecordedListener { interface OnAudioRecordedListener {
fun onAudioRecorded(file: File) fun onAudioRecorded(file: File)
} }
@ -154,12 +154,13 @@ class RecordingVoiceFragment : BottomSheetDialogFragment() {
} else { } else {
MediaRecorder() MediaRecorder()
}.apply { }.apply {
setAudioSource(MediaRecorder.AudioSource.MIC) setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
setOutputFile(fileNameMedia)
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4) setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setAudioEncoder(MediaRecorder.AudioEncoder.AAC) setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
setAudioEncodingBitRate(192000) setAudioChannels(2)
setAudioSamplingRate(48000) setAudioSamplingRate(48000)
setAudioEncodingBitRate(256000)
setOutputFile(fileNameMedia)
} }
try { try {
@ -254,47 +255,42 @@ class RecordingVoiceFragment : BottomSheetDialogFragment() {
} }
private fun startCountDownTimer() { private fun startCountDownTimer() {
countDownTimer = object : CountDownTimer(Long.MAX_VALUE, 1000) { startTime = System.currentTimeMillis()
override fun onTick(p0: Long) {
second += 1
binding.tvTimer.text = recordingTime()
if (second >= 180) { handler.post(object : Runnable {
override fun run() {
// 현재 시간에서 시작 시간을 빼서 경과 시간 계산
val elapsedMilliseconds = System.currentTimeMillis() - startTime
val centisecondsElapsed = (elapsedMilliseconds / 10.0).roundToInt()
// 경과한 시간에서 분, 초, 센티초 계산
val minutes = centisecondsElapsed / 6000
val seconds = (centisecondsElapsed % 6000) / 100
val centiseconds = centisecondsElapsed % 100
val time = String.format(
Locale.getDefault(),
"%02d:%02d.%02d",
minutes,
seconds,
centiseconds
)
binding.tvTimer.text = time
if (minutes >= 3) {
stopRecording() stopRecording()
} }
}
override fun onFinish() { // 10밀리초(1센티초)마다 실행
handler.postDelayed(this, 10)
} }
} })
countDownTimer!!.start()
}
private fun recordingTime(): String {
if (second == 60) {
minute += 1
second = 0
}
if (minute == 60) {
hour += 1
minute = 0
}
return String.format(Locale.getDefault(), "%02d:%02d:%02d", hour, minute, second)
} }
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private fun stopCountDownTimer() { private fun stopCountDownTimer() {
if (countDownTimer != null) { handler.removeCallbacksAndMessages(null)
countDownTimer!!.cancel() startTime = 0
countDownTimer = null binding.tvTimer.text = "00:00.00"
}
binding.tvTimer.text = "00:00:00"
second = -1
minute = 0
hour = 0
} }
} }

View File

@ -46,8 +46,8 @@
android:id="@+id/tv_timer" android:id="@+id/tv_timer"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:fontFamily="@font/gmarket_sans_light" android:fontFamily="@font/gmarket_sans_bold"
android:text="00:00:00" android:text="00:00.00"
android:textColor="@color/white" android:textColor="@color/white"
android:textSize="33.3sp" /> android:textSize="33.3sp" />