如何在android中发出哔哔声?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12154940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a beep in android?
提问by Cippo
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
我希望我的应用以特定频率和持续时间发出哔哔声。在这个应用程序的 Windows 等价物(用 c# 编写)中,我使用了一个带有函数的 c++ dll
beep(frequency, duration);
Is this the same in android? Or at least how can I put my c++ dll in the project?
这在android中是一样的吗?或者至少如何将我的 c++ dll 放入项目中?
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.
我不想使用预先构建的 mp3 或系统声音,因为我想让用户选择频率和持续时间。
采纳答案by Dilberted
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 应用程序中使用您的 C++ 代码。您需要查看 Android NDK,它允许您在 JNI(Java 本机接口)的帮助下使用执行 C++ 代码。
回答by Eng. Samer T
I tried amine.b's answer. In short, to play a loud Beep sound:
我尝试了amine.b的答案。简而言之,要播放响亮的 Beep 声音:
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);
回答by amine.b
The easy way is to use instance of ToneGenerator
class:
简单的方法是使用ToneGenerator
类的实例:
// 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.
请参阅的文件ToneGenerator
及AudioManager
有关参数的确切含义和发电机的可能的配置。