如何定製化Android的播放器(VideoViewEx.java,MediaControllerEx.java,模仿RockPlayer的界面)

 

Introduction

Customized PlayerThis article interpret how to implement a customized video player instead of the UI of android’s MediaController and how to handle error of android’s MediaPlayer to enable it continue replaying from the last position after reset the engine of media player

 

Background  

I have googled lots of open source player for android, unfortunately almost all of the players are just for music but not for video, audio isn’t easy to play failed compared with video, I implement the customized player to enable developer easy to modify controller UI of media player and pay attention to the drawback of VideoView control provided by Android 

Using the code 

Android provide MediaPlayer and VideoView/MediaController to create player, but if dislike the UI and hope to redesign a player to enable it increasing more features such as adjusting volume or full screen , you can refer to my customized player code , it include 3 files, MediaControllerEx.java, VideoViewEx.java, PlayerActivity.java, MediaControllerEx.java implement the controller UI layout for player,if you want to adjust the SeekBar height, you should replace the default style with your customized style. 

// layout your own UI for controller

protected View makeControllerView() {

         LayoutInflater inflate = (LayoutInflater) mContext

                                   .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         mRoot = inflate.inflate(R.layout.media_controller_view, null);

 

         initControllerView(mRoot);

 

         return mRoot;

 

//SeekBar style for controller

<style name="player_seekbar">

         <item name="android:indeterminateOnly">false</item>

         <item name="android:progressDrawable">@drawable/seekbar_style_drawable</item>

         <item name="android:indeterminateDrawable">@drawable/seekbar_style_drawable</item>

         <item name="android:minHeight">10dip</item>

         <item name="android:maxHeight">10dip</item>

         <item name="android:thumb">@drawable/controller_playhead_drawable</item>

         <item name="android:thumbOffset">8px</item>

         <item name="android:focusable">true</item>

</style> 

//assign the seek position when setting video path in VideoViewEx.java

public void setVideoPath(String path,int position) {

                 setVideoURI(Uri.parse(path),position);

 

/**

 * The MediaPlayer often change to error state when playing video,and then prompt "can't play this video" dialog, so you have to handle these error via remembering the played time and replaying video after reset MediaPlayer engine

 */

private MediaPlayer.OnErrorListener mOnErrorListener = new MediaPlayer.OnErrorListener() {

 

 public boolean onError(MediaPlayer mp, int what, int extra) {

 

   switch (what) {

 

    case MediaPlayer.MEDIA_ERROR_SERVER_DIED:

         Toast.makeText(PlayerActivity.this, "MEDIA_ERROR_SERVER_DIED",

                                                    Toast.LENGTH_SHORT).show();

         showErrorDlg(what);

                                  

         return true;

    case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:

        Toast.makeText(PlayerActivity.this,

                                            "MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK",

                                                    Toast.LENGTH_SHORT).show();

         break;

     case MediaPlayer.MEDIA_ERROR_UNKNOWN:

         Toast.makeText(PlayerActivity.this, "MEDIA_ERROR_UNKNOWN",

                                                    Toast.LENGTH_SHORT).show();

         break;

        }

 

         setProgressContainer(true, getString(R.string.msg_handle_error));

        int position=mVideoView.getCurrentPosition();

        if(position>0){

                   mCurPosition=position;

        }

         mVideoView.setVideoPath(mCurrentMediaUrl,position);

 

         return true;

         }

 

};        

 

Points of Interest 

My interest includes streaming player, decoder/encoder/demuxer of android's openCore media framework 

History 

CustomizedPlayer 0.1 version.

 

 備註:懶得用中文再描述一遍,需要繼續完善的地方是如何在播放過程中有相應的提示信息(如正在緩衝,網絡斷線,出錯處理等)

 

 

  • Download Customized Player
發佈了48 篇原創文章 · 獲贊 5 · 訪問量 56萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章