Android 安卓:按一下按钮就可以得到随机的声音效果

Android 安卓:按一下按钮就可以得到随机的声音效果,android,button,random,soundpool,Android,Button,Random,Soundpool,我正在尝试使用声音池在点击按钮时播放随机的声音效果。这在模拟器中有效,但在我的设备上不起作用。有什么线索吗 public class enc extends Activity { private static final String TAG = "MyActivity"; private SoundPool soundPool; private HashMap<Integer, Integer> soundsMap; int SOUND1=1; int SOUND2=2;

我正在尝试使用声音池在点击按钮时播放随机的声音效果。这在模拟器中有效,但在我的设备上不起作用。有什么线索吗

public class enc extends Activity {   
private static final String TAG = "MyActivity";

private SoundPool soundPool;
private HashMap<Integer, Integer> soundsMap;
int SOUND1=1;
int SOUND2=2; 
int SOUND3=3;
int SOUND4=4;
int SOUND5=5;
int SOUND6=6;
int SOUND7=7;
int SOUND8=8;
int fSpeed = 1;

Random random = new Random();
int hit = random.nextInt(6)+1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //SoundPool
    soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
    soundsMap = new HashMap<Integer, Integer>();
    soundsMap.put(SOUND1, soundPool.load(this, R.raw.hit1, 1));
    soundsMap.put(SOUND2, soundPool.load(this, R.raw.hit2, 1));
    soundsMap.put(SOUND3, soundPool.load(this, R.raw.hit3, 1));
    soundsMap.put(SOUND4, soundPool.load(this, R.raw.hit4, 1));
    soundsMap.put(SOUND5, soundPool.load(this, R.raw.hit5, 1));
    soundsMap.put(SOUND6, soundPool.load(this, R.raw.hit6, 1));
    soundsMap.put(SOUND7, soundPool.load(this, R.raw.spell, 1));
    soundsMap.put(SOUND8, soundPool.load(this, R.raw.kill, 1));


    setContentView(R.layout.enc);

}


public void setButtonClickListener() {
    Button wepb = (Button)findViewById(R.id.uwep);
    wepb.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.i(TAG, "----------------------------SFX: " + hit);
            playSound(hit, fSpeed);
            hit = random.nextInt(6)+1;
                    }
    });
}
}
公共类enc扩展活动{
私有静态最终字符串TAG=“MyActivity”;
私人声池声池;
私有HashMap soundsMap;
int SOUND1=1;
int SOUND2=2;
int SOUND3=3;
int SOUND4=4;
int SOUND5=5;
int SOUND6=6;
int SOUND7=7;
int SOUND8=8;
int fSpeed=1;
随机=新随机();
int hit=random.nextInt(6)+1;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//声场
soundPool=新的soundPool(4,AudioManager.STREAM_MUSIC,100);
soundsMap=newhashmap();
soundsMap.put(SOUND1,soundPool.load(this,R.raw.hit1,1));
soundsMap.put(SOUND2,soundPool.load(this,R.raw.hit2,1));
soundsMap.put(SOUND3,soundPool.load(this,R.raw.hit3,1));
soundsMap.put(SOUND4,soundPool.load(this,R.raw.hit4,1));
soundsMap.put(SOUND5,soundPool.load(this,R.raw.hit5,1));
soundsMap.put(SOUND6,soundPool.load(this,R.raw.hit6,1));
soundsMap.put(SOUND7,soundPool.load(this,R.raw.spell,1));
soundsMap.put(SOUND8,soundPool.load(this,R.raw.kill,1));
setContentView(R.layout.enc);
}
public void setButtonClickListener(){
按钮wepb=(按钮)findViewById(R.id.uwep);
setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
Log.i(标记“------------------------------------SFX:”+hit);
播放声音(点击、速度);
命中=随机。下一个(6)+1;
}
});
}
}

我发现了问题。我使用的是MP3文件,显然,Android 2.0+只适用于OGG文件。将它们从MP3转换成OGG修复了这个问题。一切都很好

当你说它在手机上不工作是什么意思?当我把它安装在手机上时,声音就不会播放了。如果我在没有使用随机整数的情况下从池中调用声音,它就可以正常工作。当我尝试使用mediaplayerAre时,我也遇到了同样的问题:随机数是否正确生成?你能发布日志信息吗?是的,我在按钮上的标签每次都会返回一个随机数。模拟器可以很好地播放音频。当我单击按钮时,会播放随机声音。当我在手机上试的时候,我就有问题了。R.raw.spell和R.raw.kill在调用时可以正常使用(不包括代码),并且它们不是随机的。但hit1-hit6无法播放(在手机上),将我的mp3转换为OGG解决了这个问题。谢谢如果你从来没有让我检查我手机上的日志,我就不会看到“未准备就绪”警告。谢谢