0

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.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Mr.Benson
  • 169
  • 1
  • 13

2 Answers2

0

You have to create a separate class and an Arrayadapter class which will adapt the audio when clicking on a particular image or button accordingly. Do check this link to know more https://developer.android.com/guide/topics/ui/layout/recyclerview

This to know exactly what you have to do. https://www.androidhive.info/2016/01/android-working-with-recycler-view/

Shoaib Mirza
  • 153
  • 3
  • 12
0

Since you want to choose randomly without touch the code, i have a solution for it follow the pseudo code

  1. Create a Assets folder //Reason to have assets folder is you can perform file and folder only in assets folder. For eg, in you assets folder create a sub folder named random_sounds and all the random sounds you wish to play in it.
  2. In your MainActivity java code get all the files name in assets>random_sounds folder and store it in a String[] soundStrings
  3. And every OnClick of playSound Button, use random number generator to generate any number from 0 to length of the soundStrings containing the name. Using this generated number and using it as index value to get a random sound name from soundStrings .

Code to get all the file names from Assets>random_sounds

soundStrings = getAssets().list("random_sounds");

//Has to initiated in onCreate() of MainActivity


Random rand = new Random();
int rand_number_within_soundStrings_limit = rand.nextInt(soundStrings.length);
String randomSoundToPlay = soundStrings[rand_number_within_soundStrings_limit];

Link on how to play audio from assets folder.

//Use above generated randomSoundToPlay to randomly play the audio.

Harish Rn
  • 98
  • 1
  • 9