Reputation: 165
I've searched many topics related to dtmf but didn't find what i want. I m simply creating an app which have four buttons that may generate the dtmf tones of the keys 2,4,6,8. I want to send these dtmf tones to a microcontroller and it will then do some work. I have got a code but it didnt work. I m getting ExceptionInInitializer Error.
Can someone tell me will this code work?
Or is there any other way to generate the dtmf tones of these numbers Plz help me as this is a part of my college project.
Thank you in advance.
public class MainActivity extends ActionBarActivity {
public Button up, dwn, left, right, about;
static final ToneGenerator _toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
Context context= this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
up = (Button) findViewById(R.id.button1);
left = (Button) findViewById(R.id.button2);
right = (Button) findViewById(R.id.button3);
dwn = (Button) findViewById(R.id.button4);
about = (Button) findViewById(R.id.button5);
up.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
_toneGenerator.startTone(TONE_DTMF_2);
_toneGenerator.stopTone();
}
});
left.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
_toneGenerator.startTone(TONE_DTMF_4);
_toneGenerator.stopTone();
}
});
right.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
_toneGenerator.startTone(TONE_DTMF_6);
_toneGenerator.stopTone();
}
});
dwn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
_toneGenerator.startTone(TONE_DTMF_8);
_toneGenerator.stopTone();
}
});
about.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, About.class);
startActivity(intent);
}
});
}
}
Logcat
07-11 13:01:29.240: I/Process(1162): Sending signal. PID: 1162 SIG: 9
07-11 13:01:48.080: E/ToneGenerator(1213): Unable to marshal AudioFlinger
07-11 13:01:48.080: E/ToneGenerator(1213): ToneGenerator init failed
07-11 13:01:48.090: W/dalvikvm(1213): Exception Ljava/lang/RuntimeException; thrown while initializing Lcom/project/dtmf/MainActivity;
07-11 13:01:48.090: W/dalvikvm(1213): Class init failed in newInstance call (Lcom/project/dtmf/MainActivity;)
07-11 13:01:48.090: D/AndroidRuntime(1213): Shutting down VM
07-11 13:01:48.100: W/dalvikvm(1213): threadid=1: thread exiting with uncaught exception (group=0xb3a5eba8)
07-11 13:01:48.110: E/AndroidRuntime(1213): FATAL EXCEPTION: main
07-11 13:01:48.110: E/AndroidRuntime(1213): Process: com.project.dtmf, PID: 1213
07-11 13:01:48.110: E/AndroidRuntime(1213): java.lang.ExceptionInInitializerError
07-11 13:01:48.110: E/AndroidRuntime(1213): at java.lang.Class.newInstanceImpl(Native Method)
07-11 13:01:48.110: E/AndroidRuntime(1213): at java.lang.Class.newInstance(Class.java:1208)
07-11 13:01:48.110: E/AndroidRuntime(1213): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-11 13:01:48.110: E/AndroidRuntime(1213): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-11 13:01:48.110: E/AndroidRuntime(1213): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-11 13:01:48.110: E/AndroidRuntime(1213): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-11 13:01:48.110: E/AndroidRuntime(1213): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-11 13:01:48.110: E/AndroidRuntime(1213): at android.os.Handler.dispatchMessage(Handler.java:102)
07-11 13:01:48.110: E/AndroidRuntime(1213): at android.os.Looper.loop(Looper.java:136)
07-11 13:01:48.110: E/AndroidRuntime(1213): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-11 13:01:48.110: E/AndroidRuntime(1213): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 13:01:48.110: E/AndroidRuntime(1213): at java.lang.reflect.Method.invoke(Method.java:515)
07-11 13:01:48.110: E/AndroidRuntime(1213): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-11 13:01:48.110: E/AndroidRuntime(1213): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-11 13:01:48.110: E/AndroidRuntime(1213): at dalvik.system.NativeStart.main(Native Method)
07-11 13:01:48.110: E/AndroidRuntime(1213): Caused by: java.lang.RuntimeException: Init failed
07-11 13:01:48.110: E/AndroidRuntime(1213): at android.media.ToneGenerator.native_setup(Native Method)
07-11 13:01:48.110: E/AndroidRuntime(1213): at android.media.ToneGenerator.<init>(ToneGenerator.java:740)
07-11 13:01:48.110: E/AndroidRuntime(1213): at com.project.dtmf.MainActivity.<clinit>(MainActivity.java:20)
07-11 13:01:48.110: E/AndroidRuntime(1213): ... 15 more
Upvotes: 0
Views: 3270
Reputation: 11
try to initialize instance of ToneGenerator
inside onCreate
static ToneGenerator _toneGenerator;
Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
...
to play sound use:
_toneGenerator.startTone(ToneGenerator.TONE_DTMF_0,duration_in_ms); //it's playing async
Upvotes: 1
Reputation: 2903
int durationMs = 500;
int volume_level = 100;
final ToneGenerator mToneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, volume_level); // Raising volume to 100% (For eg. 7 * 14 ~ 100)
mToneGenerator.stopTone();
mToneGenerator.startTone(ToneGenerator.TONE_DTMF_0, durationMs); // playsound for 500ms
Upvotes: 0
Reputation: 397
You probably shouldn't use stopTone()
immediately after startTone()
.
These two lines are being executed at the same time. So you won't get any sound.
You should stop the tone after startTone()
finishes. I haven't worked with ToneGenerator
much, but maybe you should specify a time parameter for your tone too. For example you want TONE_DTMF_2
to run for how long?
Upvotes: 1