Background music for call
Some mobile platforms do support this functionality at the platform-level (e.g. I've tested this with the Qualcomm APQ8064 where I had configured the hardware codec route accordingly, and it works). But Android doesn't have an API that let's you use this as an app developer, so on a non-rooted phone there's not really anything you can do.
Why there's no API for it - or whether there will be one in coming versions of Android - I don't know. There might be legal issues in some markets, or it might just be because it's considered as a semi-obscure feature that they don't see a great demand for and which is supported only by a relatively small number of platforms.
Another thing to consider is, do you really want to have music playing in the background during a call?
The audio is going to be 8 kHz mono (or 16 kHz mono if you're in a wideband call), and it's going to be AMR-encoded along with the rest of the speech signal (AMR is a low-bitrate codec designed for encoding speech). In other words, any music you play over the voice call uplink is going to sound really, really bad.
UPDATE: For APQ8064-based phones where you have root access, the procedure would be something like this (I don't remember the exact syntax off the top of my head):
audioManager.setParameters("incall_music_enabled=true");
// Execute the following using e.g. Runtime.exec()
amix 'SLIMBUS_4_RX Audio Mixer MultiMedia2' 1
aplay -Dhw:0,1 mono_8khz_data.pcm
// When you want to stop music playback, stop the aplay process, and then do
audioManager.setParameters("incall_music_enabled=false");
Why does the Background music not pause after a call disconnects?
I have solved my problem. Below, I am posting code for my music service.
public class ChalisaService extends Service implements OnCompletionListener
{
static MediaPlayer mediaPlayer;
static int playerFlag = 0;
TelephonyManager tm;
ActivityManager actManager;
/**
* 0 for stop/pause
* 1 for play*/
@Override
public IBinder onBind(Intent intent)
{
return null;
}//onBind
@Override
public void onCreate()
{
super.onCreate();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.chalisa);
mediaPlayer.setVolume(100, 100);
mediaPlayer.setOnCompletionListener(this);
tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}//onCreate
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
if(mediaPlayer.isPlaying())
{
mediaPlayer.pause();
/*HanuAlarm.txtPlay.setText("Play");
HanuAlarm.btn_Play.setBackgroundResource(R.drawable.btnplay);*/
playerFlag = 0;
}//if
else
{
mediaPlayer.start();
/*HanuAlarm.txtPlay.setText("Pause");
HanuAlarm.btn_Play.setBackgroundResource(R.drawable.btnpause);*/
playerFlag = 1;
}//else
startForeground(0, null);
return playerFlag;
}//onStartCommand
@Override
public void onDestroy()
{
super.onDestroy();
/*//mediaPlayer.stop();
//mediaPlayer.release();
playerFlag = 0;
Log.v("Chalisa service", "on destroy called");*/
}//onDestroy
private PhoneStateListener mPhoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
if(mediaPlayer.isPlaying())
{
mediaPlayer.pause();
playerFlag = 0;
}//if
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(mediaPlayer.isPlaying())
{
mediaPlayer.pause();
playerFlag = 0;
}//if
break;
/**
* Nitish
* 26 Sep 2012, Wed
* 11:50 AM*/
case TelephonyManager.CALL_STATE_IDLE:
if(!mediaPlayer.isPlaying())
{
mediaPlayer.start();
playerFlag = 1;
}//if
default:
Log.d("Chalisa Service", "Unknown phone state=" + state);
}
}
};
public void onCompletion(MediaPlayer mp)
{
mp.stop();
mp.release();
playerFlag = 0;
stopSelf();
updateUI();
Log.v("Chalisa Service media player", "on completion listener called");
}
private void updateUI()
{
Intent in = new Intent("com.dzo.HanumanChalisaWithAudioAndAlarm.UPDATE_UI");
in.putExtra("Player_FLAG_VALUE", playerFlag);
getApplicationContext().sendBroadcast(in);
}
}//ChalisaService
Related Topics
Google Drive API Doesn't Play Well with Proguard (Npe)
Moving from One Activity to Another Activity in Android
How to Get the File *.Apk Location in Android Device
Can Gradientcolor Be Used to Define a Gradient for a Fill or Stroke Entirely in Xml
Application Content Goes Behind the Navigation Bar in Android L
Swiperefreshlayout + Webview When Scroll Position Is at Top
Unexpected Namespace Prefix "Xmlns" Found for Tag Linearlayout
Android Class Parcelable with Arraylist
Why Is Calling Process.Killprocess(Process.Mypid()) a Bad Idea
Android:Google Maps API Key Signup:Md5 Certification Key
Android: Onsaveinstancestate Not Being Called from Activity
Get the Co-Ordinates of a Touch Event on Android
Onserviceconnected Never Called After Bindservice Method