我想按下耳机上的按钮,通过扬声器电话播放声音,然后录制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
有人知道为什么会这样吗?我知道在第一部分我没有将扬声器电话设置为假;如果我这样做,声音只能通过耳机播放。我怀疑这条线是“太短而不能被尊重”。 非常感谢你!
---------更新MEDIAPLAYER CODE -------------
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
答案 0 :(得分:1)
原来,我不应该在BroadcastReceiver中播放音乐(检测媒体按钮)因为音乐只会持续执行onReceive。因此,必须创建一个服务才能在录制之前播放。