Android声音池SoundPool

来源:互联网 发布:男生喜欢的内衣知乎 编辑:程序博客网 时间:2024/05/22 19:01
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="射击"        android:onClick="shoot" /></RelativeLayout>
public class MainActivity extends Activity {    private SoundPool pool;    private int soundID;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        int maxStreams = 10;// 池子中最多有多少声音        int streamType = AudioManager.STREAM_MUSIC;        int srcQuality = 0;        pool = new SoundPool(maxStreams, streamType, srcQuality);        // 加载声音        soundID = pool.load(this, R.raw.shoot, 1);    }    public void shoot(View view) {        float leftVolume = 1f;// 左声道        float rightVolume = 1f;        int priority = 0;        int loop = 0;        float rate = 1f;        pool.play(soundID, leftVolume, rightVolume, priority, loop, rate);    }}

参考:Android音频、视频