¿Cómo reproduzco un mp3 en la carpeta res / raw de mi aplicación android?

Tengo un mp3 pequeño (200kb) en la carpeta res / raw de mi aplicación android. Estoy tratando de ejecutarlo en un emulador de Eclipse. Se reconoce como un recurso en el archivo R, pero cuando intento preparar / iniciar, mi actividad se bloquea! ¿Había algo más que necesitaba cambiar, tal vez en el manifiesto?

 MediaPlayer mPlayer = MediaPlayer.create (FakeCallScreen.this, R.raw.mysoundfile);

 tratar {
 mPlayer.prepare ();
 mPlayer.start ();
 } catch (IOException e) {
 // manejar esto más tarde
 }

Al iniciar la actividad es decir onCreate poner el siguiente código.

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile); mPlayer.start(); } 

Al detener la actividad es decir en onDestroy poner el siguiente código.

  public void onDestroy() { mPlayer.stop(); super.onDestroy(); } 

Espero eso ayude 🙂

Es probable que prefiera utilizar la clase SoundPool . Reduce la latencia cuando es el momento de reproducir el sonido, y ofrece otras sutilezas como ser capaz de priorizar los sonidos cuando hay demasiados para jugar a la vez.

De los documentos:

Un SoundPool es una colección de muestras que se pueden cargar en la memoria de un recurso dentro del APK o de un archivo en el sistema de archivos. La biblioteca SoundPool utiliza el servicio MediaPlayer para decodificar el audio en un flujo mono o estéreo PCM de 16 bits sin procesar. Esto permite que las aplicaciones se envíen con flujos comprimidos sin tener que sufrir la carga de la CPU y la latencia de la descompresión durante la reproducción.

Por ejemplo:

 /** * How many sounds can be played at once. */ private static final int MAX_SOUND_POOL_STREAMS = 4; /** * Modify this as part of your own priority scheme. Higher numbers mean higher * priority. If you don't care, it's okay to use the same priority for every * sound. */ private static final int NORMAL_PRIORITY = 10; private int mySoundId; @Override public void setupContent() { this.soundPool = new SoundPool(MAX_SOUND_POOL_STREAMS, AudioManager.STREAM_MUSIC, 100); this.mySoundId = this.soundPool.load(this.getApplicationContext(), R.raw.mySound, 1); } @Override private void playMySound() { this.soundPool.play(this.mySoundId, 1, 1, NORMAL_PRIORITY, 0, 1); } 

este es un método estático que uso en mis proyectos. Lo agrego a mi clase Utils:

  public static void playSound(final Context context, final SoundType type) { new Thread(new Runnable() { @Override public void run() { MediaPlayer mediaPlayer = new MediaPlayer(); int resId = -1; switch (type) { case INCOMING_NOTIFICATION: resId=R.raw.noti_sound; break; case SEND_BETTING_SLIP: resId=R.raw.slip_sent; break; case TRIVIA_RIGHT_ANSWER: resId=R.raw.game_bonus; break; case TRIVIA_WRONG_ANSWER: resId=R.raw.whistle_referee_trivia_bad_answer; break; } if (resId != -1) { mediaPlayer = MediaPlayer.create(context, resId); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setLooping(false); mediaPlayer.start(); while (mediaPlayer.isPlaying() == true) { } } } }).start(); } } 

ahora defino un Enum (SoundType) y coloqué los archivos mp3 en la carpeta raw en la carpeta res.

  • Enviar acción multimedia en android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.