我如何在SurfaceView中使用SoundPool?

时间:2012-02-12 09:59:29

标签: android surfaceview soundpool

我有这个结构:

1)主要活动:

public class mainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {   

    super.onCreate(savedInstanceState);      
    setContentView(new GameView(this));

}

2)游戏视图

SoundPool sp;
int mySound = 0;    

public class GameView extends SurfaceView implements SurfaceHolder.Callback {


public GameView(Context context) {
    super(context);

    sp = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
    mySound = sp.load(this, R.raw.mysound, 1);      

}

在线“mySound = sp.load(this,R.raw.mysound,1);”它给了我错误 - “SoundPool类型中的方法加载(Context,int,int)不适用于参数(GameView,int,int)”。伙计们,我该怎么办呢?当我使用“扩展活动”时它可以正常工作,但在SurfaceView中它不起作用。请帮助。

1 个答案:

答案 0 :(得分:3)

更改代码以传递上下文:

mySound = sp.load(context, R.raw.mysound, 1);