0
我是Android開發人員的新手,所以我正在努力研究是否有更高效或更快速的方式來使用onTouch方法切換MediaPlayer數據源。我正在嘗試創建一種可以像長笛演奏的樂器,但是當我按下(觸摸)按鈕時,音源不會足夠快地切換。有更快的方式來切換MediaPlayer的數據源嗎?
我正在使用playNote()
方法在音頻文件之間切換。任何建議表示讚賞。
public class PlayAggeion extends Activity {
ImageButton patC1;
int soundIsOn = 1;
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_aggeion);
onConfigurationChanged(null);
addListenerPatima();
mp = new MediaPlayer();
playNote(R.raw.aa);
}
public void addListenerPatima() {
patC1 = (ImageButton) findViewById(R.id.patC1);
patC1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
playNote(R.raw.bb);
return true;
case MotionEvent.ACTION_UP:
playNote(R.raw.aa);
return true;
}
return false;
};
});
}
public void playNote(int note){
// Play note
try {
mp.reset();
mp.setDataSource(getApplicationContext(), Uri.parse("android.resource://" + getPackageName() + "/" + note));
mp.prepare();
mp.setLooping(true);
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}