SoundPool - 声音文件中的什么位置
SoundPool - What position in a sound file
我在 Android 中使用 Soundpool 加载了声音。
我已经使用了play
功能来播放声音
我如何知道文件的长度以及播放时我当前在该文件中的位置?
声音文件持续时间
MediaPlayer
可能是您最好的选择,这里:
MediaPlayer player = MediaPlayer.create(ctxt, R.raw.mysound);
int duration = player.getDuration();
player.release(); //(If not using this player later on)
当前播放位置
SoundPool
不支持此功能。您的三个主要选择:
- 使用
MediaPlayer
进行播放。 MediaPlayer
提供了一个 getCurrentPosition()
方法。
- 使用
AudioTrack
-- 逐字节手动将未压缩的音频发送到流中。 AudioTrack
有一些确定播放位置的基本功能(在大多数设备上通常精确到几十毫秒),除此之外,您还可以确切地知道有多少音频被添加到播放缓冲区。
- 坚持使用
SoundPool
,但要自己安排播放时间。也就是说,花点时间 hack (SystemClock.UptimeMillis()
) or set a CountDownTimer
,并通过假设 play()
返回后立即开始播放来估计您的播放位置。这种方法充满了问题和不准确,但好处是不涉及AudioTrack's
复杂度。
我在 Android 中使用 Soundpool 加载了声音。
我已经使用了play
功能来播放声音
我如何知道文件的长度以及播放时我当前在该文件中的位置?
声音文件持续时间
MediaPlayer
可能是您最好的选择,这里:
MediaPlayer player = MediaPlayer.create(ctxt, R.raw.mysound);
int duration = player.getDuration();
player.release(); //(If not using this player later on)
当前播放位置
SoundPool
不支持此功能。您的三个主要选择:
- 使用
MediaPlayer
进行播放。MediaPlayer
提供了一个getCurrentPosition()
方法。 - 使用
AudioTrack
-- 逐字节手动将未压缩的音频发送到流中。AudioTrack
有一些确定播放位置的基本功能(在大多数设备上通常精确到几十毫秒),除此之外,您还可以确切地知道有多少音频被添加到播放缓冲区。 - 坚持使用
SoundPool
,但要自己安排播放时间。也就是说,花点时间 hack (SystemClock.UptimeMillis()
) or set aCountDownTimer
,并通过假设play()
返回后立即开始播放来估计您的播放位置。这种方法充满了问题和不准确,但好处是不涉及AudioTrack's
复杂度。