I have loaded sounds using Soundpool in Android.
I have used the play
function to play the sound.
How do I know how long a file is and where I am currently in that file as it is playing?
Sound File Duration
MediaPlayer
is probably your best bet, here:
MediaPlayer player = MediaPlayer.create(ctxt, R.raw.mysound);
int duration = player.getDuration();
player.release(); //(If not using this player later on)
Current Playback Position
SoundPool
does not support this functionality. Your three main options:
MediaPlayer
for playback. MediaPlayer
supplies a getCurrentPosition()
method.AudioTrack
-- manually sending uncompressed audio to the stream on a byte-by-byte basis. AudioTrack
has some basic functionality for determining playback position (usually accurate to within a few tens of milliseconds, on most devices), and besides that you can know exactly how much audio's been added to the playback buffer.SoundPool
, but time the playback yourself. That is, take a time hack (SystemClock.UptimeMillis()
) or set a CountDownTimer
, and estimate your playback position by assuming that playback began as soon as play()
returned. This approach is rife with problems and inaccuracies, but has the benefit of not involving AudioTrack's
complexity.