I'm a noob in Android Studio and I created a Soundboard with over 300 Sounds. Is there a way to upload the Sounds in the Firebase and then play them out of the Firebase?
Here is the Code with only 2 Sounds. Just to show how my Soundboard works.
public class MainActivity extends AppCompatActivity{
public MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//plays sound
public void sound1(View view){
cleanUpMediaPlayer();
mp = MediaPlayer.create(this, R.raw.sound1);
mp.start();
}
public void sound2(View view){
cleanUpMediaPlayer();
mp = MediaPlayer.create(this, R.raw.sound2);
mp.start();
}
public void cleanUpMediaPlayer(){
if(mp != null) {
if(mp.isPlaying()) {
try{
mp.reset();
mp.prepare();
mp.stop();
mp.release();
mp = null;
}catch (Exception e){
e.printStackTrace();
}
}
}
}}
And this with 300 sounds.
Can you explain me how to put the sounds in the Firebase (if this is possible at all) and how to play them out of the Firebase?
Sorry for my bad experience and my English :)
As suggested by @AL in the comment, you may host the sounds in Firebase storage and each time when your app needs to play a sound, you may trigger a request to download or stream and play the sound.
But if these sounds are the mandatory resources for one or more features in your app and you want to make your app to be usable offline after the app has been started first time, I would suggest you upload these sounds as an APK expansion file.
Google Play will usually download the expansion files at the same time it downloads the APK, however you will still need to put your own logic to check whether the file has been downloaded and if it is not, download it from a URL provided to you in a response from Google Play's Application Licensing service when your app starts.
The reasons we need to check this every times the app starts are because:
The below tip from the APK Expansion Files document would especially suit your case if you are uploaded the sounds into an APK expansion file:
If you're packaging media files into a ZIP, you can use media playback calls on the files with offset and length controls (such as MediaPlayer.setDataSource() and SoundPool.load()) without the need to unpack your ZIP. In order for this to work, you must not perform additional compression on the media files when creating the ZIP packages. For example, when using the zip tool, you should use the -n option to specify the file suffixes that should not be compressed: zip -n .mp4;.ogg main_expansion media_files