How do i get a .wav sound to play?

[亡魂溺海] 提交于 2019-11-30 22:17:22

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();
   }
   ...
}
Michael G. Workman

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.

I had the same problem. this worked for me by using the application context as follows:

public class MyActivity extends Activity {
   ... 
   protected void onStart() {
      super.onStart();
      Context appContext = getApplicationContext();
      MediaPlayer mp = MediaPlayer.create(appContext , R.raw.sound_file_1);
      mp.start();
   }
   ...
}

Also, don't forget to call mp.release() once you're done

Another,preferred option is to use the SoundPool class

Try with my code, It's works perfectly. Also you need to have the sound file .wav en res/raw

public class PianoActivity extends Activity {

private MediaPlayer mediaPlayer = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_piano);
    setupUI();
}

@Override
protected void onPause() {
    super.onPause();
    if (mediaPlayer != null) {
        mediaPlayer.release();
        mediaPlayer = null;
    }
}

private void setupUI() {
    findViewById(R.id.doo).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            managerOfSound();
        }
    });
}

private void managerOfSound() {
    mediaPlayer = MediaPlayer.create(this, R.raw.doo);
    if (!mediaPlayer.isPlaying()) {
        mediaPlayer.start();
    } else {
        mediaPlayer.stop();
    }
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.reset();
            mp.release();
        }
    });
}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!