background sound during call in android - 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?

Related

MediaPlayer plays only 2-3 seconds not full mp3

There is mp3 file which is 20 seconds long.
But MediaPlayer only plays 2-3 seconds.
I am using below code to play mp3 from raw file.
try{
AssetFileDescriptor afd = getApplicationContext().getResources().openRawResourceFd(R.raw.buzzer6);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp){
mp.start();
}
});
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
} catch (IOException ioe) {
ioe.printStackTrace();
}
So what is the issue?
I want to Play full mp3 file which is 20 seconds.
I suggest you to have a look at this post
Basically if you try to enlarge the scope of MediaPlayer as instance variable it should work
(In the example is declared static, you probably do not need to declare it static)
just change this line:
MediaPlayer mediaPlayer = new MediaPlayer();
in:
private MediaPlayer mediaPlayer //is an instance variable not a local one
while into the method you maintain:
mediaPlayer = new MediaPlayer();

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

Can't play media from Assets on Android

In one of my Activities I want to show a .gif image(load it from assets) and play a music file. It is really interesting that if I remove the code which loads the gif, the music gets played, but if I want to load and show the gif, the music doesn't play! Why?
WebView web = (WebView) findViewById(R.id.web);
web.loadUrl("file:///android_asset/h.gif");
web.setPadding(0, 0, 0, 0);
web.setInitialScale(getScale());
AssetFileDescriptor afd = null;
try {
afd = getAssets().openFd("background.mp3");
MediaPlayer player = new MediaPlayer();
player.setDataSource(
afd.getFileDescriptor(),
afd.getStartOffset(),
afd.getLength());
player.prepare();
player.start();
}
catch (IOException e) {
e.printStackTrace();
}

Streaming music and save cache file on local on Android

I use below code to play streaming music:
try {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(URL);
mediaPlayer.prepare();
mediaPlayer.start();
}
catch(Exception e) {
e.printStackTrace();
}
I want to buffering file to local cache file at the same time.
And next time want to play this url music can play the local file directly.
How can I do it?
Or anyone can provide references?

Categories

Resources