MediaPlayer in Android works with a delay [duplicate]
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)
java

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.
add a comment |
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)
java

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.
add a comment |
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)
java

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

java

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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
you audio is very short,Maybe you could be to try "SoundPool".
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
you audio is very short,Maybe you could be to try "SoundPool".
add a comment |
you audio is very short,Maybe you could be to try "SoundPool".
add a comment |
you audio is very short,Maybe you could be to try "SoundPool".
you audio is very short,Maybe you could be to try "SoundPool".
answered Jan 3 at 2:14
HongyangcnHongyangcn
1
1
add a comment |
add a comment |