31

I would like my app beep with a specific frequency and duration. In the windows equivalent of this app (written in c#) I used a c++ dll with the function

beep(frequency, duration); 

Is this the same in android? Or at least how can I put my c++ dll in the project?

I would prefer not to use pre-built mp3's or system sound because I would like to give the user the choice of the frequency and duration.

John
  • 6,433
  • 7
  • 47
  • 82
Cippo
  • 1,001
  • 4
  • 14
  • 26
  • possible duplicate : http://stackoverflow.com/questions/6462105/how-do-i-access-androids-default-beep-sound – Max Aug 28 '12 at 07:56
  • Have a look at this possible duplicate also: http://stackoverflow.com/q/5279242/1127492 – Stefan Aug 28 '12 at 08:06
  • Thanks for answering (both of you) but actually it isn't really what I'm looking for. – Cippo Aug 28 '12 at 08:17
  • [android.media.ToneGenerator](https://www.codota.com/code/java/classes/android.media.ToneGenerator) would definitely be the best way to go about this, and it's relatively simple to do – Jonathan Jun 10 '18 at 19:01

3 Answers3

92

I tried amine.b's answer. In short, to play a loud Beep sound:

ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); 
John
  • 6,433
  • 7
  • 47
  • 82
Eng. Samer T
  • 6,465
  • 6
  • 36
  • 43
  • I like this one, it will actually give a beep instead of a ring tone :) – Chef Pharaoh Nov 19 '14 at 23:42
  • 2
    Best would be to use a continuous tone like the ones from ToneGenerator.TONE_DTMF_0 to ToneGenerator.TONE_DTMF_S or else the generated beep may sound interrupted. – ungalcrys Feb 10 '15 at 15:20
16

The easy way is to use instance of ToneGenerator class:

// send the tone to the "alarm" stream (classic beeps go there) with 50% volume
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 50);
if (val >= taux_max) {
    taux_text.setTextColor(warnning_col);
    toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); // 200 is duration in ms
}

Please refer to the documentation of ToneGenerator and AudioManager for exact meaning of parameters and possible configuration of the generator.

andr
  • 15,970
  • 10
  • 45
  • 59
amine.b
  • 176
  • 1
  • 7
0

If you want to use your C++ code in the android app which is possible. You need to look at Android NDK which allows you to use execute C++ code with the help of JNI (Java Native Interface).

Android NDK

Dilberted
  • 1,172
  • 10
  • 23
  • Ok... Could you please explain what should I do? Thanks! – Cippo Aug 28 '12 at 08:16
  • First of all you need to install the ndk. at the link i gave above should have the guide to let you on how to configure (which is pretty easy). and here is another link for ndk samples are the best way to start off.. [link](http://developer.android.com/tools/sdk/ndk/index.html#Samples) – Dilberted Aug 28 '12 at 08:30
  • Thank you very much. Actually I can't try it now 'cause I'm programming on my android tablet without eclipse. I'll give a look at it anyway. Again thanks a lot! – Cippo Aug 28 '12 at 08:57