【发布时间】:2015-12-07 12:09:29
【问题描述】:
我对 java 和 android 很陌生,我想在按下按钮后播放一些声音。我搜索了很多并尝试了这个:
//package and imports
public class Simulador extends Activity {
int contador;
Button somar;
SoundPool som;
boolean loaded;
int comeu, comprou;
protected void onCreate(Bundle primeiroBundle) {
super.onCreate(primeiroBundle);
setContentView(R.layout.activity_primeira_atividade);
contador = 0;
somar = (Button) findViewById(R.id.som);
som = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
//I removed the whole part from the other button,
//since it's basically the same. So that's why I need to set maxStreams to 2.
comeu = som.load(this, R.raw.comeu, 1);
som.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
public void onLoadComplete(SoundPool som, int sampleId, int status){
loaded = true;
}
});
somar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (loaded) {
som.play(comprou, 1, 1, 1, 0, 1f);
}
contador += 5;
}
}
});
}
protected void onPause() {
super.onPause();
som.release();
som = null;
}
}
我的代码正常运行,但收到以下警告:
SoundPool(int, int, int)' is deprecated
所以我来here 看看如何解决并得到了这个:
此构造函数在 API 级别 21 中已弃用。请改用 SoundPool.Builder 来创建和配置 SoundPool 实例
所以我去了here,但无法将我的代码添加到这个新的构造函数,甚至阅读参考。有谁知道如何解决这个问题?
【问题讨论】:
-
你的另一个链接here