安卓:什么音频模式应设置为发送接收设备之间的语音设置为、语音、音频、模式

2023-09-05 01:47:57 作者:转身、遇见鬼

我想流之间的两款Android平板设备和移动(对Java接口)语音/音频(双向)。

I am trying to stream voice/audio (two way) between two Android devices Tablet and Mobile (over java sockets).

平板可以播放接收到的音频(语音)明确,但移动播放接收到的音频噪声。 然后,我在平板电脑上的code设置此音频模式: audioManager.setMode(AudioManager.MODE_IN_CALL);

The Tablet can play received audio(voice) clearly, but the Mobile plays received audio as noise. Then i set this audio mode in the code on tablet: audioManager.setMode(AudioManager.MODE_IN_CALL);

现在,这将导致移动接收清晰的声音。 但平板电脑进入沉默,不能播放接收到的音频(或者更确切地说,它不发声)。

This now results in Mobile receiving clear voice. But the tablet goes silent, it does not play the received audio (or rather its not audible).

我不知道我应该在这里使用什么样的组合(如果有的话)的AudioManager模式?

I am not sure what combination (if any) of AudioManager mode i should use here?

推荐答案

这是可能的处理您要为报警播放声音

It's possible to handle the sound you want to play as Alarm.

创建 AlarmController 命名为一个新的类,并尝试这个code。

Create a new class named AlarmController and try this code.

这为我工作在Android 4.4.2(华为Ascend P7)是每个系统卷(媒体,铃声,报警)设置为0。

This worked for me on Android 4.4.2 (Huawei ascend P7) with each system volume (Media, Ringtone, Alarm) set to 0.

Context context;
MediaPlayer mp;
AudioManager mAudioManager;
int userVolume;


public AlarmController(Context c) { // constructor for my alarm controller class
    this.context = c;
    mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    //remeber what the user's volume was set to before we change it.
     userVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);

     mp = new MediaPlayer();
}

public void playSound(String soundURI){

    Uri alarmSound = null;
    Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);

    try{
        alarmSound = Uri.parse(soundURI);
    }catch(Exception e){
        alarmSound = ringtoneUri;
    }
    finally{
        if(alarmSound == null){
            alarmSound = ringtoneUri;
        }
    }



    try {

        if(!mp.isPlaying()){
        mp.setDataSource(context, alarmSound);
        mp.setAudioStreamType(AudioManager.STREAM_ALARM);
        mp.setLooping(true);
        mp.prepare();
        mp.start();
        }


    } catch (IOException e) {
        Toast.makeText(context, "Your alarm sound was unavailable.", Toast.LENGTH_LONG).show();

    }
    // set the volume to what we want it to be.  In this case it's max volume for the alarm stream.
   mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), AudioManager.FLAG_PLAY_SOUND);

}

public void stopSound(){
// reset the volume to what it was before we changed it.
    mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, userVolume, AudioManager.FLAG_PLAY_SOUND);
    mp.stop();
   mp.reset();

}

public void releasePlayer(){
    mp.release();
}

我希望这对你的作品。 :)

I hope this works for you. :)