package com.anthony.videolistplayer.media; import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.SparseArray; import android.view.View; import android.widget.TableLayout; import com.anthony.videolistplayer.R; import java.util.Locale; import tv.danmaku.ijk.media.player.IMediaPlayer; import tv.danmaku.ijk.media.player.IjkMediaPlayer; import tv.danmaku.ijk.media.player.MediaPlayerProxy; public class InfoHudViewHolder { private TableLayoutBinder mTableLayoutBinder; private SparseArray<View> mRowMap = new SparseArray<View>(); private IMediaPlayer mMediaPlayer; public InfoHudViewHolder(Context context, TableLayout tableLayout) { mTableLayoutBinder = new TableLayoutBinder(context, tableLayout); } private void appendSection(int nameId) { mTableLayoutBinder.appendSection(nameId); } private void appendRow(int nameId) { View rowView = mTableLayoutBinder.appendRow2(nameId, null); mRowMap.put(nameId, rowView); } private void setRowValue(int id, String value) { View rowView = mRowMap.get(id); if (rowView == null) { rowView = mTableLayoutBinder.appendRow2(id, value); mRowMap.put(id, rowView); } else { mTableLayoutBinder.setValueText(rowView, value); } } public void setMediaPlayer(IMediaPlayer mp) { mMediaPlayer = mp; if (mMediaPlayer != null) { mHandler.sendEmptyMessageDelayed(MSG_UPDATE_HUD, 500); } else { mHandler.removeMessages(MSG_UPDATE_HUD); } } private static String formatedDurationMilli(long duration) { if (duration >= 1000) { return String.format(Locale.US, "%.2f sec", ((float)duration) / 1000); } else { return String.format(Locale.US, "%d msec", duration); } } private static String formatedSize(long bytes) { if (bytes >= 100 * 1000) { return String.format(Locale.US, "%.2f MB", ((float)bytes) / 1000 / 1000); } else if (bytes >= 100) { return String.format(Locale.US, "%.1f KB", ((float)bytes) / 1000); } else { return String.format(Locale.US, "%d B", bytes); } } private static final int MSG_UPDATE_HUD = 1; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_UPDATE_HUD: { InfoHudViewHolder holder = InfoHudViewHolder.this; IjkMediaPlayer mp = null; if (mMediaPlayer == null) break; if (mMediaPlayer instanceof IjkMediaPlayer) { mp = (IjkMediaPlayer) mMediaPlayer; } else if (mMediaPlayer instanceof MediaPlayerProxy) { MediaPlayerProxy proxy = (MediaPlayerProxy) mMediaPlayer; IMediaPlayer internal = proxy.getInternalMediaPlayer(); if (internal != null && internal instanceof IjkMediaPlayer) mp = (IjkMediaPlayer) internal; } if (mp == null) break; int vdec = mp.getVideoDecoder(); switch (vdec) { case IjkMediaPlayer.FFP_PROPV_DECODER_AVCODEC: setRowValue(R.string.vdec, "avcodec"); break; case IjkMediaPlayer.FFP_PROPV_DECODER_MEDIACODEC: setRowValue(R.string.vdec, "MediaCodec"); break; default: setRowValue(R.string.vdec, ""); break; } float fpsOutput = mp.getVideoOutputFramesPerSecond(); float fpsDecode = mp.getVideoDecodeFramesPerSecond(); setRowValue(R.string.fps, String.format(Locale.US, "%.2f / %.2f", fpsDecode, fpsOutput)); long videoCachedDuration = mp.getVideoCachedDuration(); long audioCachedDuration = mp.getAudioCachedDuration(); long videoCachedBytes = mp.getVideoCachedBytes(); long audioCachedBytes = mp.getAudioCachedBytes(); setRowValue(R.string.v_cache, String.format(Locale.US, "%s, %s", formatedDurationMilli(videoCachedDuration), formatedSize(videoCachedBytes))); setRowValue(R.string.a_cache, String.format(Locale.US, "%s, %s", formatedDurationMilli(audioCachedDuration), formatedSize(audioCachedBytes))); mHandler.removeMessages(MSG_UPDATE_HUD); mHandler.sendEmptyMessageDelayed(MSG_UPDATE_HUD, 500); } } } }; }