如何在自定义视图中使用 soundPool class?

How to use soundPool in a custom view class?

我正在制作一个简单的演示,它带有 自定义视图 class 扩展了想要显示随机颜色的随机圆圈的视图,这些圆圈可以随机出现在 canvas.

我已经做了随机问题,设置了一个onTouchEvent来判断Click是否在圆圈内,如果圆圈被点击则app播放短促的声音。但是在这一步,我遇到了一些问题。

我用的是SoundPool来播放音乐,但是因为不能在customview下使用(我的是MyView.java)所以我把它放回了MainActivity.java在MainActivity extends Activity下的onCreate函数下,设置后,功能似乎没问题,但我仍然无法在我的customview.java中使用soundpool.play。

如何解决?我是菜鸟,所以我还不是很清楚,你能帮我吗?

代码在这里(MainActivity.java)

package com.example.luckbag2;

import java.util.HashMap;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;

public class MainActivity extends Activity {
private SoundPool soundPool;
private HashMap<Integer,Integer> soundMap= new HashMap<Integer,Integer>();

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    MyView v = new MyView(getApplicationContext());
    setContentView(v);           
    soundPool =new SoundPool(10,AudioManager. STREAM_SYSTEM,5);
    soundMap.put(1,soundPool.load(this,R.raw.collide,0));    
}
public SoundPool getSoundPool() {
    return soundPool;
}
public void setSoundPool(SoundPool soundPool) {
    this.soundPool = soundPool;
}
public HashMap<Integer,Integer> getSoundMap() {
    return soundMap;
}
public void setSoundMap(HashMap<Integer,Integer> soundMap) {
    this.soundMap = soundMap;
}
}

Myview.java

package com.example.luckbag2;

import java.util.HashMap;
import java.util.Random;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


@SuppressWarnings("deprecation")
@SuppressLint({ "DrawAllocation", "ClickableViewAccessibility" }) class MyView extends View
{
 private Handler mHandler;

 private int mColor;

 private float cx;

 private float cy;

 private float radius;

 private float x;

 private float y;

 private int score;


public MyView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    mHandler = new Handler(getMainLooper());

    setBackgroundColor(Color.WHITE);

    Thread mThread = new Thread(new Runnable() {

        @Override
        public void run()
        {
            // TODO Auto-generated method stub
            MyView.this.invalidate();
            mHandler.postDelayed(this, 500);
        }
    });
    mThread.start();


}
private Callback getMainLooper() {
    // TODO Auto-generated method stub
    return null;
}
@Override
protected void onDraw(Canvas canvas)
{

    int w = this.getWidth();
    int h = this.getHeight();
    Log.d("CustomView", "w = " + w + ", h = " + h);

    // TODO Auto-generated method stub
    super.onDraw(canvas);
    update();
    Paint p = new Paint();
    p.setColor(mColor);
    canvas.drawCircle(cx, cy, radius, p);

}

private void update()
{
    Random random = new Random();
    cx =(float)(250+random.nextInt(580));        
    cy =(float)(250+random.nextInt(1057));        
    radius =(float)( 100 + random.nextInt(150));      

    int r = random.nextInt(256);
    int g= random.nextInt(256);
    int b = random.nextInt(256);
    mColor = Color.rgb(r, g, b);                   
}

protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
}

public boolean onTouchEvent(MotionEvent event){

    x = event.getX();
    y = event.getY();

    if ((x - cx) * (x - cx) + 
            (y - cy) * (y - cy) < radius * radius) { 
            Log.d("CustomView", "circle got clicked");
            SoundPool.play(SoundPool.get(1), 1,1,0,0,1);

    }

    return false;

}

public int getScore() {
    return score;
}
public void setScore(int score) {
    this.score = score;
}

}

要从 MyView class 调用 soundPool.playMainActivity class 上下文传递给使用 MyView 构造函数:

MainActivity mActivity;
public MyView(Context context,MainActivity mActivity) {
    super(context);
this.mActivity=mActivity;

}

MainActivity 创建 MyView class 对象为:

MyView v = new MyView(getApplicationContext(),this);

现在使用 mActivityMainActivity 访问方法:

要在 MyView 中获取 SoundPool 对象 class:

SoundPool soundPool= mActivity.getSoundPool();

使用soundPool对象调用play方法:

SoundPool soundPool= mActivity.getSoundPool();
soundPool.play(mActivity.getSoundMap().get(1), 1,1,0,0,1)