Android Open Source - VankeTV Video View Playing Activity






From Project

Back to project page VankeTV.

License

The source code is released under:

GNU General Public License

If you think the Android project VankeTV listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.baidu.bvideoviewsample1;
// w w  w . java  2  s  .  c  o m
import com.baidu.bvideoviewsample1.R;
//import com.baidu.bvideoviewsample1.view.SubtitleSettingPopupWindow;
import com.baidu.cyberplayer.core.BMediaController;
import com.baidu.cyberplayer.core.BVideoView;
import com.baidu.cyberplayer.core.BVideoView.OnCompletionListener;
import com.baidu.cyberplayer.core.BVideoView.OnErrorListener;
import com.baidu.cyberplayer.core.BVideoView.OnInfoListener;
import com.baidu.cyberplayer.core.BVideoView.OnPlayingBufferCacheListener;
import com.baidu.cyberplayer.core.BVideoView.OnPreparedListener;
import com.baidu.cyberplayer.subtitle.SubtitleManager;
import com.baidu.cyberplayer.subtitle.utils.SubtitleError;
import com.baidu.cyberplayer.subtitle.utils.SubtitleErrorCallback;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.Process;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class VideoViewPlayingActivity extends Activity implements OnPreparedListener, 
              OnCompletionListener,
              OnErrorListener, 
              OnInfoListener,
              OnPlayingBufferCacheListener
              {
  private final String TAG = "VideoViewPlayingActivity";
  
  /**
   * ??ak 
   */
  private String AK = "aQPcpGoGa43BAa3TiZw5OsVB";
  /**
   * //??sk???16??
   */
  private String SK = "hrWz5lPSbzh92Mxz";
  
    
  private String mVideoSource = null;
  
  private BVideoView mVV = null;
  private BMediaController mVVCtl = null;
  private RelativeLayout mViewHolder = null;
  private LinearLayout mControllerHolder = null;
  
  private boolean mIsHwDecode = false;
  
  private EventHandler mEventHandler;
  private HandlerThread mHandlerThread;
  
  private final Object SYNC_Playing = new Object();
    
  private final int EVENT_PLAY = 0;
  
  private WakeLock mWakeLock = null;
  private static final String POWER_LOCK = "VideoViewPlayingActivity";
  
  /**
   * ?????
   */
  private  enum PLAYER_STATUS {
    PLAYER_IDLE, PLAYER_PREPARING, PLAYER_PREPARED,
  }
  
  private PLAYER_STATUS mPlayerStatus = PLAYER_STATUS.PLAYER_IDLE;
  
  
  /**
   * ???????
   */
  private int mLastPos = 0;
  
     //add for subtitle
    private Button mSubtitleButton;
    private RelativeLayout mRoot;
    private SubtitleManager mSubtitleManager;
  
  class EventHandler extends Handler {
    public EventHandler(Looper looper) {
      super(looper);
    }

    @Override
    public void handleMessage(Message msg) {
      switch (msg.what) {
      case EVENT_PLAY:
        /**
         * ????????????????????
         */
        if (mPlayerStatus != PLAYER_STATUS.PLAYER_IDLE) {
          synchronized (SYNC_Playing) {
            try {
              SYNC_Playing.wait();
              Log.v(TAG, "wait player status to idle");
            } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          }
        }

        /**
         * ????url
         */
        mVV.setVideoPath(mVideoSource);
        
        /**
         * ??????????
         */
        if (mLastPos > 0) {

          mVV.seekTo(mLastPos);
          mLastPos = 0;
        }

        /**
         * ?????????????? 
         */
        mVV.showCacheInfo(true);
        
        /**
         * ????
         */
        mVV.start();

        mPlayerStatus = PLAYER_STATUS.PLAYER_PREPARING;
        break;
      default:
        break;
      }
    }
  }
  
  /**
   * ????????
   */
  private View.OnClickListener mPreListener = new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
      // TODO Auto-generated method stub
      Log.v(TAG, "pre btn clicked");
      /**
       * ???????????????????
       */
      if(mPlayerStatus != PLAYER_STATUS.PLAYER_IDLE){
        mVV.stopPlayback();
      }
      
      /**
       * ????????????
       */
      if(mEventHandler.hasMessages(EVENT_PLAY))
        mEventHandler.removeMessages(EVENT_PLAY);
      mEventHandler.sendEmptyMessage(EVENT_PLAY);
    }
  };
  
  private View.OnClickListener mNextListener = new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
      // TODO Auto-generated method stub
      Log.v(TAG, "next btn clicked");
    }
  };
      
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            
    setContentView(R.layout.controllerplaying);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, POWER_LOCK);
    
    mIsHwDecode = getIntent().getBooleanExtra("isHW", false);
    Uri uriPath = getIntent().getData();
    if (null != uriPath) {
      String scheme = uriPath.getScheme();
      if (null != scheme) {
        mVideoSource = uriPath.toString();
      } else {
        mVideoSource = uriPath.getPath();
      }
    }
    
    initUI();
    
    /**
     * ??????????????????
     */
    mHandlerThread = new HandlerThread("event handler thread",
        Process.THREAD_PRIORITY_BACKGROUND);
    mHandlerThread.start();
    mEventHandler = new EventHandler(mHandlerThread.getLooper());
  }
  
  /**
   * ????????
   */
  private void initUI() {    
    mViewHolder = (RelativeLayout)findViewById(R.id.view_holder);
    mControllerHolder = (LinearLayout )findViewById(R.id.controller_holder);
    
    /**
     * ??ak???sk???16??
     */
    BVideoView.setAKSK(AK, SK);
    
    /**
     *??BVideoView?BMediaController
     */
    mVV = new BVideoView(this);
    mVVCtl = new BMediaController(this);
    mViewHolder.addView(mVV);
    mControllerHolder.addView(mVVCtl);
    
    /**
     * ??listener
     */
    mVV.setOnPreparedListener(this);
    mVV.setOnCompletionListener(this);
    mVV.setOnErrorListener(this);
    mVV.setOnInfoListener(this);
    mVVCtl.setPreNextListener(mPreListener, mNextListener);
    
    /**
     * ????BMediaController
     */
    mVV.setMediaController(mVVCtl);
    /**
     * ????????
     */
    mVV.setDecodeMode(BVideoView.DECODE_SW);
  }
  
     


  
  @Override
  protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Log.v(TAG, "onPause");
    /**
     * ????????? ?????????????????,????????????
     */
    if (mPlayerStatus == PLAYER_STATUS.PLAYER_PREPARED) {
      mLastPos = mVV.getCurrentPosition();
      mVV.stopPlayback();
    }
  }

  
  @Override
  protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Log.v(TAG, "onResume");
    if (null != mWakeLock && (!mWakeLock.isHeld())) {
      mWakeLock.acquire();
    }
    /**
     * ??????????,???????????????
     */
    mEventHandler.sendEmptyMessage(EVENT_PLAY);  
  }
  
  @Override
  protected void onStop(){
    super.onStop();
    Log.v(TAG, "onStop");
  }
  
  @TargetApi(Build.VERSION_CODES.ECLAIR)
    @Override
  protected void onDestroy(){
    super.onDestroy();
    /**
     * ??????????????????
     */
    mHandlerThread.quit();
    Log.v(TAG, "onDestroy");
  }

  @Override
  public boolean onInfo(int what, int extra) {
    // TODO Auto-generated method stub
    switch(what){
    /**
     * ????
     */
    case BVideoView.MEDIA_INFO_BUFFERING_START:
      break;
    /**
     * ??????
     */
    case BVideoView.MEDIA_INFO_BUFFERING_END:
      break;
    default:
      break;
    }
    return false;
  }
  
  /**
   * ?????????? ?????????onInfo??????????????????????????
   */
  @Override
  public void onPlayingBufferCache(int percent) {
    // TODO Auto-generated method stub
    
  }  

  /**
   * ????
   */
  @Override
  public boolean onError(int what, int extra) {
    // TODO Auto-generated method stub
    Log.v(TAG, "onError");
    synchronized (SYNC_Playing) {
      SYNC_Playing.notify();
    }
    mPlayerStatus = PLAYER_STATUS.PLAYER_IDLE;
    return true;
  }

  /**
   * ?????
   */
  @Override
  public void onCompletion() {
    // TODO Auto-generated method stub
    Log.v(TAG, "onCompletion");
    
    synchronized (SYNC_Playing) {
      SYNC_Playing.notify();
    }
    mPlayerStatus = PLAYER_STATUS.PLAYER_IDLE;
  }

  /**
   * ??????
   */
  @Override
  public void onPrepared() {
    // TODO Auto-generated method stub
    Log.v(TAG, "onPrepared");        
    mPlayerStatus = PLAYER_STATUS.PLAYER_PREPARED;
  }
}




Java Source Code List

com.baidu.bvideoviewsample1.BuildConfig.java
com.baidu.bvideoviewsample1.MainActivity.java
com.baidu.bvideoviewsample1.VideoViewPlayingActivity.java
com.baidu.bvideoviewsample1.filesselect.FileItem.java
com.baidu.bvideoviewsample1.filesselect.FileSelectDialog.java
com.baidu.bvideoviewsample1.filesselect.FileSelectListView.java
com.baidu.bvideoviewsample1.filesselect.FileSelectedCallBackBundle.java
com.baidu.bvideoviewsample1.view.SubtitleSettingPopupWindow.java