Kai
Kai

Reputation: 3865

Player short audio in UI thread

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.mymp3);
    mMediaPlayer.start();
}

Or, I'd better play the audio in an AsyncTask?

Thanks.

Upvotes: 0

Views: 464

Answers (1)

Lior Ohana
Lior Ohana

Reputation: 3527

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.

Upvotes: 1

Related Questions