Android-通过听筒播放音频

我目前有一些代码,该代码使用AudioRecord类从设备麦克风中读取录音,然后使用AudioTrack类将其回放。


我的问题是,当我播放时,它是通过扬声器播放的。


我希望它通过设备上的听筒播放。


这是我的代码:


public class LoopProg extends Activity {


 boolean isRecording; //currently not used

 AudioManager am;

 int count = 0;


 /** 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);

        am.setMicrophoneMute(true);

        while(count <= 1000000){

        Record record = new Record();  

        record.run();

        count ++;

        Log.d("COUNT", "Count is : " + count);

        }

    } 


   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_MUSIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize, AudioTrack.MODE_STREAM);

      am.setRouting(AudioManager.MODE_NORMAL,1, AudioManager.STREAM_MUSIC);

      int ok = am.getRouting(AudioManager.ROUTE_EARPIECE);

      Log.d("ROUTING", "getRouting = " + ok);

      setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);

      //am.setSpeakerphoneOn(true);


      }

   } 

}

如您所见,如果我尝试使用AudioManager类及其方法(包括不赞成使用的setRouting方法)尝试的代码没有任何效果,则setSpeakerphoneOn方法似乎完全无效,路由方法也无效。


有没有人知道如何通过听筒而不是Spaker电话播放它?


12345678_0001
浏览 766回答 3
3回答

白衣染霜花

请使用此代码,效果很好:&nbsp; &nbsp; //PLAY ON EARPIECE&nbsp; &nbsp; mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);&nbsp; &nbsp; audioManager.setMode(AudioManager.MODE_IN_CALL);&nbsp; &nbsp; audioManager.setSpeakerphoneOn(false);&nbsp; &nbsp; //PLAY ON SPEAKER&nbsp; &nbsp; mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);&nbsp; &nbsp; audioManager.setMode(AudioManager.MODE_IN_CALL);&nbsp; &nbsp; audioManager.setSpeakerphoneOn(true);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android