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

时间:2015-02-26 05:37:56

标签: java android android-activity android-custom-view soundpool

我正在制作一个简单的演示,其中自定义视图类扩展了想要显示随机颜色的随机圆圈的视图,该随机圆圈可以随机出现在画布上。

我已经做了随机问题并设置了一个 onTouchEvent 来判断Click是否在圈子中,如果点击了圈子,那么app会播放一个简短的声音。但在这一步,我遇到了一些问题。

我使用 SoundPool 来播放音乐,但因为它不能在customview下使用(我的是MyView.java)所以我把它放回onCreate函数下的MainActivity.java在MainActivity下扩展Activity,在设置之后,函数似乎没问题,但我仍然无法在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;
}

}

1 个答案:

答案 0 :(得分:0)

soundPool.play类传递MyView类上下文调用MainActivity到使用MyView构造函数:

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

}

MainActivity创建MyView类对象:

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

现在使用mActivity来访问MainActivity的方法:

要在SoundPool课程中获取MyView个对象:

SoundPool soundPool= mActivity.getSoundPool();

使用soundPool对象来调用play方法:

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