Android - 使用SoundManager从OnTouch播放随机声音

时间:2013-03-11 17:24:24

标签: android random ontouchlistener

我想要在用户触摸屏幕时播放随机声音。我找到并使用了这些其他帖子来帮助设置我的项目并让我朝着正确的方向前进。

Play random sound file on button press - Android

Issue With Sound Manager Playing Random Files

Issue With Sound Manager Playing Random Files

Play A Random Sound onTouch

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

在主课堂

int x = r.nextInt(1); 

并在

的SoundManager课程中
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);

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

MAIN CLASS

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);
    }

}

2 个答案:

答案 0 :(得分:1)

问题是

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

此行始终生成0作为随机数,0HashMap中不存在HashMap密钥,然后从if(mSoundPoolMap.containsKey(index)){ mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); } else{ // Generate next number } 获取值:

{{1}}

答案 1 :(得分: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);
    ...
}