0

I'm trying to play an audio file through the default speaker or through the little speaker (used during calls). Now i'm trying to choose between these 2 options using the proximity sensor. Here's my code:

am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.STREAM_MUSIC);
m=MediaPlayer.create(getApplicationContext(), R.raw.a_meno_che_non);
m.setAudioStreamType(AudioManager.STREAM_MUSIC);
am.setSpeakerphoneOn(true);




SensorEventListener proximitySensorEventListener = new SensorEventListener(){
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
    @Override
    public void onSensorChanged(SensorEvent event) {
        if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){

            if((int)(event.values[0])==myProximitySensor.getMaximumRange()){


                am.setSpeakerphoneOn(true);



            }

            else {


                am.setSpeakerphoneOn(false);


            }



        }

    }

};

this works, but not very well because when switching between the speakers it loses a part of audio (about 2 seconds).

How can I solve this?

asdf
  • 97
  • 1
  • 10
  • maybe duplicate http://stackoverflow.com/questions/19771905/play-audio-trough-phone-speaker-of-an-android-device – An-droid Feb 15 '16 at 15:29
  • No, I think it's a quite different problem because if I use `am.setMode(AudioManager.STREAM_VOICE_CALL);` it works well but doesn't change speaker – asdf Feb 15 '16 at 16:36

0 Answers0