Is it possible to control the default media player volume in Android from an Android application

I have the following requirement.

I am developing a mobile application for Android. A timer is set for a specific duration for activity.

  • I need to play the “beep” three times when the timer lasts 10 seconds to finish (i.e. become zero), two times the “beep” when the timer lasts 5 seconds to complete and once the beep “beep” signal "at the end of the timer.

  • The user can play music using the default music player on the Android phone when using the Android mobile application. I need to implement the logic, so when the sound of the sound signal is played from the mobile application, I need to first lower the default music volume, and then play the sound “sound signal” and reset again to the original volume after the sound “sound signal” was played at the right time.

I wanted to know how technically possible or not.

+3
source share
2 answers

, , "". . ", - , , , , ".

.

: , "". , - , . , -, , , .

AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,
                             // Use the music stream.
                             AudioManager.STREAM_NOTIFICATION,
                             //tell them it ok to duck
                             AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
    // Start playback.
}

, -.

+1

Source: https://habr.com/ru/post/1787322/


All Articles