3

我只是想制作一个可以用作扬声器的应用程序。进入麦克风(耳机)的任何东西都必须通过外部扬声器播放。我尝试了一些,我遵循了以下链接程序,但我无法做到。有没有其他方法可以解决这个问题。请帮我。提前致谢。

Android - 让音频通过听筒播放

如何直接在android上从麦克风播放声音到扬声器?

boolean isRecording; //currently not used
AudioManager am;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    Record record = new Record();
    record.run();

}

public class Record extends Thread
{

    static final int bufferSize = 200000;
    final short[] buffer = new short[bufferSize];
    short[] readBuffer = new short[bufferSize];

    public void run() {
        isRecording = true;
        android.os.Process.setThreadPriority
        (android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

        int buffersize = AudioRecord.getMinBufferSize(11025,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT);

        AudioRecord arec = new AudioRecord
        (MediaRecorder.AudioSource.MIC,
                11025,

                AudioFormat.CHANNEL_CONFIGURATION_MONO,

                AudioFormat.ENCODING_PCM_16BIT,
                buffersize);

        AudioTrack atrack = new AudioTrack
        (AudioManager.STREAM_VOICE_CALL,
                11025,

                AudioFormat.CHANNEL_CONFIGURATION_MONO,

                AudioFormat.ENCODING_PCM_16BIT,
                buffersize,

                AudioTrack.MODE_STREAM);

        am.setRouting(AudioManager.MODE_NORMAL,
                AudioManager.ROUTE_EARPIECE,

                AudioManager.ROUTE_ALL);

        Log.d("SPEAKERPHONE", "Is speakerphone on? : " + am.isSpeakerphoneOn());

        atrack.setPlaybackRate(11025);

        byte[] buffer = new byte[buffersize];
        arec.startRecording();
        atrack.play();

        while(isRecording) {
            arec.read(buffer, 0, 
                    buffersize);
            atrack.write(buffer, 0,
                    buffer.length);
        }

        arec.stop();
        atrack.stop();
        isRecording = false;
    }
}
4

0 回答 0