In order to receive the only short beep sound on loudspeaker I want to send single bit to the loudspeaker directly. Similarly to the LED blink. Is there any possibility to do short beep without any kind of Media Players?
4 Answers
I recommend you use the ToneGenerator
class. It requires no audio files, no media player, and you can customize the beep's volume, duration (in milliseconds) and Tone type. I like this one:
ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP,150);
You can see into the ToneGenerator object (CMD + click over ToneGenerator. , in Mac), and choose another beep type besides TONE_CDMA_PIP
, 150
is the duration in milliseconds, and 100
the volume.
-
-
5@Mauker you can use 'ToneGenerator.TONE_PROP_BEEP2' to achieve a double beep. May 17, 2018 at 12:37
-
1I ended up trying just that, and it works. Thanks for the comment Manoj!– MaukerMay 17, 2018 at 12:39
-
-
Heads up - You will need to adjust the
duration
argument (something like 300ms is good) to playToneGenerator.TONE_PROP_BEEP2
or any longer beep– SakiboyFeb 25, 2021 at 2:05
just adding josh's answer. you need to release ToneGenerator
using Handler
. especially if you got error java.lang.RuntimeException: Init failed at android.media.ToneGenerator.native_setup(Native Method)
like i did.
the complete code :
import android.media.AudioManager
import android.media.ToneGenerator
import android.os.Handler
import android.os.Looper
class BeepHelper
{
val toneG = ToneGenerator(AudioManager.STREAM_ALARM, 100)
fun beep(duration: Int)
{
toneG.startTone(ToneGenerator.TONE_DTMF_S, duration)
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({
toneG.release()
}, (duration + 50).toLong())
}
}
-
12Bless you for including the import statements! If I'm trying to figure out how to do something, chances are I don't know where to find it, either. Thank you! :) Jul 24, 2018 at 23:10
For Kotlin:
ToneGenerator(AudioManager.STREAM_MUSIC, 100).startTone(ToneGenerator.TONE_PROP_BEEP, 200)
In my case, the Tone Generator was not loud enough for some reason.
I ended up using the Media Player and created a Beeper class allowing to play a beep sound with a specified volume.
import android.content.Context
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.MediaPlayer
import android.net.Uri
import com.nfci.mesea.time.R
import kotlin.math.ln
class Beeper(val context: Context) {
private val maxVolume = 100f
private val mediaPlayer = MediaPlayer()
init {
val beep = Uri.parse("android.resource://" + context.packageName + "/" + R.raw.beep)
val audioAttributes = AudioAttributes.Builder()
.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
.setLegacyStreamType(AudioManager.STREAM_ALARM)
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
mediaPlayer.setDataSource(context, beep)
mediaPlayer.setAudioAttributes(audioAttributes)
mediaPlayer.prepare()
}
fun play(volume: Int){
if (!mediaPlayer.isPlaying) {
val calculatedVolume = 1 - (ln(maxVolume - volume.toFloat()) / ln(maxVolume))
mediaPlayer.setVolume(calculatedVolume, calculatedVolume)
mediaPlayer.start()
}
}
}
-
It is better to create a media player during initialization, then you will not need to use URI.parse(), not need to call setDataSource() and prepare(), but only set attributes:
mediaPlayer = MediaPlayer.create(context, R.raw.beep).apply{ setAudioAttributes(/*audioAttributes*/) }
Oct 30, 2021 at 13:26