如何在指定时间播放声音? (在我的应用中模拟录制)

时间:2013-04-19 11:25:33

标签: android soundpool

我想在我的钢琴应用中实现录音。为此,我节省了播放每个声音的时间以及在LinkedList中播放的时间。要用这些时间和声音播放录制的音乐(声音序列),我们应该在指定的时间播放每个声音。这是怎么回事? 这是我的一个声音代码的一部分(注释):

public class MainActivity extends Activity {

double StartTime;
double millis;
sound_pool snd;
LinkedList<list> rec_list = new LinkedList<list>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Create an instance of our sound manger
    snd = new sound_pool(getApplicationContext());
    // Set volume rocker mode to media volume
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

    Button btn = (Button) findViewById(R.id.button1);       
    Date dt=new Date();
    long mil=dt.getTime();

    btn.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN ) 

               {
                snd.play_s_r_1();                   
                Date dt=new Date();
                long mil=dt.getTime();                  
                rec_list.add(new list(mil,"s_r_1"));        
               }
             return true;                   
        }
    });  // end of ontouch listener 

} //end of oncreate
} //end of activity    

1 个答案:

答案 0 :(得分:0)

只需尝试以下代码即可 public CountDownTimer(long millisInFuture,long countDownInterval) millisInFuture - 从调用start()到倒计时完成并调用onFinish()的未来millis数。因此onFinish()将在1秒后被调用(1000毫秒= 1秒)。

   player = MediaPlayer.create(getApplicationContext(), R.raw.beepsound);
 player.start();

CountDownTimer timer = new CountDownTimer(1000, 1000) {

@Override
public void onTick(long millisUntilFinished) {
   // Nothing to do
}

@Override
public void onFinish() {
    if (player.isPlaying()) {
         player.stop();
         player.release();
    }
}
};
timer.start(); 

参考:http://developer.android.com/reference/android/os/CountDownTimer.html#CountDownTimer(long,长)