package com.itheima.soundpool;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
private SoundPool soundPool;
private int soundId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* maxStreams<br>
* the maximum number of simultaneous streams for this SoundPool object<br>
* streamType<br>
* the audio stream type as described in AudioManager For example, game
* applications will normally use STREAM_MUSIC.<br>
* srcQuality<br>
* the sample-rate converter quality. Currently has no effect. Use 0 for
* the default.
*/
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
/**
* context<br>
* the application context<br>
* resId<br>
* the resource ID<br>
* priority<br>
* the priority of the sound. Currently has no effect. Use a value of 1
* for future compatibility.
*/
soundId = soundPool.load(this, R.raw.office, 1);
}
public void click(View view) {
/**
* soundID a soundID returned by the load() function <br>
* leftVolume left volume value (range = 0.0 to 1.0) <br>
* rightVolume right volume value (range = 0.0 to 1.0) <br>
* priority stream priority (0 = lowest priority) <br>
* loop loop mode (0 = no loop, -1 = loop forever) rate playback<br>
* rate (1.0 = normal playback, range 0.5 to 2.0)
*/
int soundID = soundId;
float leftVolume = 1.0f;
float rightVolume = 1.0f;
int priority = 0;
int loop = -1;
float rate = 1.0f;
soundPool.play(soundID, leftVolume, rightVolume, priority, loop, rate);
}
}