package com.netfluke.sergey.manulmeow import android.os.Bundle import android.support.design.widget.Snackbar import android.support.v7.app.AppCompatActivity import android.view.Menu import android.view.MenuItem import android.app.Activity import android.content.res.Configuration import android.media.AudioManager import android.media.SoundPool import android.media.SoundPool.OnLoadCompleteListener import android.os.Build import android.util.Log import android.view.MotionEvent import android.view.View import android.view.View.OnTouchListener import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity(), OnTouchListener { lateinit var soundPool : SoundPool private var soundID : Int = 0 var loaded : Boolean = false var volume : Float = 0f var currentSnd : Int = 0 var sounds = listOf( R.raw.sound1, R.raw.sound2, R.raw.sound3 ) var lastSnd : Int = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView( if (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) R.layout.activity_main else R.layout.activity_main_land ) setSupportActionBar(toolbar) setupSound() // FIX: last meow should be remembered, even if touch advanced it to next meow fab.setOnClickListener { view -> Snackbar.make(view, "Repeat last meow -> ", Snackbar.LENGTH_LONG) .setAction("Meow", { v -> currentSnd=lastSnd ; playSound() }).show() } var view : View = findViewById(R.id.textView1) view.setOnTouchListener(this) view = findViewById(R.id.imgView1) view.setOnTouchListener(this) } private fun setupSound() { // Set the hardware buttons to control the music this.setVolumeControlStream(AudioManager.STREAM_MUSIC) // Load the sound if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { soundPool = SoundPool.Builder().build() } else { SoundPool(10, AudioManager.STREAM_MUSIC, 0) } soundPool.setOnLoadCompleteListener( { sp: SoundPool, sampleId: Int, status: Int -> sp.play(sampleId, volume, volume, 1, 0, 1f) Log.e("Test", "Played sound") loaded = true }) // load the first sound soundID = soundPool.load(this, sounds[currentSnd], 1) } override fun onTouch(v: View, event: MotionEvent) : Boolean { if (event.action == MotionEvent.ACTION_DOWN) { v.performClick() // Is the sound loaded already? if (loaded) { playSound() getNextSound() } } return false } private fun playSound() { // Getting the user sound settings val audioManager : AudioManager = getSystemService(AUDIO_SERVICE) as AudioManager val actualVolume : Int = audioManager .getStreamVolume(AudioManager.STREAM_MUSIC) val maxVolume : Int = audioManager .getStreamMaxVolume(AudioManager.STREAM_MUSIC) volume = actualVolume.toFloat() / maxVolume.toFloat() // will be played when loaded by OnLoadCompleteListener soundID = soundPool.load(this, sounds[currentSnd], 1) lastSnd = currentSnd } override fun onCreateOptionsMenu(menu: Menu): Boolean { // Inflate the menu; this adds items to the action bar if it is present. menuInflater.inflate(R.menu.menu_main, menu) return true } override fun onOptionsItemSelected(item: MenuItem): Boolean { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. return when (item.itemId) { R.id.action_meow -> { currentSnd = 0; playSound(); true } R.id.action_marr -> { currentSnd = 1; playSound(); true } R.id.action_mreow -> { currentSnd = 2; playSound(); true } else -> super.onOptionsItemSelected(item) } } private fun getNextSound() { currentSnd++ if( currentSnd >= sounds.size ) currentSnd = 0 } }