我在Android OS上开发了一个简单的钢琴应用程序。它运作成功但非常缓慢。谁能告诉我如何让它更快地工作?
public class MainActivity extends Activity implements OnClickListener{
@Override
public void onClick(View v) {
if(v.equals(b1)){
sound = R.raw.piano_f;
}else if(v.equals(b2)){
sound = R.raw.piano_e;
}
new MyTask(MainActivity.this,sound).execute();
}
}
class MyTask extends AsyncTask<Void, Void, Void>{
int sound;
Context context;
public MediaPlayer media;
MyTask(Context context , int sound){
this.sound = sound;
this.context = context;}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
media = MediaPlayer.create(context,sound );
media.start();
return null;
}
}
答案 0 :(得分:1)
我建议使用SoundPool而不是MediaPlayer以获得更好的性能。您可以在这里阅读Soundpool及其用法:
http://developer.android.com/reference/android/media/SoundPool.html
一些示例代码: