SoundPool构造方法被弃用后的解决方案

个人原创,转载请注明出处:https://www.jianshu.com/p/f0218baa853d

在API 21后SoundPool的构造方法被弃用了,解决方法如下:

SoundPool sp = new SoundPool.Builder().setMaxStreams(y)
                                         ...
                                      .build();

不过该方法需要API >= 21;要兼容小于21的版本,可以这样写:

SoundPool sp ;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    sp= new SoundPool.Builder()
            .setMaxStreams(10)
            .build();
} else {
    sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
}

加个版本判断就ok了,gradle的警告不用管!

参考

https://stackoverflow.com/questions/39184157/android-why-is-the-constructor-for-soundpool-deprecated

你可能感兴趣的:(SoundPool构造方法被弃用后的解决方案)