Send the uri to play sound through an array

1

Good I am practicing with the typical application where creating an object is filled with parameters through a array and then those data are sent to another screen, and I would like to send the uri ( R.raw . "the file ").

I have listaAnimales.add(Animal(R.drawable.perro, "Perro", "El perro guia de ovejas","R.raw.gallo"))

and I would receive it in this way val sonido = bundle.getString("sonido")

but in the function val mediaPlayer = MediaPlayer.create(this,).start() I do not detect the variable sound

I would like to be able to click a button and start playing the sound sent

    
asked by Daniel Montil 24.05.2018 в 18:02
source

1 answer

0

The array must be defined as an array of integer elements since you access system resources, in this case for example R.raw.gallo is an audio that must be found in the folder /raw

I see you use Kotlin, this is an example:

 val audio = R.raw.gallo;
 val mediaPlayer = MediaPlayer.create(this, audio).start()

I add more complete examples:

Play audio stored in /raw to another activity to play through MediaPlayer .

From your Activity you realize the Intent sending the value of your audio:

 val intent = Intent(this, SecondActivity::class.java)
 val message = R.raw.gallo;

 intent.putExtra("audio", message)
 startActivity(intent)

In the Activity destination you receive the value and reproduce the audio:

  val intent = getIntent();
  val audio = intent.getIntExtra("audio",0)

  val mediaPlayer = MediaPlayer.create(this, audio).start()

Play audios stored in /raw to another activity to play through MediaPlayer .

You make an array of integers containing the ids of the audios and the sendings in Intent :

val intent = Intent(this, SecondActivity::class.java)
val message: IntArray = intArrayOf(R.raw.perro, R.raw.gato, R.raw.gallo, R.raw.crocodile);

intent.putExtra("audios", message)
startActivity(intent)

in the Activity destination you receive the array of elements, and through the index you can indicate which element in the array to play:

val intent = getIntent();
val audios:IntArray = intent.getIntArrayExtra("audios")

val mediaPlayer = MediaPlayer.create(this, audios[2]).start()
    
answered by 24.05.2018 / 18:59
source