Is it a good idea to play a short audio within an UI thread? The code is like:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.mym开发者_高级运维p3);
mMediaPlayer.start();
}
Or, I'd better play the audio in an AsyncTask
?
Thanks.
As a thumb of rule running that on the main (that's what it actually means in Android) thread is not a good idea. There are several reasons, for example, what will happen if tomorrow it will not be a short audio sample? how will the application behave if exactly on the same time an incoming call will arrive...it can take 2-3 seconds (depends on the length of the audio sample) until the user will be able to answer. The "good practice" for audio playing is using services.
精彩评论