Play sound through earpiece when using MediaPlayer - android

I am playing voicemail recordings in my app. The way I currently have it set up, it plays the voicemail through the speakerphone. What is the best way to be able to toggle between speakerphone and earpiece. Here is how I set up my MediaPlayer:
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
} catch (Exception e) {
e.printStackTrace();
return;
}
mediaPlayer.prepareAsync();
I am building for 4.1 plus.

You need to set audio manager mode too. and then using audiomgr.setSpeakerphoneOn(false) api you can toggle.
audiomgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audiomgr.setMode(AudioManager.STREAM_MUSIC);
audiomgr.setSpeakerphoneOn(false);

Related

Playing an alarm sound while device's media volume is off

I am creating a alarm kind of application.When I open a particular activity, I want to play a custom sound with alarm volume level of device.
If device's media volume is off and alarm volume is on then my custom sound should play.
What have I already tried:
private void playAlarmSound(String fileName) {
MediaPlayer p = new MediaPlayer();
AudioManager audioManager= (AudioManager) getSystemService(AUDIO_SERVICE);
try {
AssetFileDescriptor afd = this.getAssets().openFd(fileName);
int volumeLevel=audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
p.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
p.setVolume(volumeLevel,volumeLevel);
afd.close();
p.prepare();
} catch (Exception e) {
e.printStackTrace();
}
p.start();
}
But by this when media volume is on it is playing sound but when media volume is off it is not playing the sound.
You are reading the alarm volume level but this doesn't mean that the audio will be played through that stream.
You need to build and set the AudioAttributes on the MediaPlayer, indicating USAGE_ALARM.
In other words:
p.setAudioAttributes(
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
);

playing android audio from a specified time

How can i play audio from a specified time ?
I have a prerecorded audio and i want to play it from a specific time to a specific time.
I have tried seekTo function in MediaPlayer but it doesnt seem to work.
mPlayer = new MediaPlayer();
try{
mPlayer.setDataSource(PATH_FILE + selectedAudio);
mPlayer.prepare();
mPlayer.seekTo(2);
mPlayer.start();
}catch(IOException ioe){
System.out.println("Error in startPlaying method");
}
Thanks in advance

How to play music from receiver at android 5.1, it is different form android 4.2

I want to play music from the receiver instead of loud-speaker, I use below code:
am.setMode(AudioManager.MODE_IN_CALL);
mMediaPlayer = MediaPlayer.create(this, R.raw.src2k);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
It works well in android 4.2, but it does not work in android 5.1, at android 5.1 it still play from loud-speaker. I have tested several cell phones at android 4.2 and 5.1. Anyone can give me some advice? Thanks.
I have solved this problem by myself, below code works well:
mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setMode(AudioManager.STREAM_MUSIC);
mAudioManager.setSpeakerphoneOn(false);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.reset();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
Uri uri=Uri.parse("android.resource://com.ee.ff/"+R.raw.music);
try
{
mMediaPlayer.setDataSource(this, uri);
}
catch (Exception e)
{
e.printStackTrace();
}
mMediaPlayer.setLooping(true);
try
{
mMediaPlayer.prepare();
}
catch (Exception e)
{
e.printStackTrace();
}
mMediaPlayer.start();

Play short sound without interrupting playing music

I play music in my app using MediaPlayer. But music interrupts when I playing another sound (duration of sound is about 1 sec). The interruption is short, only a few ms, but it disturbs me.
What should I do to avoid this interruption?
UPDATE:
Here is code that I use to play short sounds:
private final Map<Integer,MediaPlayer> mediaPlayers = new HashMap<Integer,MediaPlayer>();
void playSound(int resId, boolean looping) {
MediaPlayer mediaPlayer = mediaPlayers.get(resId);
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayers.remove(resId);
}
try {
mediaPlayer = MediaPlayer.create(context, resId);
if (mediaPlayer == null){
throw new RuntimeException("Can't create MediaPlayer");
}
mediaPlayer.setLooping(looping);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
} catch (IllegalStateException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
mediaPlayers.put(resId, mediaPlayer);
}
UPDATE2:
I solved the problem. The problem was that the short sound was in mp3 format, but all the other sounds in my app were in ogg format. So I converted this sound in ogg format and the music is no longer interrupted.

background sound during call in android

I want to play a sound automatically when I call to somebody
but I don't know the way for this.
It is possible because we have some application that play some noise (traffic and ...) to background of call but I could not find a code or something for this
example of sound play in background
this is a code to play music
MediaPlayer player;
AssetFileDescriptor afd;
try {
// Read the music file from the asset folder
afd = getAssets().openFd(“home.mp3″);
// Creation of new media player;
player = new MediaPlayer();
// Set the player music source.
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),afd.getLength());
// Set the looping and play the music.
player.setLooping(true);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
but can we use this during call?

Categories

Resources