Java SoundPool加载缓慢,MotionEvent很奇怪
你好强> 我对编程相当陌生,在SoundPool方面遇到了一些问题。 我正在尝试构建一个简单的钢琴应用程序,但还没有找到解决方案。我使用的音频文件是midi(.mid) 我的代码当前存在的问题:Java SoundPool加载缓慢,MotionEvent很奇怪,java,android,multi-touch,soundpool,motionevent,Java,Android,Multi Touch,Soundpool,Motionevent,你好 我对编程相当陌生,在SoundPool方面遇到了一些问题。 我正在尝试构建一个简单的钢琴应用程序,但还没有找到解决方案。我使用的音频文件是midi(.mid) 我的代码当前存在的问题: public class MainActivityPiano extends AppCompatActivity { //all soundIDs public static final int SOUND_1 = 1; public static final int SOUND
public class MainActivityPiano extends AppCompatActivity {
//all soundIDs
public static final int SOUND_1 = 1;
public static final int SOUND_2 = 2;
SoundPool mSoundPool;
HashMap<Integer, Integer> mSoundMap;
static int streamID;
@Override
protected void onStart() {
super.onStart();
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
mSoundMap = new HashMap<Integer, Integer>();
if(mSoundPool != null){
mSoundMap.put(SOUND_1, mSoundPool.load(this, R.raw.piano_tone1, 1));
mSoundMap.put(SOUND_2, mSoundPool.load(this, R.raw.piano_tone2, 1));
//at least twelve different sound files have to be loaded...
}
//play buttons and/or piano keys
Button pianoKey1 = this.findViewById(R.id.piano_key1);
Button pianoKey2 = this.findViewById(R.id.piano_key2);
//...
//play buttons and/or pianokeys
pianoKey1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
return playSound(SOUND_1, motionEvent, view);
}
});
pianoKey2.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
return playSound(SOUND_2, motionEvent, view);
}
});
//...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_piano);
}
public boolean playSound(int sound, MotionEvent m, View v) {
switch (m.getAction()) {
case MotionEvent.ACTION_DOWN:
streamID = mSoundPool.play(sound, 1, 1, 1, 0, 1.0f);
break;
case MotionEvent.ACTION_CANCEL:;
mSoundPool.stop(streamID);
break;
case MotionEvent.ACTION_UP:
mSoundPool.stop(streamID);
break;
case MotionEvent.ACTION_MOVE:
}
return true;
}
@Override
protected void onPause() {
mSoundPool.autoPause();
super.onPause();
}
}
- 音频文件加载非常慢,我必须等待几秒钟才能准备好声音
- 如果我只使用一个手指,效果很好,但是当我添加另一个手指时,第一个手指/声音就会卡住
public class MainActivityPiano extends AppCompatActivity {
//all soundIDs
public static final int SOUND_1 = 1;
public static final int SOUND_2 = 2;
SoundPool mSoundPool;
HashMap<Integer, Integer> mSoundMap;
static int streamID;
@Override
protected void onStart() {
super.onStart();
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
mSoundMap = new HashMap<Integer, Integer>();
if(mSoundPool != null){
mSoundMap.put(SOUND_1, mSoundPool.load(this, R.raw.piano_tone1, 1));
mSoundMap.put(SOUND_2, mSoundPool.load(this, R.raw.piano_tone2, 1));
//at least twelve different sound files have to be loaded...
}
//play buttons and/or piano keys
Button pianoKey1 = this.findViewById(R.id.piano_key1);
Button pianoKey2 = this.findViewById(R.id.piano_key2);
//...
//play buttons and/or pianokeys
pianoKey1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
return playSound(SOUND_1, motionEvent, view);
}
});
pianoKey2.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
return playSound(SOUND_2, motionEvent, view);
}
});
//...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_piano);
}
public boolean playSound(int sound, MotionEvent m, View v) {
switch (m.getAction()) {
case MotionEvent.ACTION_DOWN:
streamID = mSoundPool.play(sound, 1, 1, 1, 0, 1.0f);
break;
case MotionEvent.ACTION_CANCEL:;
mSoundPool.stop(streamID);
break;
case MotionEvent.ACTION_UP:
mSoundPool.stop(streamID);
break;
case MotionEvent.ACTION_MOVE:
}
return true;
}
@Override
protected void onPause() {
mSoundPool.autoPause();
super.onPause();
}
}
公共类MainActivityAno扩展了AppCompatActivity{
//所有声音
公共静态最终int SOUND_1=1;
公共静态最终int声音_2=2;
声池mSoundPool;
HashMap-mSoundMap;
静态整流形;
@凌驾
受保护的void onStart(){
super.onStart();
mSoundPool=newsoundpool(4,AudioManager.STREAM_MUSIC,100);
mSoundMap=newhashmap();
if(mSoundPool!=null){
mSoundMap.put(SOUND_1,mSoundPool.load(this,R.raw.piano_tone1,1));
mSoundMap.put(SOUND_2,mSoundPool.load(this,R.raw.piano_tone2,1));
//至少要加载12个不同的声音文件。。。
}
//播放按钮和/或钢琴键
Button pianoKey1=this.findViewById(R.id.piano\u key1);
Button pianoKey2=this.findViewById(R.id.piano\u键2);
//...
//播放按钮和/或钢琴
pianoKey1.setOnTouchListener(新视图.OnTouchListener(){
公共布尔onTouch(视图、运动事件、运动事件){
返回播放声音(声音1、运动事件、视图);
}
});
pianoKey2.setOnTouchListener(新视图.OnTouchListener(){
公共布尔onTouch(视图、运动事件、运动事件){
返回播放声音(声音2、运动事件、视图);
}
});
//...
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_piano);
}
公共布尔播放声音(int声音、MotionEvent m、视图v){
开关(m.getAction()){
case MotionEvent.ACTION\u DOWN:
streamID=mSoundPool.play(声音,1,1,1,0,1.0f);
打破
case MotionEvent.ACTION_CANCEL:;
mSoundPool.stop(streamID);
打破
case MotionEvent.ACTION\u UP:
mSoundPool.stop(streamID);
打破
case MotionEvent.ACTION\u移动:
}
返回true;
}
@凌驾
受保护的void onPause(){
mSoundPool.autopose();
super.onPause();
}
}
您可能希望预先加载声音,然后通过引用它们的ID播放它们
public class Sound {
private static SoundPool soundPool;
public short[] getSound(Activity activity, String...fileNames) {
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
short[] ids = new short[fileNames.length];
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
try {
AssetManager assetManager = activity.getAssets();
for(int i = 0; i < fileNames.length; i++) {
AssetFileDescriptor descriptor = assetManager.openFd(fileNames[i]);
ids[i] = (short) soundPool.load(descriptor, 1);
}
} catch(IOException e) {
//
}
return ids;
}
public void playSound(short id, float rV, float lV) {
if(id != -1) {
soundPool.play(id, rV, lV, 0, 0, 1);
}
}
public void pauseSound() {
soundPool.autoPause();
}
public void releaseSound() {
soundPool.release();
}
}
short[]
数组将包含传递给getSound()
创建一个单独的类Sound.java
:
在您的主要活动中,您可以创建一个Sound
对象,并在onCreate()
中加载声音一次,然后使用它们的ID播放它们
public class Sound {
private static SoundPool soundPool;
public short[] getSound(Activity activity, String...fileNames) {
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
short[] ids = new short[fileNames.length];
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
try {
AssetManager assetManager = activity.getAssets();
for(int i = 0; i < fileNames.length; i++) {
AssetFileDescriptor descriptor = assetManager.openFd(fileNames[i]);
ids[i] = (short) soundPool.load(descriptor, 1);
}
} catch(IOException e) {
//
}
return ids;
}
public void playSound(short id, float rV, float lV) {
if(id != -1) {
soundPool.play(id, rV, lV, 0, 0, 1);
}
}
public void pauseSound() {
soundPool.autoPause();
}
public void releaseSound() {
soundPool.release();
}
}
公共类声音{
专用静态声池声池;
public short[]getSound(活动、字符串…文件名){
soundPool=新的soundPool(10,AudioManager.STREAM_MUSIC,0);
short[]ids=新的short[filename.length];
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
试一试{
AssetManager AssetManager=activity.getAssets();
对于(int i=0;i
太棒了!谢谢,但是我如何处理多点触控而不被声音卡住呢?