1

So I wrote this piece for an Android App. The idea is simple:

Detect whether sound is playing and if yes open the BluethoothSCO Channel so the audio gets played there.

I want to use it to "route" the navigation direction infos to my car speaker. It works nearly like expected.

There is a 'huge' delay of around 1 second between the audio recognition and the bluethoothsco connection being ready. This results in a loss of nearly half the navigation information.

My Idea was to add a delay or pause the playback for a second.

NOW: Sound is detected -> BluethootSCO opening (sound keeps playing here) -> sound over car speaker

IDEA: Sound is detected -> pause/delay for 1 sec ->BluethootSCO opening -> resume playback -> sound over car speaker

I thought about recording it and playing it afterwards but that would be to late for some informations like "turn left NOW".

A short delay would be ok, but I have no idea on to implement this :(

Since the app is only for myself using root would be ok. Maybe there is an possibility direct at the AudioFlinger?

public void checkSound() {
    AudioManager localAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);

    Visualizer localVisualizer = new Visualizer(0);
    localVisualizer.setEnabled(true);
    Visualizer.MeasurementPeakRms localPeak = new Visualizer.MeasurementPeakRms();

    boolean wasPlayingBefore=false;
    while (keeprunning) //Loop to detect changes on the Media Audio Stream
    {
        localVisualizer.getMeasurementPeakRms(localPeak);
        if(localPeak.mPeak > -8500 && !wasPlayingBefore)
        {
            //There is some audio output
            wasPlayingBefore=true;
            localAudioManager.setBluetoothScoOn(true);
            localAudioManager.startBluetoothSco();
            localAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
            android.os.SystemClock.sleep(5000); //Route to BT Headset will exist min. 5 seconds ...
        }
        if(localPeak.mPeak <= -8500 && wasPlayingBefore)
        {
            //output (temporary) gone
            android.os.SystemClock.sleep(2000);//... plus this 2 seconds
            //check again
            localVisualizer.getMeasurementPeakRms(localPeak);
        }
        if(localPeak.mPeak <= -8500 && wasPlayingBefore)
        {
            //Audio didn't get back in last 2 seconds...
            wasPlayingBefore=false;
            localAudioManager.setBluetoothScoOn(false);
            localAudioManager.stopBluetoothSco();
            localAudioManager.setMode(AudioManager.MODE_NORMAL);
        }
        android.os.SystemClock.sleep(100); //Slow down the loop
        Log.d("Peak", String.valueOf(localPeak.mPeak));//Debug info - Audio peak -9600 = Silent, 0 = MAX Output
   }
    localVisualizer.release();
}
  • I'm not sure if this would be acceptable but have you considered just a simple for loop counter so you're program is busy for a couple of seconds before outputting the audio? – DigitalNinja Mar 12 '15 at 17:03
  • my programm doens't play any sound itself. It detects the playback of other apps. I need to pause/delay the playback of the other app while my app opens the bluethoot channel – JohanKingston Mar 12 '15 at 17:31

0 Answers0