Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get a .wav sound to play?

Im making an app, and i want it to make a sound when a activity is opened , the sound file is in R.raw.sound_file , if someone could do some example code to make my app play a sound that would be great.

like image 459
Dan Avatar asked Mar 16 '10 23:03

Dan


2 Answers

doesn't the android.media.MediaPlayer class do this?

Reference: http://developer.android.com/reference/android/media/MediaPlayer.html

Example: http://developer.android.com/guide/topics/media/index.html

Step 2 of the example says:

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();

In your case, I'd use the onStart() inside your Activity class:

public class MyActivity extends Activity {
   ... 
   protected void onStart() {
      super.onStart();
      MediaPlayer mp = MediaPlayer.create(this, R.raw.sound_file_1);
      mp.start();
   }
   ...
}
like image 182
ayman Avatar answered Sep 24 '22 21:09

ayman


I have experience using the MediaPlayer object for an android app I created, and I discovered the following:

  • Wav files have problems in MediaPlayer if they have a bitrate of 32kbps, but wav files of higher bit rates seem to play ok, even if it is a large wav file it will play ok as long as it has the higher bitrate.

  • If at all possible, use mp3 files for your audio, I encountered no problems whatsoever with mp3 audio files using the MediaPlayer object, so that is the best way to go, using google there are lots of different kinds of mp3 sounds available free from rings and dings, to dog barks, and cat meows, or whatever kind of sound you are looking for.

like image 29
Michael G. Workman Avatar answered Sep 25 '22 21:09

Michael G. Workman