Reputation: 13
So i'm making a xylophone app for android and i've run into a problem, i'm not sure how to correctly use onTouch. Right now I can press an image and play the sound, but I cannot play two sounds at once, nor can I slide my finger down the xylophone and play all the sounds, it will only play the first image pressed. I know this is because of onTouch and Multitouch but i'm not sure how to do this and I can't find any relevant sample code, any help is appreciated!
This is my main activity
public class Xylophone extends Activity {
private Player mSoundManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xylophone);
mSoundManager = new Player();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.note01);
mSoundManager.addSound(2, R.raw.note02);
mSoundManager.addSound(3, R.raw.note03);
mSoundManager.addSound(4, R.raw.note04);
mSoundManager.addSound(5, R.raw.note05);
mSoundManager.addSound(6, R.raw.note06);
mSoundManager.addSound(7, R.raw.note07);
mSoundManager.addSound(8, R.raw.note08);
ImageView img01 = (ImageView) findViewById(R.id.imageView1);
img01.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mSoundManager.playSound(1);
return false;
}
});
ImageView img02 = (ImageView) findViewById(R.id.imageView2);
img02.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mSoundManager.playSound(2);
return false;
}
});
This is my SoundPool activity
public class Player {
private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
public Player()
{
}
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(8, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
public void playSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
public void playLoopedSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
}
}
This is my layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
android:src="@drawable/button01" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/button02" />
Upvotes: 1
Views: 699
Reputation: 2075
hopefully you figured it out until now. Still here's my shot at it: First of all your second class, SoundPool, is not an activity.
What you have to try is not overriding each button's onTouch method.
Instead, in your main activity, make it implement OnTouchListener.
Then of course, you need to implement/override the OnTouchListener's methods. So now, if you register your main view and/or every button(see for yourself which works!) to OnTouchListener, you will get all touches (multi touch as well).
Have a look at the MotionEvent class for more details
public class Xylophone extends Activity implements onTouchLister{
public static final String TAG = "Xylophone";
@Override
public void onCreate(Bundle savedInstanceState) {
/*Either have your parent view register THIS (Xylophone) as its listener, or have each button register THIS(Xylophone) as its listener.*/
yourParentView.setOnTouchListener(this);
//or
img02.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i(TAG, "ID:" + Integer.toString(v.getId()));
Log.i(TAG, "Pointer count: " + event.getPointerCount());
Log.i(TAG, "Action: " + event.getActionMasked());
Log.i(TAG, "Action index: " + event.getActionIndex());
}
Upvotes: 1