¿Cómo reproduzco un archivo de audio en Android?

Tengo código para reproducir un archivo de audio .ogg , que he descargado desde Internet. No tengo errores, así que puedo ejecutarlo, pero luego la aplicación se bloquea:

 package play.my.sound; import android.app.Activity; import android.media.AudioManager; import android.media.SoundPool; import android.media.SoundPool.OnLoadCompleteListener; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; public class PlaySound2Activity extends Activity { private SoundPool soundPool; private int soundID; boolean loaded = false; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View view = findViewById(R.id.textView1); view.setOnClickListener((OnClickListener) this); // Set the hardware buttons to control the music this.setVolumeControlStream(AudioManager.STREAM_MUSIC); // Load the sound soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { loaded = true; } }); soundID = soundPool.load("sound1.ogg", 1); } public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { // Getting the user sound settings AudioManager audioManager = (AudioManager) getSystemService (AUDIO_SERVICE); float actualVolume = (float) audioManager .getStreamVolume(AudioManager.STREAM_MUSIC); float maxVolume = (float) audioManager .getStreamMaxVolume(AudioManager.STREAM_MUSIC); float volume = actualVolume / maxVolume; // Is the sound loaded already? if (loaded) { soundPool.play(soundID, volume, volume, 1, 0, 1f); Log.e("Test", "Played sound"); } } return false; } } 

Creo que tengo dos problemas:

  1. main.xml esto en el archivo main.xml :

     <? Xml version = "1.0" encoding = "utf-8"?>
     <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
     Android: orientación = "vertical" android: layout_width = "fill_parent"
     Androide: layout_height = "fill_parent">
     <TextView android: text = "Haga clic en la pantalla para comenzar a jugar"
         Android: id = "@ + id / textView1" android: layout_width = "fill_parent"
         Android: layout_height = "fill_parent"> </ TextView>
     </ LinearLayout>
    

    Y no sé si es correcto.

  2. Puse el archivo sound1.ogg en el workspace->SoundPlay2 carpeta de workspace->SoundPlay2 porque en la carpeta res tenía problemas, y también, intenté ponerlo en las dos carpetas de res que existen.

Esto es de mi consola:

 [2012-01-04 19:38:16 - PlaySound2] Failed to install PlaySound2.apk on device 'emulator-5554': timeout [2012-01-04 19:38:16 - PlaySound2] Launch canceled! [2012-01-04 19:47:33 - PlaySound2] Error in an XML file: aborting build. [2012-01-04 19:52:34 - PlaySound2] res\layout\main.xml:0: error: Resource entry main is already defined. [2012-01-04 19:52:34 - PlaySound2] res\layout\main.out.xml:0: Originally defined here. [2012-01-04 19:52:34 - PlaySound2] C:\Users\Natalia\workspace\PlaySound2\res\layout\main.out.xml:1: error: Error parsing XML: no element found 

Tomé este ejemplo de " Android Sounds – Tutorial ". Quiero reproducir un archivo de audio, más específicamente, un archivo .wav .

No sé dónde puedo encontrar alguna información sobre los archivos que están permitidos en la clase MediaPlayer y sus características (durantion, tasa de muestreo …) ¿Podría decirme dónde puedo encontrar esto?

Cree la carpeta en bruto en la carpeta res y agregue su archivo a ella. A continuación, reproduzca el archivo utilizando

 soundID = soundPool.load(R.raw.sound1, 1); 

Y también vea esta publicación para su problema main.xml.

Ejemplo para tocar algún zumbador. En la carpeta res en la carpeta raw tengo un buzzer.wav

Método para reproducir este zumbador:

  /** * Method to play an alarm sound to signal half time or full time. */ private void playAlarm() { MediaPlayer mp = MediaPlayer.create(this, R.raw.buzzer); mp.start(); mp.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mp.release(); } }); } 

Nunca he visto la clase SoundPool antes, sin embargo recomendaría usar una clase MediaPlayer:

 mMediaPlayer = new MediaPlayer(this, R.raw.yourfile); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.prepare(); mMediaPlayer.start(); 

Asegúrese de colocar el archivo en la carpeta PROJECT / res / raw / (o crearlo si no existe)

También hay algo mal con su main.xml. ¿Puede por favor publicarlo?

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.