53

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?

3

4 Answers 4

146

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
  • How do we achieve a double beep with it?
    – Mauker
    Nov 3, 2017 at 13:10
  • 5
    @Mauker you can use 'ToneGenerator.TONE_PROP_BEEP2' to achieve a double beep. May 17, 2018 at 12:37
  • 1
    I ended up trying just that, and it works. Thanks for the comment Manoj!
    – Mauker
    May 17, 2018 at 12:39
  • @ManojPedvi HOW TO PLAY LONG BEEP ?
    – Sagar
    Aug 6, 2020 at 12:07
  • Heads up - You will need to adjust the duration argument (something like 300ms is good) to play ToneGenerator.TONE_PROP_BEEP2 or any longer beep
    – Sakiboy
    Feb 25, 2021 at 2:05
26

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())
}
}
1
  • 12
    Bless 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! :)
    – Karen Cate
    Jul 24, 2018 at 23:10
11

For Kotlin:

ToneGenerator(AudioManager.STREAM_MUSIC, 100).startTone(ToneGenerator.TONE_PROP_BEEP, 200)
4

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()
        }
    }
}
1
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.