Created
March 5, 2019 11:19
-
-
Save EliteIntegrity/a8333bbf538b007c039b99cf6d5a53e4 to your computer and use it in GitHub Desktop.
This class enables us to easily play a sound from the game loop just by calling the playSound function and passing in the appropriate id for the sound we want to play.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gamecodeschool.kotlininvaders | |
import android.content.Context | |
import android.content.res.AssetFileDescriptor | |
import android.media.AudioManager | |
import android.media.SoundPool | |
import android.util.Log | |
import java.io.IOException | |
class SoundPlayer(context: Context) { | |
// For sound FX | |
private val soundPool: SoundPool = SoundPool(10, | |
AudioManager.STREAM_MUSIC, | |
0) | |
companion object { | |
var playerExplodeID = -1 | |
var invaderExplodeID = -1 | |
var shootID = -1 | |
var damageShelterID = -1 | |
var uhID = -1 | |
var ohID = -1 | |
} | |
init { | |
try { | |
// Create objects of the 2 required classes | |
val assetManager = context.assets | |
var descriptor: AssetFileDescriptor | |
// Load our fx in memory ready for use | |
descriptor = assetManager.openFd("shoot.ogg") | |
shootID = soundPool.load(descriptor, 0) | |
descriptor = assetManager.openFd("invaderexplode.ogg") | |
invaderExplodeID = soundPool.load(descriptor, 0) | |
descriptor = assetManager.openFd("damageshelter.ogg") | |
damageShelterID = soundPool.load(descriptor, 0) | |
descriptor = assetManager.openFd("playerexplode.ogg") | |
playerExplodeID = soundPool.load(descriptor, 0) | |
descriptor = assetManager.openFd("damageshelter.ogg") | |
damageShelterID = soundPool.load(descriptor, 0) | |
descriptor = assetManager.openFd("uh.ogg") | |
uhID = soundPool.load(descriptor, 0) | |
descriptor = assetManager.openFd("oh.ogg") | |
ohID = soundPool.load(descriptor, 0) | |
} catch (e: IOException) { | |
// Print an error message to the console | |
Log.e("error", "failed to load sound files") | |
} | |
} | |
fun playSound(id: Int){ | |
soundPool.play(id, 1f, 1f, 0, 0, 1f) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment