开发者

2nd sound plays with less integrity?

开发者 https://www.devze.com 2023-04-09 06:51 出处:网络
I want to press the button on the headset, play a sound via the speaker phone, then record a 10 second audio clip. Here is my code:

I want to press the button on the headset, play a sound via the speaker phone, then record a 10 second audio clip. Here is my code:

        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();
            }

The first sound plays on speaker fine, with very good integrity. However, when I press the button again to stop recording, the sound is nearly inaudible, despite using the same clip. Here is the logcat trace:

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)
开发者_Go百科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

Does anybody know why this is the case? I know that in the first portion I did not set the speaker phone back to false; if I do, the sound only gets played through the headset. I am suspecting it is the line that says "too short to be respected." Thank you very much!

--------- UPDATE WITH 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

ok used this code to play the sound, but the 2nd sound is still very quiet (same problem as before)... more suggestions? :D


Turns out, I wasn't supposed to play music in a BroadcastReceiver (detects the media button) because the music would only last for as long as onReceive is executed. Thus, a service had to be created to play before recording.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号