You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

140 lines
4.7 KiB

  1. package de.mlte.soundboard
  2. import android.animation.ObjectAnimator
  3. import android.app.Activity
  4. import android.content.Context
  5. import android.content.Intent
  6. import android.media.MediaPlayer
  7. import android.net.Uri
  8. import android.os.Bundle
  9. import android.support.v7.app.AppCompatActivity
  10. import android.view.LayoutInflater
  11. import android.view.animation.LinearInterpolator
  12. import android.widget.FrameLayout
  13. import android.widget.GridLayout
  14. import android.widget.ProgressBar
  15. import android.widget.TextView
  16. import java.io.BufferedInputStream
  17. import java.io.BufferedOutputStream
  18. class MainActivity : AppCompatActivity() {
  19. override fun onCreate(savedInstanceState: Bundle?) {
  20. super.onCreate(savedInstanceState)
  21. setContentView(R.layout.activity_main)
  22. duplicateButton()
  23. loadPreferences()
  24. var player: MediaPlayer? = null
  25. val progressBar = findViewById<ProgressBar>(R.id.progress_bar)
  26. val objectAnimator = ObjectAnimator.ofInt(progressBar, "progress", progressBar.getProgress(), 1000)
  27. objectAnimator.interpolator = LinearInterpolator()
  28. progressBar.max = 1000
  29. objectAnimator.addUpdateListener({ valueAnimator ->
  30. val progress = valueAnimator.animatedValue as Int
  31. progressBar.progress = progress
  32. })
  33. var playing = false
  34. val btn = findViewById<TextView>(R.id.text_view_button)
  35. btn.setOnClickListener {
  36. if (playing) {
  37. player?.let { mp ->
  38. if (mp.isPlaying) {
  39. mp.stop()
  40. }
  41. mp.reset()
  42. mp.release()
  43. }
  44. objectAnimator.cancel()
  45. playing = false
  46. progressBar.progress = 0
  47. } else {
  48. val file = getFileStreamPath("audio")
  49. if (file.exists()) {
  50. val mp = MediaPlayer.create(this, Uri.fromFile(file))
  51. mp.setOnCompletionListener {
  52. progressBar.progress = 0
  53. mp.reset()
  54. mp.release()
  55. playing = false
  56. }
  57. mp.start()
  58. player = mp
  59. playing = true
  60. progressBar.progress = 0
  61. objectAnimator.setDuration(mp.duration.toLong()).start()
  62. }
  63. }
  64. }
  65. btn.setOnLongClickListener {
  66. val intent = Intent(baseContext, EditActivity::class.java)
  67. intent.putExtra("caption", btn.text)
  68. startActivityForResult(intent, 1234)
  69. true
  70. }
  71. }
  72. private fun duplicateButton() {
  73. val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
  74. val parent = findViewById<GridLayout>(R.id.grid_layout)
  75. parent.columnCount = 2
  76. for (row in 0..1) {
  77. for (col in 0..1) {
  78. val soundButton = SoundButton(this, col, row)
  79. parent.addView(soundButton)
  80. }
  81. }
  82. }
  83. private fun loadPreferences() {
  84. val preferences = getPreferences(Context.MODE_PRIVATE)
  85. val caption = preferences.getString("caption", "")
  86. val btn = findViewById<TextView>(R.id.text_view_button)
  87. btn.setText(caption)
  88. }
  89. override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  90. super.onActivityResult(requestCode, resultCode, data)
  91. if (requestCode == 1234 && resultCode == Activity.RESULT_OK && data != null) {
  92. val btn = findViewById<TextView>(R.id.text_view_button)
  93. val caption = data.getStringExtra("caption")
  94. if (caption != null) {
  95. btn.setText(caption)
  96. }
  97. val uri = data.getParcelableExtra<Uri>("uri")
  98. savePreferences(caption, uri)
  99. }
  100. }
  101. private fun savePreferences(caption: String, uri: Uri?) {
  102. val btn = findViewById<TextView>(R.id.text_view_button)
  103. val editor = getPreferences(Context.MODE_PRIVATE).edit()
  104. editor.putString("caption", btn.text.toString())
  105. editor.commit()
  106. uri?.let { uri ->
  107. grantUriPermission(getPackageName(), uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
  108. val output = BufferedOutputStream(openFileOutput("audio", Context.MODE_PRIVATE))
  109. val input = BufferedInputStream(getContentResolver().openInputStream(uri))
  110. try {
  111. val buf = ByteArray(1024)
  112. input.read(buf)
  113. do {
  114. output.write(buf)
  115. } while (input.read(buf) !== -1)
  116. } finally {
  117. input.close()
  118. output.close()
  119. }
  120. }
  121. }
  122. }