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 https://openweathermap.org/weather-conditions.
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() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
final ImageView current_icon = rootView.findViewById(R.id.imageView6);
viewModel = new ViewModelProvider(this).get(WeatherDataViewModel.class);
viewModel.getWeatherDataLiveData().observe(getViewLifecycleOwner(), data -> {
int drawableResource = -1;
String icon = data.getWeather().get(0).getIcon();
switch (icon) {
case "01d":
case "01n":
drawableResource = R.drawable.sun;
break;
case "02d":
case "021n":
drawableResource = R.drawable.few_clouds;
break;
case "03d":
case "03n":
drawableResource = R.drawable.scattered_clouds;
break;
case "04d":
case "04n":
drawableResource = R.drawable.broken_clouds;
break;
case "09d":
case "09n":
drawableResource = R.drawable.shower_rain;
break;
case "10d":
case "10n":
drawableResource = R.drawable.small_rain;
break;
case "11d":
case "11n":
drawableResource = R.drawable.thunderstorm;
break;
case "13d":
case "13n":
drawableResource = R.drawable.snow;
break;
case "50d":
case "50n":
drawableResource = R.drawable.mist;
break;
}
if (drawableResource != -1)
current_icon.setImageResource(drawableResource);
return rootView;
}
public void getWeatherData(String name) {
viewModel.setCityName(name);
}
}
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.