Android: What Audio Mode should be set to send receive voice between devices
Asked Answered
A

1

7

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

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).

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

Asbestos answered 11/10, 2015 at 7:39 Comment(1)
is my answer working for you?Alvinaalvine
A
2

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

Create a new class named AlarmController and try this code.

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. :)

Alvinaalvine answered 21/10, 2015 at 21:22 Comment(2)
I am not sure why bounty has been awarded to this answer - it was not select it as accepted answer - also this answer is unrelated to question asked.Asbestos
It was the only question given and had 2+ votes after the time runs outAlvinaalvine

© 2022 - 2024 — McMap. All rights reserved.