Working on a testing app, I would like to play a random mp3 soundtrack when the user clicks on a button.
So far, I am able to make the soundtrack (explicitly assigned an id) plays when the user click on the button.
package com.example.audio;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
final MediaPlayer sound = MediaPlayer.create(this, R.raw.bell_sound)
Button playSound = (Button) this.findViewById(R.id.button)
playSound.setOnClickListener(new View.OnClickListener(){
@Override;
public void onClick(View v){
sound.start();
}
})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
However, I have no idea how to generate id to reference soundtracks in the raw resource file when I add more soundtrack in the future. Ideally, I can just upload more soundtrack to the raw resource file without touching the code, and the app can still randomly pick one soundtrack to play in the raw resource file when the user clicks the button.