Android Database Best Practices (Android Deep Dive) Free Download

Deep Dive: MediaPlayer Best Practices

Photo past Marcela Laskoski on Unsplash
          MediaPlayer.create(context, R.raw.cowbell).start()        

The Simple Case

                      private val            mediaPlayer = MediaPlayer().apply            {
setOnPreparedListener { start() }
setOnCompletionListener { reset() }
}
  • OnPreparedListener, which will automatically showtime playback after the actor has been prepared.
  • OnCompletionListener which automatically cleans upwardly resources when playback has finished.
                      override fun            playSound(@RawRes rawResId: Int) {
val assetFileDescriptor = context.resource.openRawResourceFd(rawResId) ?: return
mediaPlayer.run {
reset()
setDataSource(assetFileDescriptor.fileDescriptor, assetFileDescriptor.startOffset, assetFileDescriptor.declaredLength)
prepareAsync()
}
}
  • The resource ID must be converted to an AssetFileDescriptor considering this is what MediaPlayer uses to play raw resources. The zip check ensures the resource exists.
  • Calling reset() ensures the player is in the Initialized state. This works no thing what state the actor is in.
  • Prepare the data source for the player.
  • prepareAsync prepares the thespian to play and returns immediately, keeping the UI responsive. This works because fastened OnPreparedListener starts playing after the source has been prepared.
          playSound(R.raw.cowbell)        

More Cowbells

          playSound(R.raw.big_cowbell)
playSound(R.raw.small_cowbell)
                      course            MediaPlayers(context: Context) {
private val context: Context = context.applicationContext
individual val playersInUse = mutableListOf<MediaPlayer>()

private fun buildPlayer() = MediaPlayer().apply {
setOnPreparedListener { outset() }
setOnCompletionListener {
it.release()
playersInUse -= it
}
}

override fun

playSound(@RawRes rawResId: Int) {
val assetFileDescriptor = context.resources.openRawResourceFd(rawResId) ?: return
val
mediaPlayer = buildPlayer()

mediaPlayer.run {
playersInUse += it
setDataSource(assetFileDescriptor.fileDescriptor, assetFileDescriptor.startOffset,
assetFileDescriptor.declaredLength)
prepareAsync()
}
}
}

Enter MediaPlayerPool

                      course            MediaPlayerPool(context: Context, maxStreams: Int) {
private val context: Context = context.applicationContext private val mediaPlayerPool = mutableListOf<MediaPlayer>().also {
for
(i in 0..maxStreams) information technology += buildPlayer()
}
private val playersInUse
= mutableListOf<MediaPlayer>()

private fun buildPlayer() = MediaPlayer().apply {
setOnPreparedListener { start() }
setOnCompletionListener { recyclePlayer(it) }
}
/**
* Returns a
[MediaPlayer] if one is available,
* otherwise zilch.
*/
private fun requestPlayer(): MediaPlayer? {
render if (!mediaPlayerPool.isEmpty()) {
mediaPlayerPool.removeAt(0).also {
playersInUse
+= it
}
} else cipher
}

private fun recyclePlayer(mediaPlayer: MediaPlayer) {
mediaPlayer.reset()
playersInUse -= mediaPlayer
mediaPlayerPool += mediaPlayer
}

fun playSound(@RawRes rawResId: Int) {
val assetFileDescriptor = context.resources.openRawResourceFd(rawResId) ?: return
val
mediaPlayer = requestPlayer() ?: returnmediaPlayer.run {
setDataSource(assetFileDescriptor.fileDescriptor, assetFileDescriptor.startOffset,
assetFileDescriptor.declaredLength)
prepareAsync()
}
}
}

  • After maxStreams sounds are playing, whatsoever additional calls to playSound are ignored until a player is freed upwards. Y'all could work around this past "stealing" a player that's already in employ to play a new audio.
  • In that location tin be significant lag between calling playSound and actually playing the sound. Even though the MediaPlayer is being reused, information technology's actually a thin wrapper that controls an underlying C++ native object via JNI. The native player is destroyed each fourth dimension you call MediaPlayer.reset(), and information technology must be recreated whenever the MediaPlayer is prepared.

DOWNLOAD HERE

Posted by: montaltoequitiardead.blogspot.com

Post a Comment

Previous Post Next Post

Iklan Banner setelah judul