Android开发播放音乐

Android 播放音乐用SoundPool

public class MusicEffects {
    
    
    private SoundPool soundPool;
    public Map<String, Integer> musicMap = new HashMap<>();//定义一个MAP用于存放音乐ID
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MusicEffects(Context context){
    
    
        soundPool = new SoundPool.Builder().build();
        int musicId1 = soundPool.load(context, R.raw.button_press, 1);//载入音乐
        musicMap.put("BUTTON_PRESS",musicId1);
        int musicId2 = soundPool.load(context, R.raw.order_success, 1);
        musicMap.put("ORDER_SUCCESS",musicId2);
        int musicId3 = soundPool.load(context, R.raw.order_wrong, 1);
        musicMap.put("ORDER_WRONG",musicId3);
        int musicId4 = soundPool.load(context, R.raw.toast, 1);
        musicMap.put("TOAST",musicId4);
    }
    public void play_music(String music){
    
    
        soundPool.play(
                musicMap.get(music),
                0.9f,   //左耳道音量【0~1】
                0.9f,   //右耳道音量【0~1】
                0,     //播放优先级【0表示最低优先级】
                0,     //循环模式【0表示循环一次,-1表示一直循环,其他表示数字+1表示当前数字对应的循环次数】
                1     //播放速度【1是正常,范围从0~2】
        );
    }
}

猜你喜欢

转载自blog.csdn.net/qq_15181569/article/details/109253728