Reputation: 223
I have developed a simple piano application on android OS. It works successfully but very slowly. Can anyone tell me how to make it work faster?
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;
}
}
Upvotes: 0
Views: 749
Reputation: 51411
I recommend using SoundPool instead of MediaPlayer for better performance. You can read about Soundpool and its usage here:
http://developer.android.com/reference/android/media/SoundPool.html
And some example code:
Upvotes: 1