title: How do I play an audio file in Android?
body: <p>I have code to play an <code>.ogg</code> audio file, that I downloaded from the internet. I have no errors, so I can run it, but then the app crashes:</p>
  
  <pre><code>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;
  }
  }
  </code></pre>
  
  <p>I think I have two problems:</p>
  
  <ol>
  <li><p>I put this in the <code>main.xml</code> file:
  <pre>
  &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  android:orientation=&quot;vertical&quot; android:layout_width=&quot;fill_parent&quot;
  android:layout_height=&quot;fill_parent&quot;&gt;
  &lt;TextView android:text=&quot;Click on the screen to start playing&quot;
      android:id=&quot;@+id/textView1&quot; android:layout_width=&quot;fill_parent&quot;
      android:layout_height=&quot;fill_parent&quot;&gt;&lt;/TextView&gt;
  &lt;/LinearLayout&gt;
  </pre>And I don't know if it's correct.</p></li>
  <li><p>I put the file <code>sound1.ogg</code> in the <code>workspace-&gt;SoundPlay2</code> folder because in the res folder I had problems, and also, I tried to put it in the two res folders that exist.</p></li>
  </ol>
  
  <p>This is from my console:</p>
  
  <pre><code>[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
  </code></pre>
  
  <p>I took this example from "<a href="http://www.vogella.de/articles/AndroidMedia/article.html">Android Sounds - Tutorial</a>".
  I want to play an audio file, more specifically, a <code>.wav</code> file.</p>
  
  <p><strong>I dont' know where I can find some information about the files that are permitted in the MediaPlayer class and their characteristic (durantion, sample rate...) Could you tell me where I can find this??</strong></p>