package com.tencent.mm.modelvoice;

import android.content.Context;
import android.media.MediaPlayer;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.compatible.b.k;
import com.tencent.mm.modelvoice.d.a;
import com.tencent.mm.modelvoice.d.b;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.ax;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.ytcommon.BuildConfig;

public final class s implements d {
    boolean fZC;
    a fZI;
    b fZJ;
    String fileName;
    MediaPlayer gaq;
    com.tencent.mm.compatible.util.b gar;
    int status;

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

    public s() {
        AppMethodBeat.i(55841);
        this.fileName = BuildConfig.FLAVOR;
        this.fZI = null;
        this.fZJ = null;
        this.status = 0;
        this.fZC = true;
        this.gaq = new k();
        amh();
        ami();
        ab.d("MicroMsg.VoicePlayer", "VoicePlayer");
        AppMethodBeat.o(55841);
    }

    public s(Context context) {
        this();
        AppMethodBeat.i(55842);
        this.gar = new com.tencent.mm.compatible.util.b(context);
        ab.d("MicroMsg.VoicePlayer", "VoicePlayer context");
        AppMethodBeat.o(55842);
    }

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

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

    private void amh() {
        AppMethodBeat.i(55843);
        this.gaq.setOnCompletionListener(new 1(this));
        AppMethodBeat.o(55843);
    }

    private void ami() {
        AppMethodBeat.i(55844);
        this.gaq.setOnErrorListener(new 2(this));
        AppMethodBeat.o(55844);
    }

    public final void bo(boolean z) {
        AppMethodBeat.i(55845);
        ab.d("MicroMsg.VoicePlayer", "setSpeakerOn=".concat(String.valueOf(z)));
        if (this.gaq == null) {
            AppMethodBeat.o(55845);
        } else if (ax.Ex()) {
            ab.v("MicroMsg.VoicePlayer", "setSpeakOn return when calling");
            AppMethodBeat.o(55845);
        } else {
            int currentPosition = this.gaq.getCurrentPosition();
            EH();
            this.gaq = new k();
            amh();
            ami();
            d(this.fileName, z, currentPosition);
            AppMethodBeat.o(55845);
        }
    }

    public final boolean S(String str, boolean z) {
        AppMethodBeat.i(55846);
        boolean d = d(str, z, 0);
        AppMethodBeat.o(55846);
        return d;
    }

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

    private boolean d(String str, boolean z, int i) {
        AppMethodBeat.i(55848);
        if (this.status != 0) {
            ab.e("MicroMsg.VoicePlayer", "startPlay error status:" + this.status);
            AppMethodBeat.o(55848);
            return false;
        }
        ab.i("MicroMsg.VoicePlayer", "startPlay speakerOn:%s,seekTo:%s,", new Object[]{Boolean.valueOf(z), Integer.valueOf(i)});
        this.fileName = str;
        try {
            f(z, i);
        } catch (Exception e) {
            try {
                f(true, i);
            } catch (Exception e2) {
                ab.e("MicroMsg.VoicePlayer", "startPlay File[" + this.fileName + "] failed");
                ab.e("MicroMsg.VoicePlayer", "exception:%s", new Object[]{bo.l(e)});
                this.status = -1;
                AppMethodBeat.o(55848);
                return false;
            }
        }
        this.status = 1;
        AppMethodBeat.o(55848);
        return true;
    }

    /* JADX WARNING: Missing block: B:9:0x0048, code skipped:
            if (com.tencent.mm.compatible.e.q.etd.epN == 1) goto L_0x004a;
     */
    private void f(boolean r11, int r12) {
        /*
        r10 = this;
        r0 = 3;
        r9 = 1;
        r2 = 0;
        r8 = 55849; // 0xda29 float:7.8261E-41 double:2.7593E-319;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r8);
        r1 = r10.fileName;
        r1 = com.tencent.mm.vfs.e.ct(r1);
        if (r1 != 0) goto L_0x0015;
    L_0x0011:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r8);
    L_0x0014:
        return;
    L_0x0015:
        if (r11 == 0) goto L_0x0081;
    L_0x0017:
        r1 = r0;
    L_0x0018:
        r3 = "MicroMsg.VoicePlayer";
        r4 = "playImp speakerOn:%s,seekTo:%s,type:%s";
        r5 = 3;
        r5 = new java.lang.Object[r5];	 Catch:{ Exception -> 0x0083 }
        r6 = 0;
        r7 = java.lang.Boolean.valueOf(r11);	 Catch:{ Exception -> 0x0083 }
        r5[r6] = r7;	 Catch:{ Exception -> 0x0083 }
        r6 = 1;
        r7 = java.lang.Integer.valueOf(r12);	 Catch:{ Exception -> 0x0083 }
        r5[r6] = r7;	 Catch:{ Exception -> 0x0083 }
        r6 = 2;
        r7 = java.lang.Integer.valueOf(r1);	 Catch:{ Exception -> 0x0083 }
        r5[r6] = r7;	 Catch:{ Exception -> 0x0083 }
        com.tencent.mm.sdk.platformtools.ab.d(r3, r4, r5);	 Catch:{ Exception -> 0x0083 }
        r3 = com.tencent.mm.compatible.e.q.etd;	 Catch:{ Exception -> 0x0083 }
        r3 = r3.epG;	 Catch:{ Exception -> 0x0083 }
        if (r3 == 0) goto L_0x00c1;
    L_0x003f:
        r3 = com.tencent.mm.compatible.e.q.etd;	 Catch:{ Exception -> 0x0083 }
        r3.dump();	 Catch:{ Exception -> 0x0083 }
        r3 = com.tencent.mm.compatible.e.q.etd;	 Catch:{ Exception -> 0x0083 }
        r3 = r3.epN;	 Catch:{ Exception -> 0x0083 }
        if (r3 != r9) goto L_0x00c1;
    L_0x004a:
        r1 = r10.gar;	 Catch:{ Exception -> 0x0083 }
        if (r1 == 0) goto L_0x0060;
    L_0x004e:
        r1 = r10.fZC;	 Catch:{ Exception -> 0x0083 }
        if (r1 == 0) goto L_0x0060;
    L_0x0052:
        r1 = "MicroMsg.VoicePlayer";
        r3 = "playImp audioFocusHelper.requestFocus";
        com.tencent.mm.sdk.platformtools.ab.d(r1, r3);	 Catch:{ Exception -> 0x0083 }
        r1 = r10.gar;	 Catch:{ Exception -> 0x0083 }
        r1.requestFocus();	 Catch:{ Exception -> 0x0083 }
    L_0x0060:
        r1 = r10.gaq;	 Catch:{ Exception -> 0x0083 }
        r1.setAudioStreamType(r0);	 Catch:{ Exception -> 0x0083 }
        r0 = r10.gaq;	 Catch:{ Exception -> 0x0083 }
        r1 = r10.fileName;	 Catch:{ Exception -> 0x0083 }
        r0.setDataSource(r1);	 Catch:{ Exception -> 0x0083 }
        r0 = r10.gaq;	 Catch:{ Exception -> 0x0083 }
        r0.prepare();	 Catch:{ Exception -> 0x0083 }
        if (r12 <= 0) goto L_0x0078;
    L_0x0073:
        r0 = r10.gaq;	 Catch:{ Exception -> 0x0083 }
        r0.seekTo(r12);	 Catch:{ Exception -> 0x0083 }
    L_0x0078:
        r0 = r10.gaq;	 Catch:{ Exception -> 0x0083 }
        r0.start();	 Catch:{ Exception -> 0x0083 }
        com.tencent.matrix.trace.core.AppMethodBeat.o(r8);
        goto L_0x0014;
    L_0x0081:
        r1 = r2;
        goto L_0x0018;
    L_0x0083:
        r0 = move-exception;
        r1 = "MicroMsg.VoicePlayer";
        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.VoicePlayer";
        r3 = "exception:%s";
        r4 = new java.lang.Object[r9];
        r0 = com.tencent.mm.sdk.platformtools.bo.l(r0);
        r4[r2] = r0;
        com.tencent.mm.sdk.platformtools.ab.e(r1, r3, r4);
        r0 = r10.gar;
        if (r0 == 0) goto L_0x00bc;
    L_0x00b3:
        r0 = r10.fZC;
        if (r0 == 0) goto L_0x00bc;
    L_0x00b7:
        r0 = r10.gar;
        r0.Mm();
    L_0x00bc:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r8);
        goto L_0x0014;
    L_0x00c1:
        r0 = r1;
        goto L_0x004a;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.mm.modelvoice.s.f(boolean, int):void");
    }

    public final boolean bn(boolean z) {
        AppMethodBeat.i(55850);
        if (this.status != 1) {
            ab.e("MicroMsg.VoicePlayer", "pause not STATUS_PLAYING error status:" + this.status);
            AppMethodBeat.o(55850);
            return false;
        }
        try {
            ab.d("MicroMsg.VoicePlayer", "pause mediaPlayer.pause()");
            this.gaq.pause();
            if (this.gar != null && z && this.fZC) {
                ab.d("MicroMsg.VoicePlayer", "pause audioFocusHelper.abandonFocus()");
                this.gar.Mm();
            }
            this.status = 2;
            AppMethodBeat.o(55850);
            return true;
        } catch (Exception e) {
            ab.e("MicroMsg.VoicePlayer", "pause File[" + this.fileName + "] ErrMsg[" + e.getStackTrace() + "]");
            this.status = -1;
            return false;
        } finally {
            if (this.gar != null && z && this.fZC) {
                ab.d("MicroMsg.VoicePlayer", "pause audioFocusHelper.abandonFocus()");
                this.gar.Mm();
            }
            AppMethodBeat.o(55850);
        }
    }

    public final boolean Ew() {
        AppMethodBeat.i(55851);
        if (this.status != 2) {
            ab.e("MicroMsg.VoicePlayer", "resume not STATUS_PAUSE error status:" + this.status);
            AppMethodBeat.o(55851);
            return false;
        }
        try {
            ab.d("MicroMsg.VoicePlayer", "resume mediaPlayer.start()");
            this.gaq.start();
            if (this.gar != null && this.fZC) {
                ab.d("MicroMsg.VoicePlayer", "resume audioFocusHelper.requestFocus()");
                this.gar.requestFocus();
            }
            this.status = 1;
            AppMethodBeat.o(55851);
            return true;
        } catch (Exception e) {
            ab.e("MicroMsg.VoicePlayer", "resume File[" + this.fileName + "] ErrMsg[" + e.getStackTrace() + "]");
            this.status = -1;
            return false;
        } finally {
            if (this.gar != null && this.fZC) {
                ab.d("MicroMsg.VoicePlayer", "resume audioFocusHelper.requestFocus()");
                this.gar.requestFocus();
            }
            AppMethodBeat.o(55851);
        }
    }

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

    public final boolean EH() {
        AppMethodBeat.i(55852);
        if (this.status == 1 || this.status == 2) {
            try {
                ab.d("MicroMsg.VoicePlayer", "stop mediaPlayer.stop()");
                this.gaq.stop();
                this.gaq.release();
                if (this.gar != null && this.fZC) {
                    ab.d("MicroMsg.VoicePlayer", "stop audioFocusHelper.abandonFocus()");
                    this.gar.Mm();
                }
                this.status = 0;
                AppMethodBeat.o(55852);
                return true;
            } catch (Exception e) {
                ab.e("MicroMsg.VoicePlayer", "stop File[" + this.fileName + "] ErrMsg[" + e.getStackTrace() + "]");
                this.status = -1;
                return false;
            } finally {
                if (this.gar != null && this.fZC) {
                    ab.d("MicroMsg.VoicePlayer", "stop audioFocusHelper.abandonFocus()");
                    this.gar.Mm();
                }
                AppMethodBeat.o(55852);
            }
        } else {
            ab.e("MicroMsg.VoicePlayer", "stop not STATUS_PLAYING or STATUS_PAUSE error status:" + this.status);
            AppMethodBeat.o(55852);
            return false;
        }
    }

    public final double Ez() {
        AppMethodBeat.i(55853);
        if (this.status == 1 || this.status == 2) {
            try {
                int currentPosition = this.gaq.getCurrentPosition();
                int duration = this.gaq.getDuration();
                if (duration == 0) {
                    ab.e("MicroMsg.VoicePlayer", "getDuration File[" + this.fileName + "] Failed");
                    AppMethodBeat.o(55853);
                    return 0.0d;
                }
                double d = ((double) currentPosition) / ((double) duration);
                AppMethodBeat.o(55853);
                return d;
            } catch (Exception e) {
                ab.e("MicroMsg.VoicePlayer", "getNowProgress File[" + this.fileName + "] ErrMsg[" + e.getStackTrace() + "]");
                EH();
                AppMethodBeat.o(55853);
                return 0.0d;
            }
        }
        AppMethodBeat.o(55853);
        return 0.0d;
    }

    public final void b(com.tencent.mm.compatible.util.b.a aVar) {
        AppMethodBeat.i(55854);
        if (!(this.gar == null || aVar == null)) {
            this.gar.a(aVar);
        }
        AppMethodBeat.o(55854);
    }

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