Java 听起来对棒棒糖有效,但对kitkat或旧版本无效
我在onCreate中使用了一种方法,叫做loadSounds,根据版本的不同,我使用SoundPool Builder,如果是旧版本的Android,则使用不推荐的版本……但是声音只在棒棒糖设备上工作……可能有什么问题……应用程序在两个版本中运行……保存声音池的变量在onCreate之外声明为对象属性私有静态声音池我的声音 提前感谢Java 听起来对棒棒糖有效,但对kitkat或旧版本无效,java,android,soundpool,Java,Android,Soundpool,我在onCreate中使用了一种方法,叫做loadSounds,根据版本的不同,我使用SoundPool Builder,如果是旧版本的Android,则使用不推荐的版本……但是声音只在棒棒糖设备上工作……可能有什么问题……应用程序在两个版本中运行……保存声音池的变量在onCreate之外声明为对象属性私有静态声音池我的声音 提前感谢 private void loadSounds(){ if(Build.VERSION.SDK_INT>Build.VERSION_COD
private void loadSounds(){
if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN).setUsage(AudioAttributes.USAGE_GAME).build();
mySounds = new SoundPool.Builder().
setMaxStreams(10).
setAudioAttributes(audioAttributes).
build();
correctAnswerID = mySounds.load(this, R.raw.correctanswer, 1);
incorrectAnswerID = mySounds.load(this, R.raw.incorrectanswer, 1);
}
else
{
mySounds= new SoundPool(10, AudioManager.USE_DEFAULT_STREAM_TYPE,1);
correctAnswerID = mySounds.load(this, R.raw.correctanswer, 1);
incorrectAnswerID = mySounds.load(this, R.raw.incorrectanswer, 1);
}
}
使用声音的方法是(如果答案正确,则播放correctanswer mp3,否则播放错误答案mp3):
重现声音的方法如下:
private void reproduceAnswerSound(String type){
if(type.equals("Correct")){
mySounds.play(correctAnswerID,1,1,1,0,1);
}else if(type.equals("Incorrect"))
{
mySounds.play(incorrectAnswerID,1,1,1,0,1);
}
}
顺便说一句,也许你应该把
Build.VERSION.SDK\u INT>Build.VERSION\u CODES.LOLLIPOP
改成Build.VERSION.SDK\u INT>=Build.VERSION\u CODES.LOLLIPOP
@AntonMalyshev它不起作用了,我的朋友。声音只在棒棒糖中播放,而不是在4.2.2和4.4.4中播放…还有什么问题吗?已经解决了…问题出在宣布声音池采用旧方式的线路上,在其他线路上,我替换了该线路,第一个参数为1而不是10,第二个参数为流音乐而不是默认流----------------->mySounds=new SoundPool(1,AudioManager.stream_music,0);
private void reproduceAnswerSound(String type){
if(type.equals("Correct")){
mySounds.play(correctAnswerID,1,1,1,0,1);
}else if(type.equals("Incorrect"))
{
mySounds.play(incorrectAnswerID,1,1,1,0,1);
}
}