Hello everyone.
I just started developing with Kotlin and I’m using mediaplayers and it’s only working when I launch the item first time. From the second onwards I get the error “E/MediaPlayerNative: error (-38, 0)”.
Here is the code:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.soundpage)
val guarigione = MediaPlayer()
val legno = MediaPlayer()
val urlGuarigione = "https://www.istitutoartusi.it/colazioni/temp/guarigione.mp3"
val urlLegno = "https://www.istitutoartusi.it/colazioni/temp/guarigionelegno.mp3"
guarigione.setAudioAttributes(
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
)
guarigione.setDataSource(urlGuarigione)
guarigione.prepare()
legno.setAudioAttributes(
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
)
legno.setDataSource(urlLegno)
legno.prepare()
btnPlay1 = findViewById(R.id.btnPlay1)
btnPlay1.setOnClickListener{
stopAudio(legno, btnPlay2)
playAudio(guarigione, btnPlay1)
}
btnPlay2 = findViewById(R.id.btnPlay2)
btnPlay2.setOnClickListener{
stopAudio(guarigione, btnPlay1)
playAudio(legno, btnPlay2)
}
}
private fun playAudio(musica: MediaPlayer, bottone: ImageButton) {
musica.setLooping(true)
if (musica.isPlaying())
{
bottone.setImageResource(R.drawable.play)
musica.pause()
} else {
bottone.setImageResource(R.drawable.pause)
musica.start()
}
}
private fun stopAudio(musica: MediaPlayer, bottone: ImageButton) {
if (musica.isPlaying())
{
bottone.setImageResource(R.drawable.play)
musica.stop()
musica.reset()
}
}
I start the first song and it works.
I start the second song and the application stops the first one and starts the second regularly.
When I try to start the first song again it doesn’t start and I get the error E/MediaPlayerNative: error (-38, 0).
Can anyone help me fix this error please?