Click here to Skip to main content
15,072,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've just finished implementing my app's weather icons, now I'm trying to play weather sounds on the app based on the icons the app is displaying using MediaPlayer. After doing long hours of thorough research, I found out to my shock that no one
has ever asked similar questions on any platform. So as the guides to try it is limited, I decided to ask it here for help. And I'll surely appreciate it if anyone can.

Following the API instructions, I'm using
There are 9 main weather conditions.

These are my goals in trying to play the sounds based on the icons:
*If the city displays the clear_sky icon, play the clear_sky_sound
*Otherwise, If it displays the few_clouds icon, play the few_clouds_sound
*Otherwise, If it displays the scattered_clouds icon, play the scattered_clouds_sound
*Otherwise, If it displays the broken_clouds, play the broken_clouds_sound
*Otherwise, If it displays the shower_rain icon, play the shower_rain_sound
*Otherwise, If it displays the rain icon, play the rain_sound
*Otherwise, If it displays the thunderstorm icon, play the thunderstorm_sound
*Otherwise, If it displays the snow icon, play the snow_sound
*Otherwise, If it displays the mist icon, play the mist_sound.

I would really like if someone would correct me and show me what to do instead as
I'm quite confused. You don't have to do every single part, even if it's just few so that I can understand and implement it from there.

Here is my fragment's(specific) code:
public class FirstFragment extends Fragment {

    private WeatherDataViewModel viewModel;

    MediaPlayer firstSound, secondSound, thirdSound, fourthSound, fifthSound, sixthSound, seventhSound, eightSound, ninethSound;

    public FirstFragment() {
// Required empty public constructor

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_first, container, false);
        final ImageView current_icon = rootView.findViewById(;

    // Get our ViewModel instance
        viewModel = new ViewModelProvider(this).get(WeatherDataViewModel.class);

    // And whenever the data changes, refresh the UI
        viewModel.getWeatherDataLiveData().observe(getViewLifecycleOwner(), data -> {

            int drawableResource = -1; // here define default icon for example R.drawable.default_weather_icon

// display weather icons

                String icon = data.getWeather().get(0).getIcon();

                switch (icon) {
                    case "01d":
                    case "01n":
                        drawableResource = R.drawable.sun;

                    case "02d":
                    case "021n":
                        drawableResource = R.drawable.few_clouds;

                    case "03d":
                    case "03n":
                        drawableResource = R.drawable.scattered_clouds;

                    case "04d":
                    case "04n":
                        drawableResource = R.drawable.broken_clouds;

                    case "09d":
                    case "09n":
                        drawableResource = R.drawable.shower_rain;

                    case "10d":
                    case "10n":
                        drawableResource = R.drawable.small_rain;

                    case "11d":
                    case "11n":
                        drawableResource = R.drawable.thunderstorm;

                    case "13d":
                    case "13n":
                        drawableResource = R.drawable.snow;

                    case "50d":
                    case "50n":
                        drawableResource = R.drawable.mist;

                if (drawableResource != -1)

        return rootView;

    public void getWeatherData(String name) {
// The ViewModel controls loading the data, so we just
// tell it what the new name is - this kicks off loading
// the data, which will automatically call through to
// our observe() call when the data load completes

What I have tried:

While trying to implement it, i came up with this method:
switch is playing sound
 case 1:
if icon || clear_sky
sound1 = R.raw.clear_sky_sound

case 2:
if icon || few_clouds
sound2 = R.raw.clear_sky_sound 

But I got errors everywhere, There's no single API/tutorial guiding on playing weather sounds.
Updated 18-Aug-21 21:30pm
David Crow 27-Aug-21 10:42am
"But I got errors everywhere..."

And those errors would be what exactly? None of the code you've shown has anything to do with playing sounds.

"There's no single API/tutorial guiding on playing weather sounds."

Playing a sound is just that: playing a sound. Regardless of what it represents or where it comes from, there is no difference from any other sound. Richard provided you the link to the MediaPlayer docs.

if icon || clear_sky

That is not valid Java, what are you expecting?

MediaPlayer overview  |  Android Developers[^]
Chinedum Ezeozue 18-Aug-21 9:51am
I have no idea about how to do it. That's why I came here for experienced devs help
Richard MacCutchan 18-Aug-21 10:34am
This forum is for quick answers, we cannot teach you either the Java language, or the specifics of Android development. Go to the link I gave you and it will explain how to play sound files..
I'd suggest to reject set of if's. Use Dictionary (Java Platform SE 8 )[^] instead, where:
Key - icon
Value - song.

More details - here: The Java Dictionary Class: Definition & Example |[^]
Chinedum Ezeozue 19-Aug-21 7:38am
Okay, thanks a lot for your suggestion🙏 I really appreciate
Maciej Los 19-Aug-21 8:11am
You're very welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900