播放音频

1、MediaPlayer 

(1)private MediaPlayer mediaPlayer; 

(2)mediaPlayer = MediaPlayer.create(this,R.raw.money_sound);

(3)mediaPlayer.start();

2、SoundPool

(1)private SoundPool soundPool;

(2)soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);soundPoolMap = new HashMap();

            soundPoolMap.put(1, soundPool.load(this, R.raw.money_sound, 1));

(3)playSound(1, 0);

(4)

public void playSound(int sound, int loop) {

AudioManager mgr = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);

float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);

float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

float volume = streamVolumeCurrent/streamVolumeMax;

//参数:1、Map中取值  2、当前音量    3、最大音量  4、优先级  5、重播次数  6、播放速度

soundPool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f);

}

注:R.raw.money_sound为res文件夹下的raw文件中的money_sound.wav音频文件

你可能感兴趣的:(播放音频)