MediaPlayer in Android works with a delay [duplicate]












0
















This question already has an answer here:




  • Not able to achieve Gapless audio looping so far on Android

    6 answers




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)










share|improve this question













marked as duplicate by dharms, Community Jan 3 at 9:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    0
















    This question already has an answer here:




    • Not able to achieve Gapless audio looping so far on Android

      6 answers




    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)










    share|improve this question













    marked as duplicate by dharms, Community Jan 3 at 9:38


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0









      This question already has an answer here:




      • Not able to achieve Gapless audio looping so far on Android

        6 answers




      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)










      share|improve this question















      This question already has an answer here:




      • Not able to achieve Gapless audio looping so far on Android

        6 answers




      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)





      This question already has an answer here:




      • Not able to achieve Gapless audio looping so far on Android

        6 answers








      java android






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 0:09









      alexey polusovalexey polusov

      1,36711328




      1,36711328




      marked as duplicate by dharms, Community Jan 3 at 9:38


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by dharms, Community Jan 3 at 9:38


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          0














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






          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














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






            share|improve this answer




























              0














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






              share|improve this answer


























                0












                0








                0







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






                share|improve this answer













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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 2:14









                HongyangcnHongyangcn

                1




                1

















                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith