Reputation: 9
I am working on an app in which I have to play the sound of the notification being generated per second on the shake of the phone from internal speaker, and at the same time my app is connected to the bluetooth of a car via (a2dp). I have to route the notification audio to internal speaker and all the rest of the sounds (music,calls,alerts etc) to the Bluetooth speaker. Is this possible?
I have researched all the relavant questions posted on stack over flow but none seem to answer them. Following is the snippet of code that i have tried via the Audio manager class.
I have tried to route the audio stream using the built in methods
AudioManager audioManager = (AudioManager) getContext().getSystemService(AUDIO_SERVICE);
audioManager.setMode(AudioManager.ROUTE_EARPIECE);
audioManager.setSpeakerphoneOn(false);
Which for the time cuts down the stream from the Bluetooth speaker but as soon any music is played behind the stream is again routed towards the BT speaker and then there is no notification sound. I have tried all the audio manager routs and modes but none seem to work. The notification sound is an mp3 and the path is set by parsing URI and this how i am building my notification.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.newlogo)
.setLargeIcon(largeIcon)
.setSound(uri)
.setContentTitle(notificationHeading)
.setContentText(notificationBody)
.setPriority(Notification.PRIORITY_HIGH).setVibrate(v);
notificationManager.notify(1,mBuilder.build());
So if there is a slightest chance of this thing being possible i am more than willing to take it, any library even paid ones if solves this problem please do share and also i cannot root the phone this app is to be published on play-store.
Upvotes: 1
Views: 1054
Reputation: 721
Play the sound of the speaker notification via AudioTrack.java class and use stream AUDIO_STREAM_TTS (abbrev. 'transmitted trough speaker') or AUDIO_STREAM_ENFORCED_AUDIBLE .
AUDIO_STREAM_ENFORCED_AUDIBLE = 7, /* Sounds that cannot be muted by user
* and must be routed to speaker
*/
AUDIO_STREAM_TTS = 9, /* Transmitted Through Speaker.
* Plays over speaker only, silent on other devices.
*/
Upvotes: 1