package com.ss.android.ugc.aweme.live.alphaplayer.player;

import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.view.Surface;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.aweme.live.alphaplayer.model.VideoInfo;

public class DefaultSystemPlayer extends AbsPlayer<DefaultSystemPlayer> {
    public String dataSource;
    public MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
        static {
            Covode.recordClassIndex(91077);
        }

        public final void onCompletion(MediaPlayer mediaPlayer) {
            if (DefaultSystemPlayer.this.completionListener != null) {
                DefaultSystemPlayer.this.completionListener.onCompletion(DefaultSystemPlayer.this.self);
            }
        }
    };
    public MediaPlayer.OnErrorListener mErrorListener = new MediaPlayer.OnErrorListener() {
        static {
            Covode.recordClassIndex(91079);
        }

        public final boolean onError(MediaPlayer mediaPlayer, int i, int i2) {
            if (DefaultSystemPlayer.this.errorListener == null) {
                return false;
            }
            DefaultSystemPlayer.this.errorListener.onError(DefaultSystemPlayer.this.self, i, i2, "");
            return false;
        }
    };
    public MediaPlayer.OnInfoListener mInfoListener = new MediaPlayer.OnInfoListener() {
        static {
            Covode.recordClassIndex(91080);
        }

        public final boolean onInfo(MediaPlayer mediaPlayer, int i, int i2) {
            if (i != 3 || DefaultSystemPlayer.this.firstFrameListener == null) {
                return false;
            }
            DefaultSystemPlayer.this.firstFrameListener.onFirstFrame(DefaultSystemPlayer.this.self);
            return false;
        }
    };
    public MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
        static {
            Covode.recordClassIndex(91078);
        }

        public final void onPrepared(MediaPlayer mediaPlayer) {
            if (DefaultSystemPlayer.this.preparedListener != null) {
                DefaultSystemPlayer.this.preparedListener.onPrepared(DefaultSystemPlayer.this.self);
            }
        }
    };
    public MediaPlayer mediaPlayer;
    public MediaMetadataRetriever retriever = new MediaMetadataRetriever();

    static {
        Covode.recordClassIndex(91076);
    }

    public String getPlayerSimpleName() {
        return "DefaultSystemPlayer";
    }

    public int getCurrentPosition() {
        return this.mediaPlayer.getCurrentPosition();
    }

    public boolean isPlaying() {
        return this.mediaPlayer.isPlaying();
    }

    public void pause() {
        this.mediaPlayer.pause();
    }

    public void prepareAsync() {
        this.mediaPlayer.prepareAsync();
    }

    public void start() {
        this.mediaPlayer.start();
    }

    public void stop() {
        this.mediaPlayer.stop();
    }

    public void release() {
        this.mediaPlayer.release();
        this.dataSource = null;
    }

    public void reset() {
        this.mediaPlayer.reset();
        this.dataSource = null;
    }

    public void initMediaPlayer() {
        MediaPlayer mediaPlayer2 = new MediaPlayer();
        this.mediaPlayer = mediaPlayer2;
        mediaPlayer2.setOnCompletionListener(this.mCompletionListener);
        this.mediaPlayer.setOnPreparedListener(this.mPreparedListener);
        this.mediaPlayer.setOnErrorListener(this.mErrorListener);
        this.mediaPlayer.setOnInfoListener(this.mInfoListener);
    }

    public VideoInfo getVideoInfo() {
        if (!TextUtils.isEmpty(this.dataSource)) {
            this.retriever.setDataSource(this.dataSource);
            String extractMetadata = this.retriever.extractMetadata(18);
            String extractMetadata2 = this.retriever.extractMetadata(19);
            if (!TextUtils.isEmpty(extractMetadata) && !TextUtils.isEmpty(extractMetadata2)) {
                return new VideoInfo(Integer.parseInt(this.retriever.extractMetadata(18)), Integer.parseInt(this.retriever.extractMetadata(19)), this.mediaPlayer.getDuration());
            }
            throw new Exception("retriever get metadata failure");
        }
        throw new Exception("dataSource is null, please set setDataSource firstly");
    }

    public void seekTo(int i) {
        this.mediaPlayer.seekTo((long) i, 3);
    }

    public void setDataSource(String str) {
        this.dataSource = str;
        this.mediaPlayer.setDataSource(str);
    }

    public void setLooping(boolean z) {
        this.mediaPlayer.setLooping(z);
    }

    public void setScreenOnWhilePlaying(boolean z) {
        this.mediaPlayer.setScreenOnWhilePlaying(z);
    }

    public void setSurface(Surface surface) {
        this.mediaPlayer.setSurface(surface);
    }
}
