sophie281988
sophie281988

Reputation: 131

How to detect an application is playing audio or recording?

I'm developing an android application. If we have the package name of an application, could we know whether an application is playing music or recording the voice or not? I have no idea how to do that. If someone has done it, could you please help me or give me the information about it?

Thank your for your help,

Upvotes: 0

Views: 2628

Answers (2)

Mariano Zorrilla
Mariano Zorrilla

Reputation: 7666

AudioManager is what you are looking for. You can check this response: https://stackoverflow.com/a/16252044/3743245 Also the official documentation: AudioManager

And a small example of how to use it:

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

// Request audio focus for playback
int result = am.requestAudioFocus(focusChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);


if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// other app had stopped playing song now , so you can do your stuffs now .
}

Them you add the focusChangeListener listed in the link I've left you.

Upvotes: 1

ifeegoo
ifeegoo

Reputation: 7292

public class AudioManager {
    /**
     * Checks whether any music is active.
     *
     * @return true if any music tracks are active.
     */
    public boolean isMusicActive() {
        return AudioSystem.isStreamActive(STREAM_MUSIC, 0);
    }
}

Upvotes: 1

Related Questions