K
K
Konstantin_Pak_Swinburne2019-09-30 15:26:43
Android
Konstantin_Pak_Swinburne, 2019-09-30 15:26:43

How to make it so that when the screen of the android simulator is rotated, a sound is made?

The task is such that when the android simulator changes position from Portrait to Landscape Mode, sound should be produced. There are no questions with sounds, I have sounds, I turned them on through MediaPlayer. I already wrote android:configChanges = "orientation | screenSize" in the manifest, but it's not clear how to write code in MainActivity.java. Can someone help?
5d91f46fd7536192698393.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kuusandr, 2019-09-30
@Kuusandr

something like this

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        play_sound();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        play_sound();
    }
}

private void play_sound()
{
try {
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();
} catch (Exception e) {
    e.printStackTrace();
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question