0

I'm attempting to develop an android application that shows a popup whenever any app starts to play audio, even when the app is minimised.

So far I have attempted to use a service with an onAudioFocusChange listener, but the listener is never called. Here is what I have so far:

public class MyService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i("AUDIO", "SERVICE STARTED"); //CALLED

        AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
        AudioManager.OnAudioFocusChangeListener listener = new  AudioManager.OnAudioFocusChangeListener(){
            @Override
            public void onAudioFocusChange(int i) {
                Log.i("AUDIO", "FOCUS CHANGED"); //NOT CALLED
            }
        };

        return super.onStartCommand(intent, flags, startId);
    }

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

Am I on the right track?

Edit: Tried registering the listener as suggested but still not working. Even in the main activity.

public class MainActivity extends AppCompatActivity {
    @Override


  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.test);

        Button btnPlay1 = (Button) findViewById(R.id.btn_play_1);

        btnPlay1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.start();
            }
        });

        AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);

        AudioManager.OnAudioFocusChangeListener listener = new  AudioManager.OnAudioFocusChangeListener(){
            @Override
            public void onAudioFocusChange(int i) {
                Log.i("AUDIO", "FOCUS CHANGED"); //NOT CALLED
            }
        };

        am.requestAudioFocus(listener, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN );
    }

}
Aces_Charles
  • 141
  • 3
  • 14
  • https://stackoverflow.com/a/3586233/3364266 This link may help you – Samir Bhatt Sep 04 '17 at 09:07
  • @SamirBhatt Thanks for the link but that answer suggests using AudioManager.OnAudioFocusChangeListener which is what I've tried above. Maybe I am using the service wrong. – Aces_Charles Sep 04 '17 at 09:12

1 Answers1

2

You have just declared listener in your code, but not registered it. you need to register like

am.requestAudioFocus(listener, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN );

check this out

Sandip Fichadiya
  • 3,430
  • 2
  • 21
  • 47
  • I tried registering it like you suggested but it still doesn't work. I've even tried playing an audio file in the app and having the listener in the main activity (see edit). – Aces_Charles Sep 04 '17 at 10:04
  • @Aces_Charles check the result of am.requestAudioFocus. int result = am.requestAudioFocus(listener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN ); – Sandip Fichadiya Sep 04 '17 at 10:16
  • It seems to be working now. Not sure why it didn't before. Possibly didn't save the file or it didn't rebuild. Thanks for the help mate. – Aces_Charles Sep 04 '17 at 10:53
  • @Aces_Charles Glad it worked :). Sometimes you might receive AudioManager.AUDIOFOCUS_REQUEST_FAILED while requesting focus. so consider handling that situation like may be try requesting again after some time. – Sandip Fichadiya Sep 04 '17 at 10:56