package e5;

import B4.A;
import B4.I;
import android.media.SoundPool;
import e4.AbstractC0565i;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import r4.AbstractC0983h;

/* JADX INFO: loaded from: classes.dex */
public final class l implements h {

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final n f6286k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final io.sentry.internal.debugmeta.c f6287l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final G4.c f6288m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public Integer f6289n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public Integer f6290o;
    public d5.a p;
    public m q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public f5.d f6291r;

    public l(n nVar, io.sentry.internal.debugmeta.c cVar) {
        AbstractC0983h.e("wrappedPlayer", nVar);
        AbstractC0983h.e("soundPoolManager", cVar);
        this.f6286k = nVar;
        this.f6287l = cVar;
        I4.e eVar = I.f205a;
        this.f6288m = A.a(G4.n.f859a);
        d5.a aVar = nVar.f6297c;
        this.p = aVar;
        cVar.k(aVar);
        d5.a aVar2 = this.p;
        AbstractC0983h.e("audioContext", aVar2);
        m mVar = (m) ((HashMap) cVar.f7827m).get(aVar2.a());
        if (mVar != null) {
            this.q = mVar;
        } else {
            throw new IllegalStateException(("Could not create SoundPool " + this.p).toString());
        }
    }

    @Override // e5.h
    public final void B(float f6, float f7) {
        Integer num = this.f6290o;
        if (num != null) {
            this.q.f6292a.setVolume(num.intValue(), f6, f7);
        }
    }

    @Override // e5.h
    public final /* bridge */ /* synthetic */ Integer D() {
        return null;
    }

    @Override // e5.h
    public final void a() {
        Integer num = this.f6290o;
        Integer num2 = this.f6289n;
        if (num != null) {
            this.q.f6292a.resume(num.intValue());
            return;
        }
        if (num2 != null) {
            SoundPool soundPool = this.q.f6292a;
            int iIntValue = num2.intValue();
            n nVar = this.f6286k;
            float f6 = nVar.f6301g;
            this.f6290o = Integer.valueOf(soundPool.play(iIntValue, f6, f6, 0, nVar.f6303j == d5.h.f6164l ? -1 : 0, nVar.i));
        }
    }

    @Override // e5.h
    public final void b(boolean z5) {
        Integer num = this.f6290o;
        if (num != null) {
            this.q.f6292a.setLoop(num.intValue(), z5 ? -1 : 0);
        }
    }

    public final void c(f5.d dVar) throws Throwable {
        l lVar;
        f5.d dVar2;
        Throwable th;
        if (dVar != null) {
            synchronized (this.q.f6294c) {
                try {
                    Map map = this.q.f6294c;
                    Object arrayList = map.get(dVar);
                    if (arrayList == null) {
                        try {
                            arrayList = new ArrayList();
                            map.put(dVar, arrayList);
                        } catch (Throwable th2) {
                            th = th2;
                            throw th;
                        }
                    }
                    List list = (List) arrayList;
                    l lVar2 = (l) AbstractC0565i.g0(list);
                    if (lVar2 != null) {
                        boolean z5 = lVar2.f6286k.f6306m;
                        this.f6286k.h(z5);
                        this.f6289n = lVar2.f6289n;
                        this.f6286k.c("Reusing soundId " + this.f6289n + " for " + dVar + " is prepared=" + z5 + ' ' + this);
                        lVar = this;
                        dVar2 = dVar;
                    } else {
                        long jCurrentTimeMillis = System.currentTimeMillis();
                        this.f6286k.h(false);
                        this.f6286k.c("Fetching actual URL for " + dVar);
                        G4.c cVar = this.f6288m;
                        I4.e eVar = I.f205a;
                        lVar = this;
                        dVar2 = dVar;
                        try {
                            A.h(cVar, I4.d.f1002m, new k(dVar2, lVar, this, jCurrentTimeMillis, null), 2);
                        } catch (Throwable th3) {
                            th = th3;
                            th = th;
                            throw th;
                        }
                    }
                    list.add(this);
                } catch (Throwable th4) {
                    th = th4;
                }
            }
        } else {
            lVar = this;
            dVar2 = dVar;
        }
        lVar.f6291r = dVar2;
    }

    @Override // e5.h
    public final void g(d5.a aVar) {
        if (!this.p.a().equals(aVar.a())) {
            release();
            io.sentry.internal.debugmeta.c cVar = this.f6287l;
            cVar.k(aVar);
            m mVar = (m) ((HashMap) cVar.f7827m).get(aVar.a());
            if (mVar == null) {
                throw new IllegalStateException(("Could not create SoundPool " + aVar).toString());
            }
            this.q = mVar;
        }
        this.p = aVar;
    }

    @Override // e5.h
    public final void pause() {
        Integer num = this.f6290o;
        if (num != null) {
            this.q.f6292a.pause(num.intValue());
        }
    }

    @Override // e5.h
    public final void release() {
        stop();
        Integer num = this.f6289n;
        if (num != null) {
            int iIntValue = num.intValue();
            f5.d dVar = this.f6291r;
            if (dVar == null) {
                return;
            }
            synchronized (this.q.f6294c) {
                try {
                    List list = (List) this.q.f6294c.get(dVar);
                    if (list == null) {
                        return;
                    }
                    if ((list.size() == 1 ? list.get(0) : null) == this) {
                        this.q.f6294c.remove(dVar);
                        this.q.f6292a.unload(iIntValue);
                        this.q.f6293b.remove(num);
                        this.f6286k.c("unloaded soundId " + iIntValue);
                    } else {
                        list.remove(this);
                    }
                    this.f6289n = null;
                    c(null);
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    @Override // e5.h
    public final void stop() {
        Integer num = this.f6290o;
        if (num != null) {
            this.q.f6292a.stop(num.intValue());
            this.f6290o = null;
        }
    }

    @Override // e5.h
    public final /* bridge */ /* synthetic */ Integer t() {
        return null;
    }

    @Override // e5.h
    public final boolean u() {
        return false;
    }

    @Override // e5.h
    public final void v(float f6) {
        Integer num = this.f6290o;
        if (num != null) {
            this.q.f6292a.setRate(num.intValue(), f6);
        }
    }

    @Override // e5.h
    public final void w(int i) {
        if (i != 0) {
            throw new UnsupportedOperationException("LOW_LATENCY mode does not support: seek");
        }
        Integer num = this.f6290o;
        if (num != null) {
            int iIntValue = num.intValue();
            stop();
            if (this.f6286k.f6307n) {
                this.q.f6292a.resume(iIntValue);
            }
        }
    }

    @Override // e5.h
    public final void z(f5.c cVar) {
        AbstractC0983h.e("source", cVar);
        cVar.a(this);
    }

    @Override // e5.h
    public final void E() {
    }

    @Override // e5.h
    public final void n() {
    }
}
