Out of the box android video player base on ijkplayer 0.8.4
this project is total refactor of GiraffePlayer to support in ListView/RecyclerView and improve the performance,all player tasks do in worker thread.
- base on ijkplayer,support RTMP , HLS (http & https) , MP4,M4A etc.
- gestures for volume control
- gestures for brightness control
- gestures for forward or backward
- fullscreen by manual or sensor (with animation)
- try to replay when error(only for live video)
- specify video scale type
- support in ListView/RecyclerView (in Activity or Fragment)
- never block UI thread
- support select track
- support float window
//step 1: add jcenter repositories in your root poject build file
repositories {
...
jcenter()
}
//step 2: add dependency
compile 'com.github.tcking:giraffeplayer2:0.1.16'
// if need more decoder using: compile 'com.github.tcking:giraffeplayer2:0.1.16-full'
support more ABI: In most cases your app only need to support armeabi-v7a
. some articles about ABI :
- How to use 32-bit native libaries on 64-bit Android device
- What you should know about .so files
- 关于Android的.so文件你所需要知道的
to support different ABI:
compile 'com.github.tcking:ijkplayer-arm64:0.8.4' //support arm64
compile 'com.github.tcking:ijkplayer-armv5:0.8.4' //support armv5
compile 'com.github.tcking:ijkplayer-x86:0.8.4' //support x86
compile 'com.github.tcking:ijkplayer-x86_64:0.8.4' //support x86_64
How to use (example code)
just call GiraffePlayer.play(getContext(), new VideoInfo("video url"));
,all is done.
<tcking.github.com.giraffeplayer2.VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
VideoView videoView = (VideoView) findViewById(R.id.video_view);
videoView.setVideoPath(videoUri).getPlayer().start();
player in ListView or RecyclerView example code
in ListView or RecyclerView,you need do one more thing: call videoView.setFingerprint()
,
the fingerprint is the key that player distinguish list items,you can using list position
or list data's hashcode
as fingerprint
,eg:
public void onBindViewHolder(VideoItemHolder holder, int position) {
VideoItem videoItem = data.get(position);
holder.name.setText(videoItem.name);
holder.url.setText(videoItem.uri);
holder.videoView.setVideoPath(videoItem.uri).setFingerprint(position);// or using:setFingerprint(videoItem.hashCode())
}
all the configurations in VideoInfo,you can get VideoInfo and then set configurations,eg:
//standalone player
VideoInfo videoInfo = new VideoInfo("http://xxx.mp4")
.setTitle("test video") //config title
.setAspectRatio(aspectRatio) //aspectRatio
.setShowTopBar(true) //show mediacontroller top bar
.setPortraitWhenFullScreen(true);//portrait when full screen
GiraffePlayer.play(getContext(), videoInfo);
//in RecyclerView or embed player
public void onBindViewHolder(VideoItemHolder holder, int position) {
VideoItem videoItem = data.get(position);
holder.name.setText(videoItem.name);
holder.url.setText(videoItem.uri);
holder.videoView.getVideoInfo().setBgColor(Color.GRAY).setAspectRatio(VideoInfo.AR_MATCH_PARENT);//config player
holder.videoView.setVideoPath(videoItem.uri).setFingerprint(position);
}
all the configurations on VideoInfo :
videoInfo.setAspectRatio()
set video view aspect radiovideoInfo.setFingerprint()
in list must call this to distinguish itemsvideoInfo.addOption
add player init optionvideoInfo.setPortraitWhenFullScreen()
control Portrait when full screenvideoInfo.setRetryInterval()
retry to play again interval (in second,<=0 will disable retry)videoInfo.setShowTopBar()
show top bar(back arrow and title) when user tap the viewvideoInfo.VideoInfo()
set video titlevideoInfo.setUri()
set video UrivideoInfo.setBgColor()
set video background colorvideoInfo.setPlayerImpl()
VideoInfo.PLAYER_IMPL_IJK:using ijkplayer for decoder,VideoInfo.PLAYER_IMPL_SYSTEM:using android mediaplayer for decodervideoInfo.addOption()
set extra options,only for ijkplayer,eg:addOption(Option.create(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1L))videoInfo.setFullScreenAnimation()
true for play an animation when enter fullscreen or exit fullscreen,only for setPortraitWhenFullScreen(false) and API>=19
GiraffePlayer
player.start()
player.pause()
player.seekTo()
player.setPlayerListener()
// in RecyclerView,player will create and release on demand,set listener on videoView:videoView.setPlayerListener()
player.stop()
//same as releaseplayer.release()
//release the playerplayer.setVolume()
//set volumeplayer.getTrackInfo()
//get all tracksplayer.selectTrack()
//select track by track indexplayer.deselectTrack()
// deselect track by track indexplayer.setMute()
player.isMute()
player.getCurrentState()
//get current player stateplayer.setDisplayModel()
//set display model:GiraffePlayer.DISPLAY_NORMAL | GiraffePlayer.DISPLAY_FULL_WINDOW | GiraffePlayer.DISPLAY_FLOAT
VideoView (player's display container and media controller)
videoView.getPlayer()
get or create bind playervideoView.setFingerprint()
delegate of bind videoInfo setFingerprintvideoView.setVideoPath()
delegate of bind videoInfo setUrivideoView.isCurrentActivePlayer()
is bind player activevideoView.getMediaController()
return bind mediaControllervideoView.inListView()
is video view in ListView or RecyclerViewvideoView.setPlayerListener()
set player Listener (in ListView or RecyclerView you should call this method rather than player.setPlayerListener
PlayerManager (manage all players,make sure only one player is active)
PlayerManager.getInstance().getCurrentPlayer()
return current active player, return null if there is no active playerPlayerManager.getInstance().releaseCurrent()
release current active playerPlayerManager.getInstance().isCurrentPlayer(fingerprint)
judge player is active by fingerprintPlayerManager.getInstance().getPlayer(VideoView)
get player by video view (will create if not exists)
PlayerListener (player event callback)
void onPreparing();
void onPrepared(GiraffePlayer giraffePlayer);
void onBufferingUpdate(GiraffePlayer giraffePlayer, int percent);
boolean onInfo(GiraffePlayer giraffePlayer, int what, int extra);
void onCompletion(GiraffePlayer giraffePlayer);
void onSeekComplete(GiraffePlayer giraffePlayer);
boolean onError(GiraffePlayer giraffePlayer,int what, int extra);
void onPause(GiraffePlayer giraffePlayer);
void onRelease(GiraffePlayer giraffePlayer);
void onStart(GiraffePlayer giraffePlayer);
void onTargetStateChange(int oldState, int newState);
void onCurrentStateChange(int oldState, int newState);
void onDisplayModelChange(int oldModel, int newModel);