I'm trying to play a beep at a specified frequency for a specified length of time. How is this achieved in android? I noticed that their is an AudioTrack api but they don't provide any easy examples on its usage.
Asked
Active
Viewed 150 times
1 Answers
0
Put the audio in the raw folder
SoundPool soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
int soundId = soundPool.load(context, R.raw.beep, 1);
// soundId for reuse later on
soundPool.play(soundId, 1, 1, 0, 0, 1);
//(soundId , LEFT_VOLUME_VALUE , RIGHT_VOLUME_VALUE, SOUND_PLAY_PRIORITY , MUSIC_LOOP ,PLAY_RATE)
Use the PLAY_RATE attribute to change the frequency of the music being played
and use the MUSIC_LOOP to determine how many times it plays

Narendra_Nath
- 4,578
- 3
- 13
- 31
-
I'm wanting to create the beep programmatically as it is dynamic and frequency and duration vary. Does android not offer that feature? – user1406186 Jul 11 '21 at 12:06
-
This requires an audio file though doesn't it? I'm trying to generate the sound programmatically. – user1406186 Jul 11 '21 at 23:11
-
https://stackoverflow.com/q/2618182/13533028 This should help you out make sure you're using an actual device and not an emulator – Narendra_Nath Jul 12 '21 at 03:47
-
Unfortuantley that doesn't let me specify the frequency in hz – user1406186 Jul 12 '21 at 05:29