Java – react native : Volume Button Should Only Control Media (Android)

react native : Volume Button Should Only Control Media (Android)… here is a solution to the problem.

react native : Volume Button Should Only Control Media (Android)

I would like to know how to force the volume buttons to control media only in my app.

I know this is an android-specific issue and ios does this by default.

There are two related problems given the Android solution:

Volume Control in android application

How can I manage audio volumes sanely in my Android app?

They all recommend adding setVolumeControlStream(AudioManager.STREAM_MUSIC); To Android, but I’m a react-native developer and not familiar with Java

And this react-native related question:

How to control media volume?

This leaves me wondering where onCreate() is.

Any specific suggestions for where to add this line in the java section of react-native?

Solution

So I did some research, thanks to @VolkanSahin45 comment, I figured it out.

Add setVolumeControlStream(AudioManager.STREAM_MUSIC); is correct.

What I have to do is the onCreate() function in Override MainActivity.java.

public class MainActivity extends ReactActivity {
    /* Any previous code you had here */
    [...]

/* Override the onCreate function here */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        /* Add this line to keep the original behavior of onCreate() */
        super.onCreate(savedInstanceState);
        /* This one does the trick */
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    }
}

Related Problems and Solutions