Is there an equivalent requestAudioFocus function in Android API 7?

My Android application must support Android 2.1.x (this is the Android API level 7). I need to use the requestAudioFocus() and abandonAudioFocus() methods to pause other applications (like music) to play when my application starts playing media files and resumes them after the termination of my application.

However, these two functions are only available at API level 8 and above. What are the equivalent features at API level 7? Or how to do it up to API level 8?

+4
source share
1 answer

Thanks to Christopher (http://stackoverflow.com/questions/1993471/android-can-i-mute-currently-playing-audio-applications) I quote:

Audio processing on Android will be pretty awful for a while. The APIs are rather strange, poorly documented, and continue to change / become obsolete / break between versions. Even the AudioManager code contains FIXME.

In any case, there are several types of streams in Android (music, notifications, phone calls, etc.), and applications are designed to select the one suitable for playback. I believe most Android apps should use the music / media type (STREAM_TYPE_MUSIC). You install this on your MediaPlayer using the setAudioStreamType method.

The SDK allows you to set one type of stream as a solo, as a result of which all other streams will be turned off, but I do not think that you can identify the sound played by specific applications and somehow pause / pause it. Music apps in general will use PhoneStateListener to pause themselves when a call arrives.

Thus, in your case, you could “borrow” a phone call stream for your MediaPlayer and use the call to the AudioManager.setStreamSolo method (AudioManager.STREAM_VOICE_CALL, true) when playback starts, then do not solo the stream with false during playback or your activity is completed .

I can tell you that this works, but I can’t remember whether you also need to set the audio mode to MODE_IN_CALL when using the voice call stream (for example: AudioManager.setMode (AudioManager.MODE_IN_CALL)). If you find that it is necessary, you need to make sure that you return MODE_NORMAL mode after the playback is complete, otherwise whenever you press the hard volume keys, it will say “talk volume”! However, if and when you want to return to MODE_NORMAL, you must verify that an authentic phone call does not occur at this time ...

Perhaps you could use a different type of stream rather than a voice call, but I'm just talking from experience working on an application that can use either speakerphone or speaker to play sound, which requires the use of a voice call stream.

As I said, sound processing is not particularly interesting ...;)

+1
source

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


All Articles