0

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,

sophie281988
  • 131
  • 9
  • In fact, what I want to do is: I have the package name of an application and the phone is in music mode or not. In the case that it is in music mode, could we know the playing audio is the application or it is another app. In other words, could we get the name of playing audio application? – sophie281988 Sep 08 '15 at 10:23

2 Answers2

1

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.

Community
  • 1
  • 1
Mariano Zorrilla
  • 7,165
  • 2
  • 34
  • 52
  • Thank you for your help, I have seen this question before, but it is not what I want. I couldn't use it for my app. I have just commended in my question to explain more. Could we do that? – sophie281988 Sep 08 '15 at 10:29
1
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);
    }
}
ifeegoo
  • 7,054
  • 3
  • 33
  • 38