How do i get a .wav sound to play?

2019-02-14 14:11发布

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.

4条回答
在下西门庆
2楼-- · 2019-02-14 14:20

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

查看更多
Melony?
3楼-- · 2019-02-14 14:24

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.

查看更多
Animai°情兽
4楼-- · 2019-02-14 14:32

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();
   }
   ...
}
查看更多
Evening l夕情丶
5楼-- · 2019-02-14 14:47

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();
        }
    });
}

}

查看更多
登录 后发表回答