Advertisement
suzukigenzan

Untitled

Jan 20th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import android.content.Intent
  2. import androidx.appcompat.app.AppCompatActivity
  3. import android.os.Bundle
  4. import android.view.View
  5. import kotlinx.android.synthetic.main.activity_main.*
  6. import android.media.SoundPool
  7. import android.media.AudioAttributes
  8.  
  9. class MainActivity : AppCompatActivity() {
  10.  
  11. private lateinit var soundPool: SoundPool
  12. private var ro = 0
  13.  
  14. override fun onCreate(savedInstanceState: Bundle?) {
  15. super.onCreate(savedInstanceState)
  16. setContentView(R.layout.activity_harm)
  17.  
  18. //ボタン
  19.  
  20. btn1.setOnClickListener{ ButtonTapped(it) }
  21. }
  22.  
  23. fun ButtonTapped(view: View?) {
  24. val intent = Intent(this, HarmActivity::class.java)
  25. startActivity(intent)
  26.  
  27. //オーディオ
  28.  
  29. val audioAttributes = AudioAttributes.Builder()
  30.  
  31. // CONTENT_TYPE_MUSIC
  32. // CONTENT_TYPE_SPEECH, etc.
  33. .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
  34. .build()
  35.  
  36. soundPool = SoundPool.Builder()
  37. .setAudioAttributes(audioAttributes)
  38. // ストリーム数に応じて
  39. .setMaxStreams(1)
  40. .build()
  41.  
  42. // MP3をロードしておく
  43. ro = soundPool.load(this, R.raw.ro, 1)
  44.  
  45. // ボタンで再生
  46. btn1.setOnClickListener {
  47. soundPool.play(ro, 1.0f, 1.0f, 0, 0, 1.0f)
  48. }
  49.  
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement