|
|
@@ -36,6 +36,7 @@ import kotlinx.coroutines.suspendCancellableCoroutine
|
|
|
import java.util.Timer
|
|
|
import java.util.TimerTask
|
|
|
import kotlin.coroutines.resume
|
|
|
+import kotlin.math.max
|
|
|
import com.adealink.weparty.R as APP_R
|
|
|
|
|
|
class VoiceEditFragment : BaseFragment(R.layout.fragment_talent_voice_edit) {
|
|
|
@@ -52,11 +53,15 @@ class VoiceEditFragment : BaseFragment(R.layout.fragment_talent_voice_edit) {
|
|
|
|
|
|
private val profileViewModel by activityViewModels<ProfileViewModel> { ProfileViewModelFactory() }
|
|
|
|
|
|
- private var mTimer: Timer? = null
|
|
|
- private var times = 0
|
|
|
-
|
|
|
+ private var recordTimer: Timer? = null
|
|
|
+ private var recordTimes = 0
|
|
|
private var isRecording: Boolean = false
|
|
|
|
|
|
+
|
|
|
+ private var previewTimer: Timer? = null
|
|
|
+ private var previewTimes = 0
|
|
|
+ private var isPreviewPause = false
|
|
|
+
|
|
|
override fun initViews() {
|
|
|
super.initViews()
|
|
|
binding.btnRecord.onClick {
|
|
|
@@ -84,6 +89,8 @@ class VoiceEditFragment : BaseFragment(R.layout.fragment_talent_voice_edit) {
|
|
|
binding.clRecord.gone()
|
|
|
binding.clPreview.show()
|
|
|
binding.btnSubmit.isEnabled = true
|
|
|
+ previewTimes = editViewModel.getRecordDuration()
|
|
|
+ binding.tvPreviewDuration.text = formatMissTime(previewTimes)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -110,12 +117,29 @@ class VoiceEditFragment : BaseFragment(R.layout.fragment_talent_voice_edit) {
|
|
|
}
|
|
|
|
|
|
private fun audioPreview() {
|
|
|
+ stopRecord()
|
|
|
val voicePath = editViewModel.voiceDataLD.value?.path
|
|
|
if (voicePath.isNullOrEmpty()) {
|
|
|
return
|
|
|
}
|
|
|
AudioPlayer.getInstance().stopPlay()
|
|
|
- AudioPlayer.getInstance().startPlay(voicePath) { }
|
|
|
+ AudioPlayer.getInstance().startPlay(voicePath, object : AudioPlayer.Callback {
|
|
|
+ override fun onPlayStart() {
|
|
|
+ Log.d(TAG, "audioPreview, onPlayStart")
|
|
|
+ if (!isViewBindingValid()) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ onPreviewStart()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onCompletion(success: Boolean?) {
|
|
|
+ Log.d(TAG, "audioPreview, onCompletion")
|
|
|
+ if (!isViewBindingValid()) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resetPreview()
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
private fun startRecord() {
|
|
|
@@ -126,27 +150,17 @@ class VoiceEditFragment : BaseFragment(R.layout.fragment_talent_voice_edit) {
|
|
|
val audioPath = "${FilePath.audioPath}voice_edit.m4a"
|
|
|
AudioRecorder.startRecord(audioPath, object : AudioRecorderCallback {
|
|
|
override fun onStarted() {
|
|
|
- Log.d(TAG, "startAudioRecord, onStarted")
|
|
|
- isRecording = true
|
|
|
- if (mTimer == null) {
|
|
|
- mTimer = Timer()
|
|
|
+ if (!isViewBindingValid()) {
|
|
|
+ return
|
|
|
}
|
|
|
- mTimer?.schedule(object : TimerTask() {
|
|
|
- override fun run() {
|
|
|
- runOnUiThread {
|
|
|
- times++
|
|
|
- if (isViewBindingValid()) {
|
|
|
- binding.tvDuration.text = formatMissTime(times)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }, 0, 1000)
|
|
|
- binding.tvDurationTips.text =
|
|
|
- getCompatString(R.string.profile_edit_talent_voice_stop)
|
|
|
- binding.btnRecord.setImageResource(APP_R.drawable.profile_edit_talent_voice_stop_ic)
|
|
|
+ Log.d(TAG, "startAudioRecord, onStarted")
|
|
|
+ onRecordStart()
|
|
|
}
|
|
|
|
|
|
override fun onFinished(outputPath: String?) {
|
|
|
+ if (!isViewBindingValid()) {
|
|
|
+ return
|
|
|
+ }
|
|
|
Log.d(TAG, "startAudioRecord, onFinished:$outputPath")
|
|
|
resetRecord()
|
|
|
val duration = AudioRecorder.getDuration(outputPath)
|
|
|
@@ -159,6 +173,9 @@ class VoiceEditFragment : BaseFragment(R.layout.fragment_talent_voice_edit) {
|
|
|
}
|
|
|
|
|
|
override fun onFailed(errorCode: Int, errorMessage: String?) {
|
|
|
+ if (!isViewBindingValid()) {
|
|
|
+ return
|
|
|
+ }
|
|
|
Log.d(
|
|
|
TAG,
|
|
|
"startAudioRecord, onFailed, errorCode:$errorCode, errorMessage:$errorMessage"
|
|
|
@@ -180,6 +197,9 @@ class VoiceEditFragment : BaseFragment(R.layout.fragment_talent_voice_edit) {
|
|
|
}
|
|
|
|
|
|
override fun onCanceled() {
|
|
|
+ if (!isViewBindingValid()) {
|
|
|
+ return
|
|
|
+ }
|
|
|
Log.d(TAG, "startAudioRecord, onCanceled")
|
|
|
resetRecord()
|
|
|
binding.btnRecord.setImageResource(APP_R.drawable.profile_edit_talent_voice_record_ic)
|
|
|
@@ -224,6 +244,26 @@ class VoiceEditFragment : BaseFragment(R.layout.fragment_talent_voice_edit) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private fun onRecordStart() {
|
|
|
+ isRecording = true
|
|
|
+ if (recordTimer == null) {
|
|
|
+ recordTimer = Timer()
|
|
|
+ }
|
|
|
+ recordTimer?.schedule(object : TimerTask() {
|
|
|
+ override fun run() {
|
|
|
+ runOnUiThread {
|
|
|
+ recordTimes++
|
|
|
+ if (isViewBindingValid()) {
|
|
|
+ binding.tvDuration.text = formatMissTime(recordTimes)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, 0, 1000)
|
|
|
+ binding.tvDurationTips.text =
|
|
|
+ getCompatString(R.string.profile_edit_talent_voice_stop)
|
|
|
+ binding.btnRecord.setImageResource(APP_R.drawable.profile_edit_talent_voice_stop_ic)
|
|
|
+ }
|
|
|
+
|
|
|
private fun stopRecord() {
|
|
|
AudioRecorder.stopRecord()
|
|
|
}
|
|
|
@@ -231,13 +271,69 @@ class VoiceEditFragment : BaseFragment(R.layout.fragment_talent_voice_edit) {
|
|
|
@SuppressLint("SetTextI18n")
|
|
|
private fun resetRecord() {
|
|
|
isRecording = false
|
|
|
- mTimer?.cancel()
|
|
|
- mTimer = null
|
|
|
- times = 0
|
|
|
- if (isViewBindingValid()) {
|
|
|
- binding.tvDuration.text = "00:00"
|
|
|
- binding.tvDurationTips.text = getCompatString(R.string.profile_edit_talent_voice_record)
|
|
|
- binding.btnRecord.setImageResource(APP_R.drawable.profile_edit_talent_voice_record_ic)
|
|
|
+ recordTimer?.cancel()
|
|
|
+ recordTimer = null
|
|
|
+ recordTimes = 0
|
|
|
+ binding.tvDuration.text = "00:00"
|
|
|
+ binding.tvDurationTips.text = getCompatString(R.string.profile_edit_talent_voice_record)
|
|
|
+ binding.btnRecord.setImageResource(APP_R.drawable.profile_edit_talent_voice_record_ic)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private fun onPreviewStart() {
|
|
|
+ isPreviewPause = false
|
|
|
+ previewTimes = editViewModel.getRecordDuration()
|
|
|
+ if (previewTimer == null) {
|
|
|
+ previewTimer = Timer()
|
|
|
+ }
|
|
|
+ previewTimer?.schedule(object : TimerTask() {
|
|
|
+ override fun run() {
|
|
|
+ runOnUiThread {
|
|
|
+ if (isPreviewPause) {
|
|
|
+ return@runOnUiThread
|
|
|
+ }
|
|
|
+ previewTimes = max(previewTimes - 1, 0)
|
|
|
+ if (isViewBindingValid()) {
|
|
|
+ binding.tvPreviewDuration.text = formatMissTime(previewTimes)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, 0, 1000)
|
|
|
+
|
|
|
+ binding.btnPreview.setImageResource(APP_R.drawable.profile_edit_talent_voice_preview_pause_ic)
|
|
|
+ binding.btnPreview.onClick {
|
|
|
+ pausePreview()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun pausePreview() {
|
|
|
+ isPreviewPause = true
|
|
|
+ AudioPlayer.getInstance().pausePlay()
|
|
|
+ binding.btnPreview.setImageResource(APP_R.drawable.profile_edit_talent_voice_preview_ic)
|
|
|
+ binding.btnPreview.onClick {
|
|
|
+ resumePreview()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun resumePreview() {
|
|
|
+ isPreviewPause = false
|
|
|
+ AudioPlayer.getInstance().resumePlay()
|
|
|
+ binding.btnPreview.setImageResource(APP_R.drawable.profile_edit_talent_voice_preview_pause_ic)
|
|
|
+ binding.btnPreview.onClick {
|
|
|
+ pausePreview()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressLint("SetTextI18n")
|
|
|
+ private fun resetPreview() {
|
|
|
+ isPreviewPause = false
|
|
|
+ previewTimer?.cancel()
|
|
|
+ previewTimer = null
|
|
|
+ previewTimes = 0
|
|
|
+ binding.tvPreviewDuration.text = formatMissTime(editViewModel.getRecordDuration())
|
|
|
+ binding.btnPreview.setImageResource(APP_R.drawable.profile_edit_talent_voice_preview_ic)
|
|
|
+ binding.btnPreview.onClick {
|
|
|
+ audioPreview()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -261,8 +357,13 @@ class VoiceEditFragment : BaseFragment(R.layout.fragment_talent_voice_edit) {
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
super.onDestroyView()
|
|
|
- AudioPlayer.getInstance().stopPlay()
|
|
|
+ recordTimer?.cancel()
|
|
|
+ recordTimer = null
|
|
|
AudioRecorder.stopRecord()
|
|
|
+
|
|
|
+ previewTimer?.cancel()
|
|
|
+ previewTimer = null
|
|
|
+ AudioPlayer.getInstance().stopPlay()
|
|
|
}
|
|
|
|
|
|
}
|