开发者

Player short audio in UI thread

开发者 https://www.devze.com 2023-04-01 20:32 出处:网络
Is it a good idea to play a short audio within an UI thread? The code is like: public void onCreate(Bundle savedInstanceState) {

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消