如何在android中生成DTMF Tones?

时间:2014-07-11 08:35:39

标签: android

我搜索了很多与dtmf相关的主题,但没有找到我想要的东西。我只是创建一个应用程序,它有四个按钮,可以生成键2,4,6,8的dtmf音调。我想将这些dtmf音调发送到微控制器,然后它会做一些工作。 我有一个代码,但它没有工作。我得到了ExceptionInInitializer错误。

有人可以告诉我这段代码会有效吗?

或者是否有其他方法可以生成这些数字的dtmf音调 Plz帮助我,因为这是我大学项目的一部分。

提前谢谢。

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

2 个答案:

答案 0 :(得分:1)

您可能不应在stopTone()之后立即使用startTone()。 这两行正在同时执行。所以你不会发出声音。

你应该在startTone()完成后停止音调。我没有使用ToneGenerator,但也许你应该为你的音调指定一个时间参数。例如,您希望TONE_DTMF_2运行多长时间?

答案 1 :(得分:0)

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