Feroz Siddiqui
Feroz Siddiqui

Reputation: 4096

Android Audio Focus Listener not working in service

I have Play tone service in which i have to wait for audio focus to play a tone.

Below is code which i have implemented :

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioAttributes;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;

public class PlayToneService extends Service implements AudioManager.OnAudioFocusChangeListener {
    private static final String TAG = "PlayToneService";

    public PlayToneService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();

        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        Log.d(TAG,"AudioManager.requestAudioFocus...");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            audioManager.requestAudioFocus(new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
                    .setAudioAttributes(
                            new AudioAttributes.Builder()
                                    .setUsage(AudioAttributes.USAGE_MEDIA)
                                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                                    .build()
                    )
                    .setAcceptsDelayedFocusGain(true)
                    .setOnAudioFocusChangeListener(this).build()
            );
        } else {

            audioManager.requestAudioFocus(this,
                    AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN);
        }



    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onAudioFocusChange(int focusChange) {
        if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
            // You now have the audio focus and may play sound.
            final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 500);
            tg.startTone(ToneGenerator.TONE_PROP_BEEP);
            Log.d(TAG,"AudioManager.AUDIOFOCUS_REQUEST_Sucess...");

            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                public void run() {
                    tg.release();
                    stopSelf();
                }
            },550);
        }
        else if (focusChange == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
            // Handle the failure.
            Log.d(TAG,"AudioManager.AUDIOFOCUS_REQUEST_FAILED ...");
            stopSelf();

        }

    }
}

I am not getting any callback is my onAudioFocusChange listener. There is no logger printing in my onAudioFocusChange listner.

Upvotes: 0

Views: 1709

Answers (1)

codemaster91
codemaster91

Reputation: 86

Your AudioFocusChangeListener instance will not be called upon initial request to the AudioManager (e.g. calling requestAudioFocus()). Use the return value from requestAudioFocus() to determine if you have initially been granted audio focus from the system. Your change listener should be called for any subsequent changes to focus.

Upvotes: 1

Related Questions