/range:.audi/o
NEUE M|X~.tape:EMME, CURSIVE ET PARFUME[\|-𝓮𝓶𝓶𝓮,𝓒𝓾𝓻𝓼𝓲𝓿𝓮 𝓮𝓽-𝓟𝓪𝓻𝓯.𝓾𝓶𝓮|] ,by DJ*G*D*P' for the hazzmattes in collaboration with The algoriddims~~~ for the hazzmattes due in 6 days.

160+ Books for Web Developers - FREE for 7 days!
54:29:05
Blog
Community
Library
Login

mobile May 27, 2016 By Abbas Suterwala 
Managing Multiple Sound Sources in Android with Audio Focus
Sound is a great way to grab user attention, give interface feedback or immerse a player in a game. Imagine if multiple apps all tried to play sounds at the same time. This would result in an unintelligible cacophony and make users reach for the mute button. Android provides a simple API to play music and audio effects and manage different sources. The Android audio focus API lets an app request ‘audio focus’ and lets the app know if it has lost focus so it can react. In this tutorial I will show how to use these APIs in your own apps.
You can find the final code for this tutorial on GitHub.
Requesting Audio Focus for Your App
You should request audio focus in your app before playing any sound. To do this you need to get an instance of the AudioManager. Once you have the instance you can then use requestAudioFocus.
Create a new app with an empty activity and replace the contents of activity_main.xml with the following:
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView android:text="Audio Focus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btnRequestFocus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Request Audio Focus"/>

    <Button
        android:id="@+id/btnReleaseFocus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Release Audio Focus"/>

</LinearLayout>
This layout uses the LinearLayout style that contains two buttons. One to request focus and one to release it. Update the contents MainActivity.java to the following:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);