001/*
002 * Copyright (C) 2013-2014 Zhang Rui <bbcallen@gmail.com>
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.baidu.cloud.media.player;
018
019import com.baidu.cloud.media.player.misc.IMediaDataSource;
020
021@SuppressWarnings("WeakerAccess")
022public abstract class AbstractMediaPlayer implements IMediaPlayer {
023    private OnPreparedListener mOnPreparedListener;
024    private OnCompletionListener mOnCompletionListener;
025    private OnBufferingUpdateListener mOnBufferingUpdateListener;
026    private OnSeekCompleteListener mOnSeekCompleteListener;
027    private OnVideoSizeChangedListener mOnVideoSizeChangedListener;
028    private OnErrorListener mOnErrorListener;
029    private OnInfoListener mOnInfoListener;
030
031    public final void setOnPreparedListener(OnPreparedListener listener) {
032        mOnPreparedListener = listener;
033    }
034
035    public final void setOnCompletionListener(OnCompletionListener listener) {
036        mOnCompletionListener = listener;
037    }
038
039    public final void setOnBufferingUpdateListener(
040            OnBufferingUpdateListener listener) {
041        mOnBufferingUpdateListener = listener;
042    }
043
044    public final void setOnSeekCompleteListener(OnSeekCompleteListener listener) {
045        mOnSeekCompleteListener = listener;
046    }
047
048    public final void setOnVideoSizeChangedListener(
049            OnVideoSizeChangedListener listener) {
050        mOnVideoSizeChangedListener = listener;
051    }
052
053    public final void setOnErrorListener(OnErrorListener listener) {
054        mOnErrorListener = listener;
055    }
056
057    public final void setOnInfoListener(OnInfoListener listener) {
058        mOnInfoListener = listener;
059    }
060
061    public void resetListeners() {
062        mOnPreparedListener = null;
063        mOnBufferingUpdateListener = null;
064        mOnCompletionListener = null;
065        mOnSeekCompleteListener = null;
066        mOnVideoSizeChangedListener = null;
067        mOnErrorListener = null;
068        mOnInfoListener = null;
069    }
070
071    protected final void notifyOnPrepared() {
072        if (mOnPreparedListener != null) {
073            mOnPreparedListener.onPrepared(this);
074        }
075    }
076
077    protected final void notifyOnCompletion() {
078        if (mOnCompletionListener != null) {
079            mOnCompletionListener.onCompletion(this);
080        }
081    }
082
083    protected final void notifyOnBufferingUpdate(int percent) {
084        if (mOnBufferingUpdateListener != null) {
085            mOnBufferingUpdateListener.onBufferingUpdate(this, percent);
086        }
087    }
088
089    protected final void notifyOnSeekComplete() {
090        if (mOnSeekCompleteListener != null) {
091            mOnSeekCompleteListener.onSeekComplete(this);
092        }
093    }
094
095    protected final void notifyOnVideoSizeChanged(int width, int height, int sarNum, int sarDen) {
096        if (mOnVideoSizeChangedListener != null) {
097            mOnVideoSizeChangedListener.onVideoSizeChanged(this, width, height, sarNum, sarDen);
098        }
099    }
100
101    protected final boolean notifyOnError(int what, int extra) {
102        return mOnErrorListener != null && mOnErrorListener.onError(this, what, extra);
103    }
104
105    protected final boolean notifyOnInfo(int what, int extra) {
106        return mOnInfoListener != null && mOnInfoListener.onInfo(this, what, extra);
107    }
108
109    public void setDataSource(IMediaDataSource mediaDataSource) {
110        throw new UnsupportedOperationException();
111    }
112}