我目前使用AudioRecord類從設備麥克風中讀取錄音,然後使用AudioTrack類將其播放出去。Android - 通過聽筒播放音頻
我的問題是,當我播放它通過揚聲器電話播放。
我希望它通過設備上的耳塞播放。
這裏是我的代碼:
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);
Log.d("SPEAKERPHONE", "Is speakerphone on? : " + am.isSpeakerphoneOn());
am.setSpeakerphoneOn(false);
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;
}
}
}
正如你可以看到,如果代碼中,我使用AudioManager類及其方法,包括棄用setRouting方法並沒有什麼作品試過了,setSpeakerphoneOn方法似乎沒有任何效果根本沒有路由方法。
有沒有人有任何想法如何讓它通過耳機而不是spaker手機玩?
在設置爲IN_CALL之前,您是否需要將音頻設置回初始狀態?像這樣:int currAudioMode = audioManager.getMode(); \t \t \t \t \t \t audioManager.setMode(AudioManager.MODE_IN_CALL); \t \t \t audioManager.setSpeakerphoneOn(actionData.speakerOn); \t \t \t \t \t \t audioManager.setMode(currAudioMode); – Muzikant 2011-06-16 09:04:28
我將其重新設置爲MODE_NORMAL以重新打開揚聲器電話。因此,上面的代碼關閉揚聲器並路由到聽筒,下面的代碼路徑可以回到揚聲器的默認設置。 'm_amAudioManager.setMode(AudioManager.MODE_NORMAL);' – Piwaf 2011-08-10 15:39:18
它干擾手機的全局設置嗎? – 2012-10-31 09:17:20