У меня есть Soundpool, который воспроизводит короткий звуковой эффект после запуска анимации просмотра. Проблема в том, что он воспроизводит этот звук только в первый раз, а не несколько раз подряд.
Я проверил возвращаемый результат функции sound.play, и это всегда идентификатор потока, а не ноль. Он создает потоки, когда это необходимо, но звук воспроизводится только в первый раз. Звук загружается только один раз, поэтому проблем с загрузкой быть не должно, так как он воспроизводится в первый раз. Я буду вставлять код ниже:
Я вызываю createSoundPool() для создания звукового пула.
private void createSoundPool() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
createNewSoundPool();
} else {
createOldSoundPool();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void createNewSoundPool(){
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
sounds = new SoundPool.Builder().setMaxStreams(5)
.setAudioAttributes(attributes)
.build();
}
@SuppressWarnings("deprecation")
private void createOldSoundPool(){
sounds = new SoundPool(5, AudioManager.STREAM_MUSIC,0);
}
Вот как я его использую
public MyTouchListener(final Context context, final RecyclerView recyclerView,
OnTouchActionListener onTouchActionListener){
mOnTouchActionListener = onTouchActionListener;
initAnim();
createSoundPool();
drawCardSound = sounds.load(context,R.raw.drawcard,1); //this is where the soundID is created. Only once the class is instantiated
mGestureDetector = new GestureDetectorCompat(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
// Find the item view that was swiped based on the coordinates
final View child = recyclerView.findChildViewUnder(e1.getX(), e1.getY());
final int childPosition = recyclerView.getChildAdapterPosition(child);
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (mOnTouchActionListener != null && child != null) {
if(!animation.hasStarted() || animation.hasEnded()) {
child.startAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
int res=sounds.play(drawCardSound,3,3,1,0,1f);// sound is being played here
Log.e("test", "onAnimationStart: "+res );
}
LogCat показывает увеличивающийся идентификатор потока, а не ноль, поэтому я думаю, что он должен работать правильно.
ОБНОВЛЕНИЕ: эта проблема возникает только на Lollipop и выше. Я тестировал ее на эмуляторах ниже Lollipop, и звук работает нормально. Есть ли какая-то ошибка на Lollipop и выше? Я тестировал его на Samsung S7.