2

对android来说有点新。我有一个应用程序启动并运行,我正在生成 DTMF 信号并解释使用微控制器生成的内容。我成功地生成了音调,但是音调正在通过扬声器播放,我想通过拼接的耳机插孔播放音调(所以即使在解决方案之后,我也需要在它认为没有时覆盖 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");
    }
}

}
4

1 回答 1

3

尝试STREAM.MUSIC代替STREAM.DTMF

static final ToneGenerator _toneGenerator = new ToneGenerator(
                                              AudioManager.STREAM_MUSIC, 100);
于 2012-10-24T16:06:15.887 回答