0

I'm trying to play very short wav files (around 0.5 seconds each) at a very precise moments.

I've loaded a wav file and tried to play it when it's looped:

private val player = MediaPlayer()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val afd = resources.openRawResourceFd(R.raw.sub_kick_36_045)
        val fileDescriptor = afd.fileDescriptor

        try {
            player.setDataSource(
                fileDescriptor, afd.startOffset,
                afd.length
            )
            player.isLooping = true
            player.prepare()

        } catch (ex: IOException) {
            Log.d("Activity", ex.message)
        }

        play.setOnClickListener {
            player.start()
        }

        stop.setOnClickListener {
            player.stop()
        }
    }

Sound is playing, however I have a significant delay when playing loops.

I've found an app which plays sound very accurately but it uses much more complicated process to play files and files itself are very peculiar(not wav)

https://github.com/tube42/drumon

Maybe you guys can advice me how I can play sounds (0.5 sec - 5 sec long) instantly, with a minimum delay. (using some java library or something)

nutella_eater
  • 3,393
  • 3
  • 27
  • 46

1 Answers1

0

you audio is very short,Maybe you could be to try "SoundPool".