Hi,everyone!
I have a question- how to make all the buttons work, like the button with its ID "button"?
There are 10 buttons (button, button2,3,5,6,7,8,9,10,11) and 10 sounds (bark, bark 2,3,5,6,7,8,9,10,11)
activity code^
[SRC JAVA]package com.example.drums.candydrums;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends Activity implements OnTouchListener {
private SoundPool soundPool;
private int soundID;
boolean loaded = false;
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.button);
View view2 = findViewById(R.id.button2);
View view3 = findViewById(R.id.button3);
View view5 = findViewById(R.id.button5);
View view6 = findViewById(R.id.button6);
View view7 = findViewById(R.id.button7);
View view8 = findViewById(R.id.button8);
View view9 = findViewById(R.id.button9);
View view10 = findViewById(R.id.button10);
View view11 = findViewById(R.id.button11);
view.setOnTouchListener(this);
view2.setOnTouchListener(this);
view3.setOnTouchListener(this);
view5.setOnTouchListener(this);
view6.setOnTouchListener(this);
view7.setOnTouchListener(this);
view8.setOnTouchListener(this);
view9.setOnTouchListener(this);
view10.setOnTouchListener(this);
view11.setOnTouchListener(this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MU SIC);
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.bark, 1);
}
@override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
if (event.getAction() == MotionEvent.ACTION_UP) {
}
return true;
}
}[/SRC]
I have a question- how to make all the buttons work, like the button with its ID "button"?
There are 10 buttons (button, button2,3,5,6,7,8,9,10,11) and 10 sounds (bark, bark 2,3,5,6,7,8,9,10,11)
activity code^
[SRC JAVA]package com.example.drums.candydrums;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends Activity implements OnTouchListener {
private SoundPool soundPool;
private int soundID;
boolean loaded = false;
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.button);
View view2 = findViewById(R.id.button2);
View view3 = findViewById(R.id.button3);
View view5 = findViewById(R.id.button5);
View view6 = findViewById(R.id.button6);
View view7 = findViewById(R.id.button7);
View view8 = findViewById(R.id.button8);
View view9 = findViewById(R.id.button9);
View view10 = findViewById(R.id.button10);
View view11 = findViewById(R.id.button11);
view.setOnTouchListener(this);
view2.setOnTouchListener(this);
view3.setOnTouchListener(this);
view5.setOnTouchListener(this);
view6.setOnTouchListener(this);
view7.setOnTouchListener(this);
view8.setOnTouchListener(this);
view9.setOnTouchListener(this);
view10.setOnTouchListener(this);
view11.setOnTouchListener(this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MU SIC);
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.bark, 1);
}
@override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
if (event.getAction() == MotionEvent.ACTION_UP) {
}
return true;
}
}[/SRC]
No comments:
Post a Comment