fixed a bug
This commit is contained in:
@@ -93,7 +93,7 @@ class MainActivity : Activity() {
|
|||||||
private val mainHandler = Handler(Looper.getMainLooper())
|
private val mainHandler = Handler(Looper.getMainLooper())
|
||||||
private var mediaRecorder: MediaRecorder? = null
|
private var mediaRecorder: MediaRecorder? = null
|
||||||
private var recordingFile: File? = null
|
private var recordingFile: File? = null
|
||||||
private val dubSegments = mutableListOf<File>()
|
private val dubSegments = mutableMapOf<Int, File>()
|
||||||
private var mergedDubFile: File? = null
|
private var mergedDubFile: File? = null
|
||||||
private var dubbingPlayback = false
|
private var dubbingPlayback = false
|
||||||
|
|
||||||
@@ -103,6 +103,7 @@ class MainActivity : Activity() {
|
|||||||
private var catalogStatusTextValue = "正在同步"
|
private var catalogStatusTextValue = "正在同步"
|
||||||
private var activeModule = Module.TRAIN
|
private var activeModule = Module.TRAIN
|
||||||
private var continuousPlaybackEnabled = false
|
private var continuousPlaybackEnabled = false
|
||||||
|
private var testSubtitleVisible = false
|
||||||
private var landscapeSubtitleVisible = true
|
private var landscapeSubtitleVisible = true
|
||||||
|
|
||||||
private val screenActionReceiver = object : BroadcastReceiver() {
|
private val screenActionReceiver = object : BroadcastReceiver() {
|
||||||
@@ -298,8 +299,12 @@ class MainActivity : Activity() {
|
|||||||
return LinearLayout(this).apply {
|
return LinearLayout(this).apply {
|
||||||
orientation = LinearLayout.VERTICAL
|
orientation = LinearLayout.VERTICAL
|
||||||
setBackgroundColor(COLOR_BACKGROUND)
|
setBackgroundColor(COLOR_BACKGROUND)
|
||||||
addView(createPlayerSection())
|
if (testSubtitleVisible) {
|
||||||
addView(createSentenceSection())
|
addView(createPlayerSection())
|
||||||
|
addView(createSentenceSection())
|
||||||
|
} else {
|
||||||
|
addView(createPlayerSection())
|
||||||
|
}
|
||||||
addView(createTestSection())
|
addView(createTestSection())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -334,8 +339,9 @@ class MainActivity : Activity() {
|
|||||||
}
|
}
|
||||||
activeModule = module
|
activeModule = module
|
||||||
controller.setContinuousPlayback(
|
controller.setContinuousPlayback(
|
||||||
if (activeModule == Module.TRAIN) continuousPlaybackEnabled else true
|
if (activeModule == Module.TRAIN) continuousPlaybackEnabled else false
|
||||||
)
|
)
|
||||||
|
landscapeSubtitleVisible = activeModule == Module.TRAIN || testSubtitleVisible
|
||||||
setContentView(createContentView())
|
setContentView(createContentView())
|
||||||
renderCatalog()
|
renderCatalog()
|
||||||
refreshCurrentUi()
|
refreshCurrentUi()
|
||||||
@@ -767,6 +773,30 @@ class MainActivity : Activity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val subtitleToggle = LinearLayout(this).apply {
|
||||||
|
orientation = LinearLayout.HORIZONTAL
|
||||||
|
gravity = Gravity.CENTER_VERTICAL
|
||||||
|
setPadding(0, 10.dp, 0, 0)
|
||||||
|
addView(TextView(this@MainActivity).apply {
|
||||||
|
text = "显示字幕"
|
||||||
|
setTextColor(COLOR_TEXT_DARK)
|
||||||
|
textSize = 13f
|
||||||
|
typeface = Typeface.DEFAULT_BOLD
|
||||||
|
layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f)
|
||||||
|
})
|
||||||
|
addView(Switch(this@MainActivity).apply {
|
||||||
|
isChecked = testSubtitleVisible
|
||||||
|
buttonTintList = ColorStateList.valueOf(COLOR_ACCENT)
|
||||||
|
setOnCheckedChangeListener { _, checked ->
|
||||||
|
testSubtitleVisible = checked
|
||||||
|
landscapeSubtitleVisible = checked
|
||||||
|
setContentView(createContentView())
|
||||||
|
refreshCurrentUi()
|
||||||
|
refreshTestUi()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
playDubButton = dubButton("播放配音", {}).apply { isEnabled = false }
|
playDubButton = dubButton("播放配音", {}).apply { isEnabled = false }
|
||||||
mergeDubButton = dubButton("生成配音", {}).apply { isEnabled = false }
|
mergeDubButton = dubButton("生成配音", {}).apply { isEnabled = false }
|
||||||
shareDubButton = dubButton("分享配音", {}).apply { isEnabled = false }
|
shareDubButton = dubButton("分享配音", {}).apply { isEnabled = false }
|
||||||
@@ -798,6 +828,7 @@ class MainActivity : Activity() {
|
|||||||
textSize = 16f
|
textSize = 16f
|
||||||
typeface = Typeface.DEFAULT_BOLD
|
typeface = Typeface.DEFAULT_BOLD
|
||||||
})
|
})
|
||||||
|
addView(subtitleToggle, 0)
|
||||||
addView(testStatusText)
|
addView(testStatusText)
|
||||||
addView(controlRow)
|
addView(controlRow)
|
||||||
addView(recordButton)
|
addView(recordButton)
|
||||||
@@ -816,10 +847,15 @@ class MainActivity : Activity() {
|
|||||||
if (!::testStatusText.isInitialized) {
|
if (!::testStatusText.isInitialized) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
applyTestSubtitleVisibility()
|
||||||
recordButton.text = if (mediaRecorder == null) "开始录音" else "停止并评测"
|
recordButton.text = if (mediaRecorder == null) "开始录音" else "停止并评测"
|
||||||
val segmentLabel = if (dubSegments.isEmpty()) "" else " · 已保存 ${dubSegments.size} 段"
|
|
||||||
val item = controller.currentTrainingItem()
|
val item = controller.currentTrainingItem()
|
||||||
val sentence = controller.currentSentence()
|
val sentence = controller.currentSentence()
|
||||||
|
val currentRecording = sentence?.let { dubSegments[it.index] }
|
||||||
|
val segmentLabel = when {
|
||||||
|
currentRecording != null -> " · 本句已录音,重录将覆盖"
|
||||||
|
else -> ""
|
||||||
|
}
|
||||||
testStatusText.text = if (item == null || sentence == null) {
|
testStatusText.text = if (item == null || sentence == null) {
|
||||||
"请先在训练模块选择课程$segmentLabel"
|
"请先在训练模块选择课程$segmentLabel"
|
||||||
} else {
|
} else {
|
||||||
@@ -828,6 +864,20 @@ class MainActivity : Activity() {
|
|||||||
refreshDubButtons()
|
refreshDubButtons()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun applyTestSubtitleVisibility() {
|
||||||
|
if (activeModule != Module.TEST) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
landscapeSubtitleVisible = testSubtitleVisible
|
||||||
|
if (!::sentenceText.isInitialized || !::progressBar.isInitialized) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val visibility = if (testSubtitleVisible) View.VISIBLE else View.GONE
|
||||||
|
sentenceMetaText.visibility = visibility
|
||||||
|
sentenceText.visibility = visibility
|
||||||
|
progressBar.visibility = visibility
|
||||||
|
}
|
||||||
|
|
||||||
private fun startRecording() {
|
private fun startRecording() {
|
||||||
controller.pause()
|
controller.pause()
|
||||||
if (!::testStatusText.isInitialized) {
|
if (!::testStatusText.isInitialized) {
|
||||||
@@ -853,10 +903,20 @@ class MainActivity : Activity() {
|
|||||||
recorder.setOutputFile(file.absolutePath)
|
recorder.setOutputFile(file.absolutePath)
|
||||||
recorder.prepare()
|
recorder.prepare()
|
||||||
recorder.start()
|
recorder.start()
|
||||||
|
val sentence = controller.currentSentence()
|
||||||
|
if (sentence != null) {
|
||||||
|
controller.seekTo(sentence.startMs)
|
||||||
|
controller.setVolume(0f)
|
||||||
|
}
|
||||||
|
controller.play()
|
||||||
mediaRecorder = recorder
|
mediaRecorder = recorder
|
||||||
recordingFile = file
|
recordingFile = file
|
||||||
recordButton.text = "停止并保存"
|
recordButton.text = "停止并保存"
|
||||||
testStatusText.text = "正在录音…读完当前句子后点击“停止并保存”"
|
testStatusText.text = if (sentence == null) {
|
||||||
|
"正在录音…读完当前句子后点击“停止并保存”"
|
||||||
|
} else {
|
||||||
|
"正在录音…请对照静音视频口形,读完点击“停止并保存”"
|
||||||
|
}
|
||||||
} catch (error: Throwable) {
|
} catch (error: Throwable) {
|
||||||
testStatusText.text = "录音启动失败:${error.message.orEmpty()}"
|
testStatusText.text = "录音启动失败:${error.message.orEmpty()}"
|
||||||
runCatching { recorder.release() }
|
runCatching { recorder.release() }
|
||||||
@@ -878,7 +938,18 @@ class MainActivity : Activity() {
|
|||||||
} finally {
|
} finally {
|
||||||
runCatching { recorder.release() }
|
runCatching { recorder.release() }
|
||||||
}
|
}
|
||||||
dubSegments.add(file)
|
controller.pause()
|
||||||
|
controller.setVolume(1f)
|
||||||
|
val sentenceIndex = controller.currentSentence()?.index
|
||||||
|
if (sentenceIndex == null) {
|
||||||
|
testStatusText.text = "没有可归属的句子,本次录音已丢弃"
|
||||||
|
file.delete()
|
||||||
|
recordingFile = null
|
||||||
|
refreshTestUi()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dubSegments[sentenceIndex]?.delete()
|
||||||
|
dubSegments[sentenceIndex] = file
|
||||||
mergedDubFile = null
|
mergedDubFile = null
|
||||||
recordingFile = null
|
recordingFile = null
|
||||||
submitAssessment(Uri.fromFile(file))
|
submitAssessment(Uri.fromFile(file))
|
||||||
@@ -907,9 +978,11 @@ class MainActivity : Activity() {
|
|||||||
playDubButton.isEnabled = dubSegments.isNotEmpty() && !dubbingPlayback
|
playDubButton.isEnabled = dubSegments.isNotEmpty() && !dubbingPlayback
|
||||||
mergeDubButton.isEnabled = dubSegments.size >= 2 && !dubbingPlayback
|
mergeDubButton.isEnabled = dubSegments.size >= 2 && !dubbingPlayback
|
||||||
shareDubButton.isEnabled = mergedDubFile?.exists() == true && !dubbingPlayback
|
shareDubButton.isEnabled = mergedDubFile?.exists() == true && !dubbingPlayback
|
||||||
|
val activeAudio = controller.currentSentence()?.let { dubSegments[it.index] }
|
||||||
playDubButton.text = when {
|
playDubButton.text = when {
|
||||||
dubbingPlayback -> "停止配音"
|
dubbingPlayback -> "停止配音"
|
||||||
mergedDubFile?.exists() == true -> "播放配音"
|
mergedDubFile?.exists() == true -> "播放配音"
|
||||||
|
activeAudio != null -> "播放本句配音"
|
||||||
else -> "播放录音(${dubSegments.size})"
|
else -> "播放录音(${dubSegments.size})"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -919,7 +992,14 @@ class MainActivity : Activity() {
|
|||||||
stopDubPlayback("已停止配音播放")
|
stopDubPlayback("已停止配音播放")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
startDubPlayback(mergedDubFile ?: dubSegments.lastOrNull() ?: return)
|
startDubPlayback(mergedDubFile ?: activeAudioForPlayback() ?: return)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun activeAudioForPlayback(): File? {
|
||||||
|
if (mergedDubFile != null) {
|
||||||
|
return mergedDubFile
|
||||||
|
}
|
||||||
|
return controller.currentSentence()?.let { sentence -> dubSegments[sentence.index] }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startDubPlayback(audioFile: File) {
|
private fun startDubPlayback(audioFile: File) {
|
||||||
@@ -978,7 +1058,7 @@ class MainActivity : Activity() {
|
|||||||
var muxerAudioTrack = -1
|
var muxerAudioTrack = -1
|
||||||
var presentationTimeUs = 0L
|
var presentationTimeUs = 0L
|
||||||
|
|
||||||
dubSegments.forEach { segment ->
|
dubSegments.toSortedMap().values.forEach { segment ->
|
||||||
val extractor = MediaExtractor().apply { setDataSource(segment.absolutePath) }
|
val extractor = MediaExtractor().apply { setDataSource(segment.absolutePath) }
|
||||||
try {
|
try {
|
||||||
val trackIndex = (0 until extractor.trackCount).firstOrNull { index ->
|
val trackIndex = (0 until extractor.trackCount).firstOrNull { index ->
|
||||||
|
|||||||
Reference in New Issue
Block a user