gpt4 book ai didi

android - 如何通过我的耳机播放 DTMF 音调?

转载 作者:搜寻专家 更新时间:2023-11-01 09:06:24 30 4
gpt4 key购买 nike

Android 的新手。我有一个应用程序正在运行,我正在生成 DTMF 信号并解释微 Controller 生成的内容。我成功地生成了音调,但是音调是通过扬声器播放的,我想通过一个拼接的耳机插孔播放音调(所以即使在解决方案之后,我也需要覆盖 android 在它认为没有时切换到扬声器的愿望耳机在那里)。提前致谢。代码在这里..

package edu.lehigh.cse.paclab.carbot;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ToggleButton;
import android.media.ToneGenerator;
import android.media.AudioManager;

import static android.media.ToneGenerator.TONE_DTMF_0;
import static android.media.ToneGenerator.TONE_DTMF_1;
import static android.media.ToneGenerator.TONE_DTMF_2;
import static android.media.ToneGenerator.TONE_DTMF_3;
import static android.media.ToneGenerator.TONE_DTMF_4;
import static android.media.ToneGenerator.TONE_DTMF_5;
import static android.media.ToneGenerator.TONE_DTMF_6;

public class DTMFActivity extends Activity {
private static final String TAG = "DTMF";

ToggleButton forward;
ToggleButton reverse;
ToggleButton c_clockwise;
ToggleButton clockwise;
ToggleButton pointTurnLeft;
ToggleButton pointTurnRight;

static final ToneGenerator _toneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF, 100);

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


forward = (ToggleButton)findViewById(R.id.forward);
reverse = (ToggleButton)findViewById(R.id.reverse);
c_clockwise = (ToggleButton)findViewById(R.id.c_clockwise);
clockwise = (ToggleButton)findViewById(R.id.clockwise);
pointTurnLeft = (ToggleButton)findViewById(R.id.pointTurnLeft);
pointTurnRight = (ToggleButton)findViewById(R.id.pointTurnRight);

}

public void onPause() {
super.onPause();
}

public void onDestroy() {
super.onDestroy();
}

public void forward(View v){

if(!forward.isChecked()){
_toneGenerator.stopTone();
Log.e(TAG, "forward: not emitting");
}
else{
_toneGenerator.startTone(TONE_DTMF_0);
Log.e(TAG, "forward: emitting");
}
}

public void reverse(View v){

if(!reverse.isChecked()){
_toneGenerator.stopTone();
Log.e(TAG, "reverse: not emitting");
}
else{
_toneGenerator.startTone(TONE_DTMF_1);
Log.e(TAG, "reverse: emitting");
}
}

public void c_clockwise(View v){

if(!c_clockwise.isChecked()){
_toneGenerator.stopTone();
Log.e(TAG, "c_clockwise: not emitting");
}
else{
_toneGenerator.startTone(TONE_DTMF_2);
Log.e(TAG, "c_clockwise: emitting");
}
}

public void clockwise(View v){

if(!clockwise.isChecked()){
_toneGenerator.stopTone();
Log.e(TAG, "clockwise: not emitting");
}
else{
_toneGenerator.startTone(TONE_DTMF_3);
Log.e(TAG, "clockwise: emitting");
}
}

public void pointTurnLeft(View v){

if(!pointTurnLeft.isChecked()){
_toneGenerator.stopTone();
Log.e(TAG, "pointTurnLeft: not emitting");
}
else{
_toneGenerator.startTone(TONE_DTMF_4);
Log.e(TAG, "pointTurnLeft: emitting");
}
}

public void pointTurnRight(View v){

if(!pointTurnRight.isChecked()){
_toneGenerator.stopTone();
Log.e(TAG, "pointTurnRight: not emitting");
}
else{
_toneGenerator.startTone(TONE_DTMF_5);
Log.e(TAG, "pointTurnRight: emitting");
}
}

}

最佳答案

尝试使用 STREAM.MUSIC 而不是 STREAM.DTMF:

static final ToneGenerator _toneGenerator = new ToneGenerator(
AudioManager.STREAM_MUSIC, 100);

关于android - 如何通过我的耳机播放 DTMF 音调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11589213/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com