Android 使用soundpool在两个按钮上同时播放两种声音
当两个按钮同时触碰在一起时,我试图同时播放两种声音。我有按钮1和按钮2,当按钮1是触摸时,按钮2不会播放声音。但当我把手指从按钮1上移开时,触摸按钮2就会发出声音 我试着在谷歌上搜索,但找不到答案。 我想做的很清楚。当两个按钮接触在一起时,同时播放两种声音 下面是到目前为止我的代码Android 使用soundpool在两个按钮上同时播放两种声音,android,multi-touch,soundpool,Android,Multi Touch,Soundpool,当两个按钮同时触碰在一起时,我试图同时播放两种声音。我有按钮1和按钮2,当按钮1是触摸时,按钮2不会播放声音。但当我把手指从按钮1上移开时,触摸按钮2就会发出声音 我试着在谷歌上搜索,但找不到答案。 我想做的很清楚。当两个按钮接触在一起时,同时播放两种声音 下面是到目前为止我的代码 public class MultitouchtestActivity extends MultiTouchActivity { /** Called when the activity is first creat
public class MultitouchtestActivity extends MultiTouchActivity {
/** Called when the activity is first created. */
private Button btn1;
private Button btn2;
SoundPool soundPool;
AudioManager audioManager;
int soundId;
int kID, sID;
@Override
public void onCreate(Bundle savedInstanceState)
{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
super.onCreate(savedInstanceState);
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
//the maximum number of simultaneous streams for this SoundPool object
int maxStreams = 4;
//the audio stream type as described in AudioManager
int streamType = AudioManager.STREAM_MUSIC;
//the sample-rate converter quality. Currently has no effect. Use 0 for the default.
int srcQuality = 0;
soundPool = new SoundPool(maxStreams, streamType, srcQuality);
soundPool.setOnLoadCompleteListener(soundPoolOnLoadCompleteListener);
soundId = soundPool.load(this, R.raw.c, 1);
kID = soundPool.load(this, R.raw.k, 1);
sID = soundPool.load(this, R.raw.s, 1);
btn1.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
float vol = audioManager.getStreamVolume(
AudioManager.STREAM_MUSIC);
float maxVol = audioManager.getStreamMaxVolume(
AudioManager.STREAM_MUSIC);
float leftVolume = vol/maxVol;
float rightVolume = vol/maxVol;
int priority = 1;
int no_loop = 0;
float normal_playback_rate = 1f;
soundPool.play(kID,
leftVolume,
rightVolume,
priority,
no_loop,
normal_playback_rate);
return true;
}
return false;
}
});
btn2.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
float vol = audioManager.getStreamVolume(
AudioManager.STREAM_MUSIC);
float maxVol = audioManager.getStreamMaxVolume(
AudioManager.STREAM_MUSIC);
float leftVolume = vol/maxVol;
float rightVolume = vol/maxVol;
int priority = 1;
int no_loop = 0;
float normal_playback_rate = 1f;
soundPool.play(sID,
leftVolume,
rightVolume,
priority,
no_loop,
normal_playback_rate);
return true;
}
return false;
}
});
}
SoundPool.OnLoadCompleteListener soundPoolOnLoadCompleteListener = new SoundPool.OnLoadCompleteListener()
{
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status)
{
if(status==0)
{
btn1.setEnabled(true);
btn2.setEnabled(true);
}
else
{
Toast.makeText(MultitouchtestActivity.this,"SoundPool.load() fail", Toast.LENGTH_LONG).show();
}
}
};
}
公共类多点触控活动将活动实现扩展到TouchListener上{
私有视图父级;
创建公共void(捆绑包实例)
{
super.onCreate(实例);
parent=findviewbyd(android.R.id.content).getRootView();
parent.setOnTouchListener(this);
}
公共布尔onTouch(视图v,运动事件){
for(int-ptrIndex=0;ptrIndex4){
actionResolved=actionResolved-5;
}
if(actionResolved==MotionEvent.ACTION\u MOVE){
dealEvent(ptrIndex、event、v、actionResolved);
Log.v(“标记”、“移动”+ptrIndex);
}否则{
dealEvent(actionPointerIndex,event,v,actionResolved);
}
}
返回false;
}
私有void dealEvent(int actionPointerIndex、MotionEvent事件、,
查看事件视图,int actionresolved){
int-rawX,rawY;
int位置[]={0,0};
eventView.getLocationOnScreen(位置);
//Log.v(“标签”,位置+”);
rawX=(int)event.getX(actionPointerIndex)+位置[0];
rawY=(int)event.getY(actionPointerIndex)+位置[1];
ArrayList视图=GetTouchedView(rawX、rawY、actionresolved);
//dumpEvent(事件);
用于(视图:视图){
int x,y;
view.GetLocationOn屏幕(位置);
x=rawX-位置[0];
y=rawY-位置[1];
//Log.v(“tag”,“toucted”+view.toString());
/*
*view.onTouchEvent(MotionEvent.Get(event.getDownTime()),
*event.getEventTime(),event.getActionMasked(),x,y,
*event.getMetaState());
*/
//MotionEvent me=MotionEvent.get(事件);
MotionEvent me=MotionEvent.Get(event.getDownTime(),
event.getEventTime(),actionresolved,x,y,
事件.getPressure(actionPointerIndex),
事件.getPressure(actionPointerIndex),
event.getMetaState(),event.getXPrecision(),
event.getYPrecision(),event.getDeviceId(),
event.getEdgeFlags());
me.setLocation(x,y);
//Log.v(“tag”,“oldeventid:+event.getAction()+”#”+
//actionPointerIndex+“id”+ptrId+“已解决”+actionResolved);
//Log.v(“tag”,“neweventid:+me.getAction()+”#”+
//actionPointerIndex+“id”+ptrId+“已解决”+actionResolved);
如果(!me.equals(事件)){
/*
*Log.v(“标记”,“操作索引:”+actionPointerIndex+
*已解决:“+actionPointerIndex+”到“+view.toString()
*+“y:”+view.getTop()+“-”+(view.getTop())+
*view.getHeight());
*/
//me.setAction(actionresolved);
视图。onTouchEvent(我);
}
if(actionresolved==MotionEvent.ACTION\u MOVE){
Log.v(“标签”,
“#”+actionPointerIndex+”Rawx:“+Rawx+”rawy:”
+rawY+“x:+x+”y:+y+“
+view.toString());
}
}
}
私有ArrayList GetTouchedView(int x,int y,int action){
int-moveGap=0;
if(action==MotionEvent.action\u MOVE){
moveGap=0;
}
ArrayList touchedViews=新建ArrayList();
ArrayList possibleViews=新的ArrayList();
if(视图组的父实例){
可能的视图添加(父级);
对于(int i=0;i=y)
&(view.getWidth()+位置[0]+moveGap>=x)
&(view.getLeft()-moveGap请帮助任何评论都非常感谢请帮助任何评论都非常感谢请帮助任何评论都非常感谢请帮助任何评论都非常感谢请帮助任何评论都非常感谢
public class MultiTouchActivity extends Activity implements OnTouchListener {
private View parent;
public void onCreate(Bundle instance)
{
super.onCreate(instance);
parent = findViewById(android.R.id.content).getRootView();
parent.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
for (int ptrIndex = 0; ptrIndex < event.getPointerCount(); ptrIndex++) {
// index of the pointer which starts this Event
int actionPointerIndex = event.getActionIndex();
// resolve the action as a basic type (up, down or move)
int actionResolved = event.getAction() & MotionEvent.ACTION_MASK;
if (actionResolved < 7 && actionResolved > 4) {
actionResolved = actionResolved - 5;
}
if (actionResolved == MotionEvent.ACTION_MOVE) {
dealEvent(ptrIndex, event, v, actionResolved);
Log.v("tag", "move" + ptrIndex);
} else {
dealEvent(actionPointerIndex, event, v, actionResolved);
}
}
return false;
}
private void dealEvent(int actionPointerIndex, MotionEvent event,
View eventView, int actionresolved) {
int rawX, rawY;
int location[] = { 0, 0 };
eventView.getLocationOnScreen(location);
// Log.v("tag", location + "");
rawX = (int) event.getX(actionPointerIndex) + location[0];
rawY = (int) event.getY(actionPointerIndex) + location[1];
ArrayList<View> views = getTouchedViews(rawX, rawY, actionresolved);
// dumpEvent(event);
for (View view : views) {
int x, y;
view.getLocationOnScreen(location);
x = rawX - location[0];
y = rawY - location[1];
// Log.v("tag", "touched" + view.toString());
/*
* view.onTouchEvent(MotionEvent.obtain(event.getDownTime(),
* event.getEventTime(), event.getActionMasked(), x, y,
* event.getMetaState()));
*/
// MotionEvent me = MotionEvent.obtain(event);
MotionEvent me = MotionEvent.obtain(event.getDownTime(),
event.getEventTime(), actionresolved, x, y,
event.getPressure(actionPointerIndex),
event.getPressure(actionPointerIndex),
event.getMetaState(), event.getXPrecision(),
event.getYPrecision(), event.getDeviceId(),
event.getEdgeFlags());
me.setLocation(x, y);
// Log.v("tag", "oldeventid: " + event.getAction() + " #" +
// actionPointerIndex+ " id" + ptrId+ " resolved "+ actionResolved);
// Log.v("tag", "neweventid: " + me.getAction() + " #" +
// actionPointerIndex+ " id" + ptrId+ " resolved "+ actionResolved);
if (!me.equals(event)) {
/*
* Log.v("tag", "actionindex: " + actionPointerIndex +
* " resolved: " + actionPointerIndex + " to " + view.toString()
* + " y:" + view.getTop() + "-" + (view.getTop() +
* view.getHeight()));
*/
// me.setAction(actionresolved);
view.onTouchEvent(me);
}
if (actionresolved == MotionEvent.ACTION_MOVE) {
Log.v("tag",
"#" + actionPointerIndex + "Rawx:" + rawX + " rawy:"
+ rawY + "x:" + x + " y:" + y + " "
+ view.toString());
}
}
}
private ArrayList<View> getTouchedViews(int x, int y, int action) {
int moveGap = 0;
if (action == MotionEvent.ACTION_MOVE) {
moveGap = 0;
}
ArrayList<View> touchedViews = new ArrayList<View>();
ArrayList<View> possibleViews = new ArrayList<View>();
if (parent instanceof ViewGroup) {
possibleViews.add(parent);
for (int i = 0; i < possibleViews.size(); i++) {
View view = possibleViews.get(i);
int location[] = { 0, 0 };
view.getLocationOnScreen(location);
if (((view.getHeight() + location[1] + moveGap >= y)
& (view.getWidth() + location[0] + moveGap >= x)
& (view.getLeft() - moveGap <= x) & (view.getTop()
- moveGap <= y))
|| view instanceof FrameLayout) {
touchedViews.add(view);
possibleViews.addAll(getChildViews(view));
}
}
}
return touchedViews;
}
private ArrayList<View> getChildViews(View view) {
ArrayList<View> views = new ArrayList<View>();
if (view instanceof ViewGroup) {
ViewGroup v = ((ViewGroup) view);
if (v.getChildCount() > 0) {
for (int i = 0; i < v.getChildCount(); i++) {
views.add(v.getChildAt(i));
}
}
}
return views;
}