5

I'm new to android development

I'm working in an android app that simply changes volume and play beep "to preview current sound level"

I use startTone in ToneGenerator class to generate this beep , like this :

 ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_RING, 100);
 tg.startTone(ToneGenerator.TONE_PROP_BEEP);

but sometimes this code doesn't work and generate following exceptions

11-20 00:32:44.262: E/AudioTrack(4701): AudioFlinger could not create track, status: -12
11-20 00:32:44.262: E/ToneGenerator(4701): AudioTrack->initCheck failed

Thanks.

Nat Ritmeyer
  • 5,634
  • 8
  • 45
  • 58
Mahmoud Farahat
  • 5,364
  • 4
  • 43
  • 59

1 Answers1

6

I know it's too late now , but i am posting the answer for anyone will come later.

it was just about releasing created objects of ToneGenerator because rapidly creating objects of 'ToneGenerator' without releasing them will cause the application to crash.

complete code :

    final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
    tg.startTone(ToneGenerator.TONE_PROP_BEEP);
    tg.release();
Mahmoud Farahat
  • 5,364
  • 4
  • 43
  • 59
  • 1
    After adding the `tg.Release();`, I also had to restart the whole emulator (not just the app) in order to stop the app from crashing. – CragMonkey Nov 04 '20 at 05:03
  • 1
    Also you have to re-initialize like this... final ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 400); – Mick Jul 15 '23 at 18:27