【问题标题】:Android: What Audio Mode should be set to send receive voice between devicesAndroid:应设置什么音频模式以在设备之间发送接收语音
【发布时间】:2016-01-08 20:12:54
【问题描述】:

我正在尝试在两个 Android 设备平板电脑和移动设备(通过 java 套接字)之间传输语音/音频(双向)。

平板电脑可以清晰播放接收到的音频(语音),但手机将接收到的音频播放为噪音。
然后我在平板电脑的代码中设置此音频模式:
audioManager.setMode(AudioManager.MODE_IN_CALL);

这会导致移动设备接收到清晰的语音。 但是平板电脑会静音,它不会播放接收到的音频(或者更确切地说是听不到)。

我不确定我应该在这里使用哪种 AudioManager 模式组合(如果有)?

【问题讨论】:

  • 我的回答对你有用吗?

标签: android android-networking android-audiomanager android-audiorecord


【解决方案1】:

可以将你想播放的声音处理成Alarm

创建一个名为 AlarmController 的新类并尝试此代码。

这适用于我在 Android 4.4.2(华为 Avence P7)上,每个系统音量(媒体、铃声、闹钟)设置为 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();
}

我希望这对你有用。 :)

【讨论】:

  • 我不知道为什么这个答案被授予赏金 - 它没有选择它作为接受的答案 - 而且这个答案与提出的问题无关。
  • 这是唯一给出的问题,在时间用完后获得了 2+ 票
猜你喜欢
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多