0

当按下android屏幕上的按钮并且所有音频质量听起来都很棒时,我在eclipse中获取一些音频文件没有问题:java可以正常播放。然而,我正在尝试制作的程序是一个节拍制作工作室,你只能这么快地按下按钮。之前按下相同的按钮(大约 0.5 秒)后,您可以按下按钮来触发音频样本的时间会稍有延迟。显然,当能够在所需时间按下按钮非常重要时,这会给音乐应用程序带来问题。关于如何让我的应用程序允许更快地按下按钮的任何建议?

package studio.music;

import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class StudioActivity extends Activity implements OnClickListener{

MediaPlayer mp1,mp2;
Button button_one,button_two;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { //The activity is being Created   Create any threads/streams here
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mp1 = MediaPlayer.create(this, R.raw.bassfifteen);
    mp2 = MediaPlayer.create(this, R.raw.snarenine);
        button_one = (Button) findViewById(R.id.buttonOne);
            button_one.setOnClickListener(this);            
        button_two = (Button) findViewById(R.id.buttonTwo);
            button_two.setOnClickListener(this);        
}

@Override
protected void onStart(){ //The activity is about to become visible
    super.onStart();    
    //TODO set state for program start
}

@Override
protected void onResume(){ //The activity has become visible(it is now "resumed")
    super.onResume();
    //TODO set state for program resume
}

@Override
protected void onPause(){ // Another activity is taking focus (this activity will be "paused")
    super.onPause();
    //TODO set state for program hang
}

@Override
protected void onStop(){ // This activity is no longer visibled (it is "stopped")
    super.onStop();
    //TODO set state for program drop back
}

@Override
protected void onDestroy(){ // The Activity is about too be destoryed (exiting)  Destroy all threads and streams here
    super.onDestroy();
    //TODO set state for program exit
}

@Override
public void onClick(View v) {

//Where the audio is fired  
if(v == button_one){    mp1.start();}
if(v == button_two){    mp2.start();}
}

}

4

2 回答 2

0

使用SoundPool它提供低延迟播放。

Android Snippets提供了一个现成的示例。您可以轻松地根据自己的要求调整它。

于 2012-05-25T11:53:20.670 回答
0

也许你应该去SoundPool。因为当与 MediaPlayer 比较时,soundpool 是一个相当轻量级的对象。这是两者之间的比较。

http://www.stealthcopter.com/blog/2010/08/android-soundpool-vs-mediaplayer-focus-on-soundboards-and-memory-problems/

正如您所见,SoundPool 是为小文件设计的。这是另一个链接,其中指出 SoundPool 的工作简单而好..

https://stackoverflow.com/a/5339437/603744

于 2012-05-25T11:54:53.947 回答