我想按下耳机上的按钮,通过免提电话播放声音,然后录制一个 10 秒的音频片段。这是我的代码:
SoundPool soundPool;
HashMap<Integer, Integer> soundPoolMap;
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(SOUND_RECORD_ON,
soundPool.load(context, R.raw.on, 1));
soundPoolMap.put(SOUND_RECORD_OFF,
soundPool.load(context, R.raw.off, 1));
AudioManager mAudioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeMax = mAudioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
if (recordStarted == false) {
try { // start recording
audioRecorder.start();
recordStarted = true;
System.out.println(String.format("recording: %s",
recordStarted));
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.setSpeakerphoneOn(true);
System.out.println("speaker on");
soundPool.play(soundPoolMap.get(SOUND_RECORD_ON),
streamVolumeMax, streamVolumeMax, 1, 0, 1f);
} catch (IOException e) {
e.printStackTrace();
}
} else { // stop recording
try {
audioRecorder.stop();
recordStarted = false;
System.out.println(String.format("recording: %s",
recordStarted));
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.setSpeakerphoneOn(true);
System.out.println("speaker on");
soundPool.play(soundPoolMap.get(SOUND_RECORD_ON),
streamVolumeMax, streamVolumeMax, 2, 0, 1f);
} catch (IOException e) {
e.printStackTrace();
}
第一个声音在扬声器上播放良好,完整性非常好。但是,当我再次按下按钮停止录制时,尽管使用了相同的剪辑,但声音几乎听不见。这是 logcat 跟踪:
ERROR/MediaButtonIntentReceiver(5290): <!> MediaButtonIntentReceiver 52<!> BUTTON PRESSED :D
WARN/StagefrightRecorder(1466): Target duration (10000000 us) too short to be respected
ERROR/AudioHardwareMSM72XX(1466): [LGE] output devices :2
ERROR/AudioHardwareMSM72XX(1466): [doRouting]new_snd_device=2,mCurSndDevice=-1
ERROR/AudioHardwareMSM72XX(1466): [SKW]do_route_audio_rpc(2, 1, 0)
ERROR/AudioHardwareMSM72XX(1466): [LGE] output devices :2
ERROR/AudioHardwareMSM72XX(1466): [doRouting]new_snd_device=2,mCurSndDevice=2
ERROR/AudioHardwareMSM72XX(1466): [LGE] output devices :2
ERROR/AudioHardwareMSM72XX(1466): [doRouting]new_snd_device=2,mCurSndDevice=2
ERROR/AudioHardwareMSM72XX(1466): [LGE] output devices :2
ERROR/AudioHardwareMSM72XX(1466): [doRouting]new_snd_device=2,mCurSndDevice=-1
ERROR/AudioHardwareMSM72XX(1466): [SKW]do_route_audio_rpc(2, 1, 0)
INFO/System.out(5290): recording: true
ERROR/AudioHardwareMSM72XX(1466): [LGE] output devices :2
ERROR/AudioHardwareMSM72XX(1466): Routing audio to Speakerphone_conversation
ERROR/AudioHardwareMSM72XX(1466): [doRouting]new_snd_device=7,mCurSndDevice=-1
ERROR/AudioHardwareMSM72XX(1466): [SKW]do_route_audio_rpc(7, 0, 0)
ERROR/AudioService(1586): <!>android.media.AudioService 895<!> [BTUI] [SCO] ### setMode (2)
ERROR/AudioHardwareMSM72XX(1466): [LGE] output devices :2
ERROR/AudioHardwareMSM72XX(1466): Routing audio to Speakerphone_conversation
ERROR/AudioHardwareMSM72XX(1466): [doRouting]new_snd_device=7,mCurSndDevice=7
INFO/System.out(5290): speaker on
WARN/AudioFlinger(1466): write blocked for 138 msecs, 71 delayed writes, thread 0xce08
ERROR/SUMAN-statusbarpolicy(1665): <!>com.android.systemui.statusbar.policy.StatusBarPolicy 1069<!> mServiceState.getRoaming()false
DEBUG/StatusBarPolicy(1665): [BRIGHTHY] 0. mDataNetType: 8
DEBUG/StatusBarPolicy(1665): [BRIGHTHY] curNetwork=302880 curHPLMN=302220
WARN/KeyCharacterMap(5290): No keyboard for id 0
WARN/KeyCharacterMap(5290): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
ERROR/MediaButtonIntentReceiver(5290): <!> MediaButtonIntentReceiver 52<!> BUTTON PRESSED :D
ERROR/AudioHardwareMSM72XX(1466): [LGE] output devices :2
ERROR/AudioHardwareMSM72XX(1466): [doRouting]new_snd_device=2,mCurSndDevice=-1
ERROR/AudioHardwareMSM72XX(1466): [SKW]do_route_audio_rpc(2, 0, 0)
ERROR/AudioHardwareMSM72XX(1466): [LGE] output devices :2
ERROR/AudioHardwareMSM72XX(1466): Routing audio to Speakerphone_conversation
ERROR/AudioHardwareMSM72XX(1466): [doRouting]new_snd_device=7,mCurSndDevice=2
ERROR/AudioHardwareMSM72XX(1466): [SKW]do_route_audio_rpc(7, 0, 0)
ERROR/AudioHardwareMSM72XX(1466): [LGE] output devices :2
ERROR/AudioHardwareMSM72XX(1466): Routing audio to Speakerphone_conversation
ERROR/AudioHardwareMSM72XX(1466): [doRouting]new_snd_device=7,mCurSndDevice=-1
ERROR/AudioHardwareMSM72XX(1466): [SKW]do_route_audio_rpc(7, 0, 0)
INFO/System.out(5290): recording: false
ERROR/AudioHardwareMSM72XX(1466): [LGE] output devices :2
ERROR/AudioHardwareMSM72XX(1466): Routing audio to Speakerphone_conversation
ERROR/AudioHardwareMSM72XX(1466): [doRouting]new_snd_device=7,mCurSndDevice=7
INFO/System.out(5290): speaker on
有谁知道为什么会这样?我知道在第一部分中我没有将免提电话设置回 false;如果我这样做,声音只会通过耳机播放。我怀疑这是说“太短而无法尊重”的台词。非常感谢!
--------- 使用媒体播放器代码更新 -------------
if (action == KeyEvent.ACTION_DOWN) {
Log.e(TAG, "BUTTON PRESSED :D ");
AudioManager mAudioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
MediaPlayer playOn = MediaPlayer.create(context, R.raw.on);
if (recordStarted == false) {
try { // start recording
audioRecorder.start();
recordStarted = true;
System.out.println(String.format("recording: %s",
recordStarted));
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.setSpeakerphoneOn(true);
System.out.println("speaker on");
playOn.start();
System.out.println("playing on tune");
} catch (IOException e) {
e.printStackTrace();
}
} else { // stop recording
try {
audioRecorder.stop();
recordStarted = false;
System.out.println(String.format("recording: %s",
recordStarted));
playOn.start();
System.out.println("playing off tune");
} catch (IOException e) {
e.printStackTrace();
} finally {
// mAudioManager.setSpeakerphoneOn(false);
// System.out.println("speaker off");
// mAudioManager.setMode(AudioManager.MODE_NORMAL);
}
} // end else
} // end key down action
好的,使用此代码播放声音,但第二个声音仍然很安静(与以前相同的问题)...更多建议?:D