REPRODUCIR EFECTOS DE SONIDO En el siguiente ejemplo utilizaremos cuatro ficheros de audio con efectos de sonido, con los formatos wav y mp3. Copiar estos ficheros en la carpeta res/raw de nuestro proyecto. Hay que tener en cuenta que los nombres solo deben estar formados con caracteres como a- z, 0-9 y _. En esta actividad definimos cuatro botones para reproducir los cuatro ficheros de sonido. package com.example.sonidos; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Sonidos extends Activity implements OnClickListener { MediaPlayer mplayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sonidos); Button boton1=(Button)findViewById(R.id.boton1); boton1.setOnClickListener(this); Button boton2=(Button)findViewById(R.id.boton2); boton2.setOnClickListener(this); Button boton3=(Button)findViewById(R.id.boton3); boton3.setOnClickListener(this); Button boton4=(Button)findViewById(R.id.boton4); boton4.setOnClickListener(this); } @Override public void onClick(View v) { //Libera el MediaPlayer if(mplayer != null) mplayer.release(); int id=v.getId(); if(id == R.id.boton1) mplayer = MediaPlayer.create(this, R.raw.aplauso); else if(id == R.id.boton2) mplayer = MediaPlayer.create(this, R.raw.escalera); else if(id == R.id.boton3) mplayer = MediaPlayer.create(this, R.raw.gente); else mplayer = MediaPlayer.create(this, R.raw.risa); mplayer.seekTo(0); mplayer.start(); } public void onPause(){ super.onPause(); //Libera el MediaPlayer en el background if(mplayer != null) mplayer.release(); } } El layout correspondiente a esta aplicacin contiene cuatro botones definidos en el siguiente fichero xml de la forma usual. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#ffffff" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:textSize="20sp" android:textColor="#000000" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/titulo" /> <Button android:text="@string/aplauso" android:id="@+id/boton1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:text="@string/escalera" android:id="@+id/boton2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:text="@string/gente" android:id="@+id/boton3" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:text="@string/risa" android:id="@+id/boton4" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>