Created
October 28, 2021 16:01
-
-
Save CreatorB/08da5488ebcd8d1d916c7b9f0d4926a3 to your computer and use it in GitHub Desktop.
Android Simple Audio Player
This file contains hidden or 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.blogspot.situbondoprogrammer.voicepress | |
import android.content.Context | |
import android.media.MediaPlayer | |
class SimpleAudioPlayer { | |
private var mMediaPlayer: MediaPlayer = MediaPlayer() | |
private fun stopAudio() { | |
try { | |
mMediaPlayer.release() | |
}catch (ex: Exception){ | |
ex.printStackTrace() | |
} | |
} | |
fun playAudio(mContext: Context, fileName: String) { | |
try { | |
stopAudio() | |
mMediaPlayer = MediaPlayer.create(mContext, mContext.resources.getIdentifier(fileName, "raw", mContext.packageName)) | |
mMediaPlayer.setOnCompletionListener { stopAudio() } | |
mMediaPlayer.start() | |
}catch (ex: Exception){ | |
ex.printStackTrace() | |
} | |
} | |
} | |
// HOW TO USE ? | |
// 1. make sure you already the sound in your raw folder at app/src/main/res/raw/sound1.mp3 | |
// 2. "call it on another class like this below : " | |
// AudioPlayer().playAudio(c, "sound1") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment