package com.tencent.mm.modelvoice;

import android.content.Context;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.compatible.b.e;
import com.tencent.mm.compatible.e.q;
import com.tencent.mm.compatible.util.b;
import com.tencent.mm.modelvoice.d.a;
import com.tencent.mm.plugin.appbrand.appstorage.ZipJNI;
import com.tencent.mm.plugin.appbrand.jsapi.wifi.d;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.ytcommon.BuildConfig;
import java.io.InputStream;

public final class j implements d {
    AudioTrack aQf;
    private int cIb;
    b cop;
    boolean fZC;
    OnCompletionListener fZE;
    OnErrorListener fZF;
    a fZI;
    d.b fZJ;
    private int fZK;
    private int fZL;
    private Runnable fZM;
    com.tencent.qqpinyin.voicerecoapi.a fZN;
    private int fZO;
    int fZP;
    boolean fZQ;
    byte[] fZR;
    int fZS;
    InputStream fZT;
    int fZU;
    String fZt;
    String fZu;
    String fileName;
    private int nSamplerate;
    volatile int status;

    public final int getStatus() {
        return this.status;
    }

    public j() {
        int i = 0;
        AppMethodBeat.i(55819);
        this.fileName = BuildConfig.FLAVOR;
        this.fZI = null;
        this.fZJ = null;
        this.status = 0;
        this.fZK = 2;
        this.nSamplerate = 16000;
        this.cIb = 1;
        this.fZL = 20;
        this.fZN = null;
        this.fZP = 0;
        this.fZQ = false;
        this.fZt = BuildConfig.FLAVOR;
        this.fZu = BuildConfig.FLAVOR;
        this.fZC = true;
        this.fZR = new byte[com.tencent.qqpinyin.voicerecoapi.a.MAX_FRAME_SIZE];
        this.fZS = 0;
        this.fZT = null;
        this.fZU = d.CTRL_INDEX;
        this.fZE = new OnCompletionListener() {
            public final void onCompletion(MediaPlayer mediaPlayer) {
                AppMethodBeat.i(55816);
                if (j.this.cop != null && j.this.fZC) {
                    j.this.cop.Mm();
                }
                try {
                    j.this.status = 0;
                    j.this.alX();
                    try {
                        j.a(j.this);
                        AppMethodBeat.o(55816);
                    } catch (InterruptedException e) {
                        ab.e("MicroMsg.SpeexPlayer", "exception:%s", new Object[]{bo.l(e)});
                        AppMethodBeat.o(55816);
                    }
                } catch (Exception e2) {
                    ab.e("MicroMsg.SpeexPlayer", "exception:%s", new Object[]{bo.l(e2)});
                    ab.e("MicroMsg.SpeexPlayer", "setCompletion File[" + j.this.fileName + "] ErrMsg[" + e2.getStackTrace() + "]");
                    AppMethodBeat.o(55816);
                }
            }
        };
        this.fZF = new 2(this);
        this.fZN = new com.tencent.qqpinyin.voicerecoapi.a();
        com.tencent.qqpinyin.voicerecoapi.a aVar = this.fZN;
        if (aVar.AqB != 0) {
            i = ZipJNI.UNZ_BADZIPFILE;
        } else {
            int nativeTRSpeexDecodeInit = aVar.AqC.nativeTRSpeexDecodeInit();
            if (nativeTRSpeexDecodeInit == -1 || nativeTRSpeexDecodeInit == -100 || nativeTRSpeexDecodeInit == -101 || nativeTRSpeexDecodeInit == ZipJNI.UNZ_PARAMERROR) {
                i = nativeTRSpeexDecodeInit;
            } else {
                aVar.AqB = nativeTRSpeexDecodeInit;
                aVar.Aqz = new byte[(com.tencent.qqpinyin.voicerecoapi.a.MAX_FRAME_SIZE * 15)];
            }
        }
        if (i != 0) {
            ab.e("speex", "res: ".concat(String.valueOf(i)));
        }
        AppMethodBeat.o(55819);
    }

    public j(Context context) {
        this();
        AppMethodBeat.i(55820);
        this.cop = new b(context);
        AppMethodBeat.o(55820);
    }

    public final void a(a aVar) {
        this.fZI = aVar;
    }

    public final void a(d.b bVar) {
        this.fZJ = bVar;
    }

    /* Access modifiers changed, original: final */
    public final void alX() {
        AppMethodBeat.i(55821);
        if (this.aQf != null) {
            this.aQf.stop();
            this.aQf.release();
            this.aQf = null;
        }
        AppMethodBeat.o(55821);
    }

    public final void bo(boolean z) {
        int i;
        AppMethodBeat.i(55822);
        this.fZQ = true;
        if (this.cIb == 2) {
            this.fZK = 3;
        } else {
            this.fZK = 2;
        }
        this.fZO = AudioTrack.getMinBufferSize(this.nSamplerate, this.fZK, 2);
        if (this.aQf != null) {
            try {
                this.aQf.stop();
                this.aQf.release();
                this.aQf = null;
            } catch (Exception e) {
                ab.e("MicroMsg.SpeexPlayer", e.getMessage());
                this.aQf = null;
            } catch (Throwable th) {
                this.aQf = null;
                AppMethodBeat.o(55822);
            }
        }
        if (z) {
            i = 3;
        } else {
            i = 0;
        }
        if (q.etd.epG) {
            q.etd.dump();
            if (q.etd.epN == 1) {
                i = 3;
            }
        }
        this.aQf = new e(i, this.nSamplerate, this.fZK, this.fZO * 8);
        try {
            this.aQf.play();
        } catch (Exception e2) {
            ab.e("MicroMsg.SpeexPlayer", "audioTrack error:%s", new Object[]{e2.getMessage()});
        }
        this.fZQ = false;
        AppMethodBeat.o(55822);
    }

    public final boolean S(String str, boolean z) {
        AppMethodBeat.i(55823);
        boolean T = T(str, z);
        AppMethodBeat.o(55823);
        return T;
    }

    public final boolean c(String str, boolean z, int i) {
        AppMethodBeat.i(55824);
        boolean T = T(str, z);
        AppMethodBeat.o(55824);
        return T;
    }

    private boolean T(String str, boolean z) {
        AppMethodBeat.i(55825);
        if (this.status != 0) {
            ab.e("MicroMsg.SpeexPlayer", "startPlay error status:" + this.status);
            AppMethodBeat.o(55825);
            return false;
        }
        ab.i("MicroMsg.SpeexPlayer", "startPlay");
        this.status = 1;
        this.fileName = str;
        try {
            db(z);
        } catch (Exception e) {
            try {
                db(true);
            } catch (Exception e2) {
                ab.e("MicroMsg.SpeexPlayer", "startPlay File[" + this.fileName + "] failed");
                ab.e("MicroMsg.SpeexPlayer", "exception:%s", new Object[]{bo.l(e)});
                this.status = -1;
                AppMethodBeat.o(55825);
                return false;
            }
        }
        AppMethodBeat.o(55825);
        return true;
    }

    /* JADX WARNING: Missing block: B:9:0x0027, code skipped:
            if (com.tencent.mm.compatible.e.q.etd.epN == 1) goto L_0x0029;
     */
    private void db(boolean r9) {
        /*
        r8 = this;
        r0 = 3;
        r7 = 1;
        r2 = 0;
        r6 = 55826; // 0xda12 float:7.8229E-41 double:2.75817E-319;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r6);
        r1 = r8.fileName;
        r1 = com.tencent.mm.vfs.e.ct(r1);
        if (r1 != 0) goto L_0x0015;
    L_0x0011:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r6);
    L_0x0014:
        return;
    L_0x0015:
        if (r9 == 0) goto L_0x0068;
    L_0x0017:
        r1 = r0;
    L_0x0018:
        r3 = com.tencent.mm.compatible.e.q.etd;	 Catch:{ Exception -> 0x006a }
        r3 = r3.epG;	 Catch:{ Exception -> 0x006a }
        if (r3 == 0) goto L_0x00ab;
    L_0x001e:
        r3 = com.tencent.mm.compatible.e.q.etd;	 Catch:{ Exception -> 0x006a }
        r3.dump();	 Catch:{ Exception -> 0x006a }
        r3 = com.tencent.mm.compatible.e.q.etd;	 Catch:{ Exception -> 0x006a }
        r3 = r3.epN;	 Catch:{ Exception -> 0x006a }
        if (r3 != r7) goto L_0x00ab;
    L_0x0029:
        r1 = r8.nSamplerate;	 Catch:{ Exception -> 0x006a }
        r3 = r8.fZK;	 Catch:{ Exception -> 0x006a }
        r4 = 2;
        r1 = android.media.AudioTrack.getMinBufferSize(r1, r3, r4);	 Catch:{ Exception -> 0x006a }
        r8.fZO = r1;	 Catch:{ Exception -> 0x006a }
        r8.alX();	 Catch:{ Exception -> 0x006a }
        r1 = new com.tencent.mm.compatible.b.e;	 Catch:{ Exception -> 0x006a }
        r3 = r8.nSamplerate;	 Catch:{ Exception -> 0x006a }
        r4 = r8.fZK;	 Catch:{ Exception -> 0x006a }
        r5 = r8.fZO;	 Catch:{ Exception -> 0x006a }
        r5 = r5 * 8;
        r1.<init>(r0, r3, r4, r5);	 Catch:{ Exception -> 0x006a }
        r8.aQf = r1;	 Catch:{ Exception -> 0x006a }
        r0 = r8.nSamplerate;	 Catch:{ Exception -> 0x006a }
        r0 = r0 / 1000;
        r1 = r8.cIb;	 Catch:{ Exception -> 0x006a }
        r0 = r0 * r1;
        r1 = r8.fZL;	 Catch:{ Exception -> 0x006a }
        r0 = r0 * r1;
        r0 = r0 * 2;
        r8.fZU = r0;	 Catch:{ Exception -> 0x006a }
        r0 = r8.cop;	 Catch:{ Exception -> 0x006a }
        if (r0 == 0) goto L_0x0061;
    L_0x0058:
        r0 = r8.fZC;	 Catch:{ Exception -> 0x006a }
        if (r0 == 0) goto L_0x0061;
    L_0x005c:
        r0 = r8.cop;	 Catch:{ Exception -> 0x006a }
        r0.requestFocus();	 Catch:{ Exception -> 0x006a }
    L_0x0061:
        r8.alY();	 Catch:{ Exception -> 0x006a }
        com.tencent.matrix.trace.core.AppMethodBeat.o(r6);
        goto L_0x0014;
    L_0x0068:
        r1 = r2;
        goto L_0x0018;
    L_0x006a:
        r0 = move-exception;
        r1 = r8.cop;
        if (r1 == 0) goto L_0x0078;
    L_0x006f:
        r1 = r8.fZC;
        if (r1 == 0) goto L_0x0078;
    L_0x0073:
        r1 = r8.cop;
        r1.Mm();
    L_0x0078:
        r8.alX();
        r1 = "MicroMsg.SpeexPlayer";
        r3 = new java.lang.StringBuilder;
        r4 = "playImp : fail, exception = ";
        r3.<init>(r4);
        r4 = r0.getMessage();
        r3 = r3.append(r4);
        r3 = r3.toString();
        com.tencent.mm.sdk.platformtools.ab.e(r1, r3);
        r1 = "MicroMsg.SpeexPlayer";
        r3 = "exception:%s";
        r4 = new java.lang.Object[r7];
        r0 = com.tencent.mm.sdk.platformtools.bo.l(r0);
        r4[r2] = r0;
        com.tencent.mm.sdk.platformtools.ab.e(r1, r3, r4);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r6);
        goto L_0x0014;
    L_0x00ab:
        r0 = r1;
        goto L_0x0029;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.mm.modelvoice.j.db(boolean):void");
    }

    private void alY() {
        AppMethodBeat.i(55827);
        if (this.aQf != null) {
            try {
                this.aQf.play();
                this.fZM = new 3(this);
                com.tencent.mm.sdk.g.d.post(this.fZM, "SpeexPlayer_play");
            } catch (Exception e) {
                ab.e("MicroMsg.SpeexPlayer", "audioTrack error:%s", new Object[]{e.getMessage()});
                AppMethodBeat.o(55827);
                return;
            }
        }
        AppMethodBeat.o(55827);
    }

    public final boolean bn(boolean z) {
        AppMethodBeat.i(55828);
        if (this.status != 1) {
            AppMethodBeat.o(55828);
            return false;
        }
        this.status = 2;
        synchronized (this.fZu) {
            try {
                ab.v("MicroMsg.SpeexPlayer", "before mOk.wait");
                long currentTimeMillis = System.currentTimeMillis();
                this.fZu.wait();
                ab.v("MicroMsg.SpeexPlayer", "after mOk.wait time:" + (System.currentTimeMillis() - currentTimeMillis));
                if (this.cop != null && z && this.fZC) {
                    this.cop.Mm();
                }
            } catch (Exception e) {
                ab.e("MicroMsg.SpeexPlayer", "exception:%s", new Object[]{bo.l(e)});
                if (this.cop != null && z && this.fZC) {
                    this.cop.Mm();
                }
                AppMethodBeat.o(55828);
                return false;
            } catch (Throwable th) {
                AppMethodBeat.o(55828);
                throw th;
            }
        }
        AppMethodBeat.o(55828);
        return true;
    }

    public final boolean Ew() {
        AppMethodBeat.i(55829);
        if (this.status != 2) {
            AppMethodBeat.o(55829);
            return false;
        }
        this.status = 1;
        synchronized (this.fZt) {
            try {
                ab.v("MicroMsg.SpeexPlayer", "before mpause.notify");
                this.fZt.notify();
                ab.v("MicroMsg.SpeexPlayer", "after mpause.notify");
                if (this.cop != null && this.fZC) {
                    this.cop.requestFocus();
                }
            } catch (Exception e) {
                ab.e("MicroMsg.SpeexPlayer", "exception:%s", new Object[]{bo.l(e)});
                if (this.cop != null && this.fZC) {
                    this.cop.requestFocus();
                }
                AppMethodBeat.o(55829);
                return false;
            } catch (Throwable th) {
                AppMethodBeat.o(55829);
                throw th;
            }
        }
        AppMethodBeat.o(55829);
        return true;
    }

    public final boolean isPlaying() {
        return this.status == 1;
    }

    public final boolean EH() {
        AppMethodBeat.i(55830);
        ab.e("MicroMsg.SpeexPlayer", "stop  status:" + this.status);
        if (this.status == 1 || this.status == 2) {
            this.status = 3;
            synchronized (this.fZt) {
                try {
                    this.fZt.notify();
                    if (this.cop != null && this.fZC) {
                        this.cop.Mm();
                    }
                } catch (Exception e) {
                    ab.e("MicroMsg.SpeexPlayer", "exception:%s", new Object[]{bo.l(e)});
                    alX();
                    if (this.cop != null && this.fZC) {
                        this.cop.Mm();
                    }
                    AppMethodBeat.o(55830);
                    return false;
                } catch (Throwable th) {
                    AppMethodBeat.o(55830);
                    throw th;
                }
            }
            AppMethodBeat.o(55830);
            return true;
        }
        ab.e("MicroMsg.SpeexPlayer", "stop  error status:" + this.status);
        try {
            synchronized (this.fZt) {
                this.fZt.notify();
            }
            synchronized (this.fZu) {
                this.fZu.notify();
            }
        } catch (Exception e2) {
        } catch (Throwable th2) {
            while (true) {
                AppMethodBeat.o(55830);
                throw th2;
            }
        }
        this.status = 3;
        AppMethodBeat.o(55830);
        return false;
    }

    public final double Ez() {
        return 0.0d;
    }

    public final void b(b.a aVar) {
        AppMethodBeat.i(55831);
        if (!(aVar == null || this.cop == null)) {
            this.cop.a(aVar);
        }
        AppMethodBeat.o(55831);
    }

    public final void alP() {
        this.fZC = false;
    }
}
