TrapezeArtist
TrapezeArtist

Reputation: 907

Play a Sound When Making a Selection in Settings (Android App)

I have a settings activity in my Android app that selects a sound from a choice of files in the res/raw folder. There are other settings too but the essential part of the settings_pref.xml file is

    <PreferenceCategory android:title="Pager Tone">

    <ListPreference
        android:defaultValue="pager1"
        android:entries="@array/pagerName"
        android:entryValues="@array/pagerFile"
        android:key="pagerSound"
        android:title="Select Pager Tone"
        app:useSimpleSummaryProvider="true" />

</PreferenceCategory>

Then the SettingsFragment.java is

import android.os.Bundle;
import androidx.preference.PreferenceFragmentCompat;

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.settings_pref, rootKey);
    }
}

Simple. And it works. Now I want to play a short sample of the sound when the user selects it. That's where I'm coming unstuck. I've been trying out various suggestions that I have found on the internet but I haven't got anything to work yet.

The best looking option I have found is to change the SettingsFragment.java to this

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.settings_pref, rootKey);

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
        String pagerTone =  sharedPreferences.getString("pagerSound", "");
        Resources res = this.getResources();
        int soundId = res.getIdentifier(pagerTone, "raw", "cgpagerdev");
        MediaPlayer player = MediaPlayer.create(getContext(), soundId);
        AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

        int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int Volume = (int) (maxVolume * 0.2);

        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, Volume,AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
        player.start();
    }

}

I don't know if it will actually work because Android Studio gives the following error:

Cannot resolve method 'getSystemService' in 'SettingsFragment'

Following any of the suggestions from Android Studio just seems to generate other error messages. Similar code in another place works OK so I can't see what is going wrong here. I know I will have to add something to make the sound stop again (perhaps a timer to limit it to 1 or 2 seconds) but I would just like to get it working at all first.

Upvotes: 0

Views: 243

Answers (1)

oemel09
oemel09

Reputation: 763

getSystemService is a method of the class Context (1)

Activity extends Context so you can call the method from within an activity. Fragment doesn't extend anything so you need to get the context by calling getContext or requireContext in your fragment and then call getSystemService.

AudioManager mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);

Upvotes: 1

Related Questions