Sonidos en Android
Sonidos con SoundPool y Mediaplayer en Android
SoundPool (OBSOLETO. Para API 21 o superiores es recomendable SoundPool.Builder)
SoundPool es una librería nativa de Android que permite reproducir audio desde un archivo. A continuación un ejemplo de reproducción de un sonido.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="//schemas.android.com/apk/res/android"
xmlns:app="//schemas.android.com/apk/res-auto"
xmlns:tools="//schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xip.midominio.com.sonidos.MainActivity">
<TextView
android:id="@+id/texto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package xip.midominio.com.sonidos;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends AppCompatActivity implements OnTouchListener{
private SoundPool soundPool;
private int soundID;
boolean loaded;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.texto);
view.setOnTouchListener(this);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(10,AudioManager.STREAM_MUSIC,0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int i, int i1) {
loaded = true;
}
});
soundID = soundPool.load(this,R.raw.sound37,1);
}
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_DOWN){
AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
float volumenActual = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float volumenMax = (float) audioManager.getStreamMaxVolume(audioManager.STREAM_MUSIC);
float volumen = volumenActual / volumenMax;
if(loaded){
soundPool.play(soundID,volumen,volumen,1,0,1f);
Log.e("test","reproduce sonido");
}
}
return false;
}
}
MediaPlayer
Esta librería permite reproducir sonido y vídeo.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/texto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
<Button
android:id="@+id/boton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Play"/>
<Button
android:id="@+id/boton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop"/>
</LinearLayout>
MainActivity.java
package xip.midominio.com.mediplayer;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity implements OnClickListener{
Button boton,boton2;
TextView texto;
public MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
texto = (TextView) findViewById(R.id.texto);
boton = (Button) findViewById(R.id.boton);
boton2 = (Button) findViewById(R.id.boton2);
boton.setOnClickListener(this);
boton2.setOnClickListener(this);
}
public void onClick(View view){
switch(view.getId()){
case R.id.boton:
play_mp();
break;
case R.id.boton2:
stop_mp();
break;
default:
break;
}
}
public void play_mp(){
mp = MediaPlayer.create(this,R.raw.sound37);
mp.start();
}
public void stop_mp(){
mp.stop();
}
}
Para poder comentar es necesario iniciar sesión