2012-10-24 48 views
1

我正在为学校创建一个采样器应用程序。我编写了一些代码,当我按下按钮时播放一个样本,当我释放它时停止播放。我的问题是它有太多的延迟。按下按钮播放声音后需要很长时间。按下按钮后播放声音的延迟太长

我的音频文件是mp3的。

这里是我的代码:

smpl1.setOnTouchListener(new View.OnTouchListener() 
{ 
    public boolean onTouch(View arg0, MotionEvent theMotion) 
    { 
    switch (theMotion.getAction()) 
    { 
     case MotionEvent.ACTION_DOWN: 
     sample = MediaPlayer.create(MainActivity.this, R.raw.bassdrum); 
     smpl1.setText("ON"); 
     smpl1.setTextColor(Color.GREEN); 
     sample.start(); 
     break; 
     case MotionEvent.ACTION_UP: 
     smpl1.setText("OFF"); 
     smpl1.setTextColor(Color.RED); 
     sample.stop(); 
     break; 
    } 
    return true;   
    } 

回答

2

创建按钮按下前的sample对象,那么就使用开始/在你的处理器的代码停止功能。

+0

您可能还需要“准备”它 – njzk2