android - 如何在 SurfaceView 中使用 SoundPool?

标签 android surfaceview soundpool

我有这样的结构:

1) 主要 Activity :

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 类型中的方法 load(Context, int, int) 不适用于参数 (GameView, int, int)”。伙计们,我该如何解决?当我使用“扩展 Activity ”时它工作正常,但在 SurfaceView 中它不起作用。请帮忙。

最佳答案

更改您的代码以像这样传递上下文:

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

关于android - 如何在 SurfaceView 中使用 SoundPool?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9247916/

相关文章:

java - 如何使用 Android 动画播放 'Tick Tick' 声音(可能使用 `Android MediaPlayer/SoundPool` )?

android - 如何在各种Android设备和OS版本之间提供一致的音频排序时序?

android - 如何一个接一个地混合或附加 mp3 或 wave 文件并将生成的文件保存到 android 中的 SD 卡上?

android - 将recyclerview的焦点更改为某个项目位置?

android - Android Studio 中 ListView 中的适配器

android - SurfaceView 在 PopupWindow 中不工作

android - 如何将相机预览传递给 MediaCodec.createInputSurface() 创建的 Surface?

android - Image Cropper 无法正常工作 - 为什么?

安卓 TextView : Getting "W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1" when setting text

android - SurfaceView:ClassCastException,无法启动 Activity