<LinearLayout 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:orientation="vertical">
<Button
android:id="@+id/btnPlay"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:text="Play Wav"
tools:ignore="HardcodedText" />
</LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnPlay;
private SoundPool soundPool;//声明一个SoundPool
private int soundID;//创建某个声音对应的音频ID
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btnPlay = (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(this);
initSound();
}
@SuppressLint("NewApi")
private void initSound() {
soundPool = new SoundPool.Builder().build();
soundID = soundPool.load(this, R.raw.testsong, 1);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnPlay:
playSound();
break;
}
}
private void playSound() {
soundPool.play(
soundID,
0.1f, //左耳道音量【0~1】
0.5f, //右耳道音量【0~1】
0, //播放优先级【0表示最低优先级】
1, //循环模式【0表示循环一次,-1表示一直循环,其他表示数字+1表示当前数字对应的循环次数】
1 //播放速度【1是正常,范围从0~2】
);
}
}
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有