Reputation: 902
What is the shortest duration of e.g. mp3 ? I want to play sound which is in mp3 and has 1 second. However I need to play this sound more frequently according to some behavior which does not matter now. Problem is that if I have one instance of MediaPlayer
with this track it cannot be played again when the last start did not finish. Any different aproach ?
Upvotes: 0
Views: 1537
Reputation: 18978
MediaPlayer is generally intended for playing longer audio streams (such as music).
Use SoundPool for playing short audio clips - it is also capable of playing multiple audio streams at once.
In addition to low-latency playback, SoundPool can also manage the number of audio streams being rendered at once. When the SoundPool object is constructed, the maxStreams parameter sets the maximum number of streams that can be played at a time from this single SoundPool. SoundPool tracks the number of active streams. If the maximum number of streams is exceeded, SoundPool will automatically stop a previously playing stream based first on priority and then by age within that priority. Limiting the maximum number of streams helps to cap CPU loading and reducing the likelihood that audio mixing will impact visuals or UI performance.
Upvotes: 2
Reputation: 894
One idea is to try using MediaPlayer.stop() at the beginning of your function which plays the sound. That way the app will stop the sound that was playing and then immediately start it again.
Upvotes: 0