How do I play an mp3 in the res/raw folder of my android app?

余生长醉 提交于 2019-11-26 22:32:36

When starting the activity i.e on onCreate put the following code.

  public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);         
        setContentView(R.layout.main);

        MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
        mPlayer.start();

    }

When stopping the activity i.e on onDestroy put the following code.

   public void onDestroy() {

    mPlayer.stop();
    super.onDestroy();

}

Hope it helps :)

You'll likely prefer to use the SoundPool class. It reduces latency when it's time to play the sound, and offers other niceties like being able to prioritise sounds when there are too many to play at once.

From the docs:

A SoundPool is a collection of samples that can be loaded into memory from a resource inside the APK or from a file in the file system. The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream. This allows applications to ship with compressed streams without having to suffer the CPU load and latency of decompressing during playback.

For example:

/**
 * How many sounds can be played at once.
 */
private static final int MAX_SOUND_POOL_STREAMS = 4;

/**
 * Modify this as part of your own priority scheme. Higher numbers mean higher
 * priority. If you don't care, it's okay to use the same priority for every
 * sound.
 */
private static final int NORMAL_PRIORITY = 10;

private int mySoundId;

@Override
public void setupContent() {
    this.soundPool = new SoundPool(MAX_SOUND_POOL_STREAMS,
            AudioManager.STREAM_MUSIC, 100);
    this.mySoundId = this.soundPool.load(this.getApplicationContext(),
            R.raw.mySound, 1);
}

@Override
private void playMySound() {
    this.soundPool.play(this.mySoundId, 1, 1, NORMAL_PRIORITY, 0, 1);
}

this is a static method I use in my projects. I add it to my Utils class:

    public static void playSound(final Context context, final SoundType type)
    {

            new Thread(new Runnable()
            {

                @Override
                public void run()
                {
                    MediaPlayer mediaPlayer = new MediaPlayer();
                    int resId = -1;
                    switch (type)
                    {
                    case INCOMING_NOTIFICATION:
                        resId=R.raw.noti_sound;
                        break;
                    case SEND_BETTING_SLIP:
                        resId=R.raw.slip_sent;
                        break;
                    case TRIVIA_RIGHT_ANSWER:
                        resId=R.raw.game_bonus;
                        break;
                    case TRIVIA_WRONG_ANSWER:
                        resId=R.raw.whistle_referee_trivia_bad_answer;
                        break;
                    }

                    if (resId != -1)
                    {
                        mediaPlayer = MediaPlayer.create(context, resId);
                        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mediaPlayer.setLooping(false);
                        mediaPlayer.start();

                        while (mediaPlayer.isPlaying() == true)
                        {
                        }
                    }
                }
            }).start();

        }
}

now I defind an Enum (SoundType) and placed the mp3 files in raw folder under res folder.

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