SongLyricActivity.kt 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package com.bestv.edu.activity
  2. import android.os.Bundle
  3. import androidx.appcompat.app.AppCompatActivity
  4. import com.bestv.edu.R
  5. import kotlinx.android.synthetic.main.activity_songe.*
  6. import kotlin.math.min
  7. /**
  8. * author : 程中强
  9. * e-mail : 740479946@qq.com
  10. * date : 2022/9/613:50
  11. * desc :
  12. * version: 1.0
  13. */
  14. class SongLyricActivity : AppCompatActivity() {
  15. private val lyric = "你发如雪\n" +
  16. "凄美了离别\n" +
  17. "我焚香感动了谁\n" +
  18. "邀明月让回忆皎洁\n" +
  19. "爱在月光下完美\n" +
  20. "你发如雪\n" +
  21. "纷飞了眼泪\n" +
  22. "我等待苍老了谁\n" +
  23. "红尘醉微醺的岁月\n" +
  24. "我用无悔\n" +
  25. "刻永世爱你的碑"
  26. override fun onCreate(savedInstanceState: Bundle?) {
  27. super.onCreate(savedInstanceState)
  28. setContentView(R.layout.activity_songe)
  29. // 设置歌词
  30. text.text = lyric
  31. start.setOnClickListener {
  32. val lyricList = lyric.split("\n")
  33. var startIndex = 0
  34. var delayTime = 0L
  35. // text.startPlayLine(
  36. // 0,
  37. // lyric.length,
  38. // 5000
  39. // )
  40. lyricList.forEach {
  41. val startIndexTemp = startIndex
  42. val duration = (1500 + Math.random() * 500).toLong()
  43. text.postDelayed(
  44. {
  45. text.startPlayLine(
  46. startIndexTemp,
  47. min(lyric.length, startIndexTemp + it.length + 1), // 因为有个换行符,所以 + 1
  48. duration
  49. )
  50. },
  51. delayTime
  52. )
  53. delayTime += duration + 50
  54. startIndex += it.length + 1
  55. }
  56. }
  57. end.setOnClickListener {
  58. text.stopPlay()
  59. }
  60. }
  61. }