Playing an arbitrary tone with Android

后端 未结 10 2205
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 10:38

Is there any way to make Android emit a sound of arbitrary frequency (meaning, I don\'t want to have pre-recorded sound files)?

I\'ve looked around and ToneGenerator

10条回答
  •  [愿得一人]
    2020-11-22 11:05

    I wrapped the above wonderful solutions into a neat little package that's more useable out of the box as a simple configurable buzzer. It runs it in a background thread and has stop and play methods and a handful of options you can set.

    It's up on JCenter so you can add it to your dependencies list like this

    compile 'net.mabboud:android-tone-player:0.2'
    

    and you use it like this for a continuous buzzer

    ContinuousBuzzer tonePlayer = new ContinuousBuzzer();
    tonePlayer.play();
    
    // just an example don't actually use Thread.sleep in your app
    Thread.sleep(1000); 
    tonePlayer.stop();
    

    or a buzzer played only once and you can set frequency and volume like this

    OneTimeBuzzer buzzer = new OneTimeBuzzer();
    buzzer.setDuration(5);
    
    // volume values are from 0-100
    buzzer.setVolume(50);
    buzzer.setToneFreqInHz(110);
    

    Extended blog post here about it here GitHub here

提交回复
热议问题