Quellcode
- public class MainActivity extends AppCompatActivity{
- private SoundPool soundpool;
- private int sound1, sound2, sound3, sound4;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) //Ab SDK 21 funktioniert die Erstellung von SoundPool anders
- {
- AudioAttributes audioAttributes = new AudioAttributes.Builder()
- .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) //Bei der App: Speech
- .setUsage(AudioAttributes.USAGE_MEDIA)
- .build();
- soundpool = new SoundPool.Builder()
- .setMaxStreams(4)
- .setAudioAttributes(audioAttributes)
- .build();
- }
- else
- {
- soundpool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
- }
- sound1 = soundpool.load(this, R.raw.a, 1);
- sound2 = soundpool.load(this, R.raw.b, 1);
- sound3 = soundpool.load(this, R.raw.c, 1);
- sound4 = soundpool.load(this, R.raw.d, 1);
- public void playSound(View v)
- {
- switch(v.getId())
- {
- case R.id.button_sound1:
- soundpool.play(sound1,1,0,0,1, 1);
- break;
- case R.id.button_sound2:
- soundpool.play(sound2,1,0,0,1, 1);
- break;
- case R.id.button_sound3:
- soundpool.play(sound3,1,0,0,1, 1);
- break;
- case R.id.button_sound4:
- soundpool.play(sound4,1,0,0,1, 1);
- break;
- }
- }
- }
- }
R.id.button_sound1 und die anderen 3 sind auch rot unterwellt, da schreibt er "constant expression required".
Kann mir da jemand weiterhelfen?