0

每当用户触摸屏幕时,我都希望播放随机声音。我已经找到并使用了这些其他帖子来帮助设置我的项目并让我朝着正确的方向前进。

按下按钮时播放随机声音文件 - Android

声音管理器播放随机文件的问题

声音管理器播放随机文件的问题

播放随机声音 onTouch

问题是,使用我当前的代码,我得到了 NullPointerException。LogCat 显示它来自两个位置:

在主要课程中

int x = r.nextInt(1); 

在 SoundManager 类中

mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);

我知道这只是我忽略的东西,但似乎无法弄清楚。对 Android 和 Java 开发来说相当新,所以这就是原因。我已经包含了 Main 和 SoundManager 类。任何帮助,将不胜感激!

主要课程

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;


public class Main extends Activity implements OnTouchListener
{
    Random r = new Random();
    int x = r.nextInt(1);

    private SoundManager mSoundManager;

    @Override public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView image = (ImageView) findViewById(R.id.mIll);
        image.setOnTouchListener(this);

        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(1, R.raw.m1);
        mSoundManager.addSound(2, R.raw.m2);
        mSoundManager.addSound(3, R.raw.m3);
        mSoundManager.addSound(4, R.raw.m4);
        mSoundManager.addSound(5, R.raw.m5);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

    switch (event.getAction()) 
    {
        case MotionEvent.ACTION_DOWN:
        {
            mSoundManager.playSound(x);
        }

        break;

    }

    return true;

    }

}

声音管理器

import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {

    private SoundPool mSoundPool;
    private HashMap<Integer, Integer> mSoundPoolMap;
    private AudioManager mAudioManager;
    private Context mContext;
    public static final int maxSounds = 1;

    public SoundManager() {

    }

    public void initSounds(Context theContext) {
        mContext = theContext;
        mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
        mSoundPoolMap = new HashMap<Integer, Integer>();
        mAudioManager = (AudioManager) mContext
                .getSystemService(Context.AUDIO_SERVICE);
    }

    public void addSound(int Index, int SoundID) {
        mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));

    }

    public void playSound(int index) {

        int streamVolume = mAudioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume,
                1, 0, 1f);
    }

}
4

2 回答 2

1

问题是

int x = r.nextInt(1);  //<<<<

此行始终生成 0 作为随机数,并且在.check null0中不存在密钥,然后从as 获取值:HashMapHashMap

if(mSoundPoolMap.containsKey(index)){
 mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume,
                1, 0, 1f);
}
else{
 // Generate  next number

}
于 2013-03-11T17:35:51.987 回答
0

将初始化移动到 onCreate() 部分

  Random r ;
  int x ;

  @Override public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    r= new Random();
    x= r.nextInt(1);
    ...
}
于 2013-03-11T17:30:50.497 回答