Quick Start
Set AppId(licenseId)
Before the use of SDK, users need to go to Baidu AI Cloud (https://console.bce.baidu.com/bvc/?_=1615793424319#/bvc/license/list) to apply for and download the license file, put it in your project assets directory, and set the LicenseID for the player. Call the static method setAppId of BDCloudMediaPlayer to set appid (licenseId):
BDCloudMediaPlayer.setAppId(appid);
Set Access Key
Get the Access Key of Baidu AI Cloud before using SDK, SEE Get AK/SK.
Call the static method setAK of BDCloudMediaPlayer to set ak:
BDCloudMediaPlayer.setAK(ak);
Initialization and Monitoring Settings
Initialize the playerBDCloudMediaPlayer
BDCloudMediaPlayer mMediaPlayer = new BDCloudMediaPlayer(this.getApplicationContext());
Set monitoring callback
//Callback when the player has parsed the play source format
mMediaPlayer.setOnPreparedListener(mPreparedListener);
//Call back when the width-height ratio of the video changes, and call back when parsing the width and height of the playback source for the first time.
mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
//Callback after finishing play
mMediaPlayer.setOnCompletionListener(mCompletionListener);
//Call back when an error occurs when playing
mMediaPlayer.setOnErrorListener(mErrorListener);
//Player information callback, such as starting and ending caches
mMediaPlayer.setOnInfoListener(mInfoListener);
//Callback overall loading progress, and it returns the percentage of the loading progress to the total video duration.
mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
//Seek adjusts the playback position quickly and call back after completion
mMediaPlayer.setOnSeekCompleteListener(mSeekCompleteListener);
//Call back metadata information in real time during the LVB of RTMP or HTTP-FLV
mMediaPlayer.setOnMetadataListener(mOnMetadataListener);
When playing the play source of the live, according to the different play protocols, the anchor may use onCompletion callback or onError if the stream is interrupted. In the program, you are required to check if the VJ host have completed the LVB, if not, the self-write listener will call start()
regularly to attempt to continue to pull traffic for playback.
Set the Playback Source and Display Target
Set play source and make the player resolve to be prepared for play
//If you want to set headers, you need to use setDataSource(String url, Map<String, String> headers) method
mMediaPlayer.setDataSource(strVideoUrl);
//The player only supports asynchronous preparations to call start() to start play after onPrepared callback
mMediaPlayer.prepareAsync();
Set the displayed object of play
Support to two setup methods setDisplay(SurfaceHolder holder)
/setSurface(Surface surface)
Set the timing in onSurfaceCreated/onSurfaceChanged callback function in general, and get the effective SurfaceHolder for setup.
public void surfaceCreated(SurfaceHolder holder) {
mMediaPlayer.setDisplay(holder);
}
Play Control
Start play
Starting play or resuming play all use start interface. When you call this interface, make sure that the player has called onPrepared.
mMediaPlayer.start();
Pause play
mMediaPlayer.pause();
Stop play
mMediaPlayer.stop();
The following calls can be made from stop to replay:
- Do not change the playback source: stop() --> prepareAsync() --> start() when prepared
- Change the paly source: stop() --> reset() --> setDataSource(url) --> prepareAsync() --> start() when prepared
Release player
mMediaPlayer.release();
After releasing, you need to create a new player to play again.
Obtaining of Properties
Get the AV length
Get the duration of audio and video, with the unit of millisecond.
long durationInMilliSeconds = mMediaPlayer.getDuration();
Get the current play position
Get the current play position, with the unit of millisecond.
long positionInMilliSeconds = mMediaPlayer.getCurrentPosition();
Get the video width and height
int videoWidth = mMediaPlayer.getVideoWidth();
int videoHeight = mMediaPlayer.getVideoHeight();
BDCloudVideoView Controls
BDCloudVideoView
packs BDCloudMediaPlayer
to let you use the controls directly to play the video. The class directly provides the source code, and you should copy the source code to your project if you use it. In most cases, you only need to use the control without modifying it, to avoid incompatibility with the new ‘BDCloudVideoView’ during updating.
The BDCloudVideoView interface is very similar to the system class VideoView interface, with the following features:
- With built-in video rending controls (TextureView/SurfaceView are optional), specify the mode of video rendering (clip, filling and bespread), avoid the problem of manipulating mediaPlayer directly to render views;
- Simplify the MediaPlayer control interface. Use
new BDCloudVideoView --> setVideoPath --> start
for quick playback; - The parameter setting interfaces nearly the same as BDCloudMediaPlayer have been packed. Using this control, you don
t have to interact with ‘BDCloudMediaPlayer
;
BDCloudVideoView play control
Set a new playback source
The interface creates a brand new BDCloudMediaPlayer internal object to play the new source.
Note
Before stopPlayback() is called, the interface may not be called more than once to prevent object leakage.
bdCloudVideoView.setVideoPath(info.getUrl());
Start or resume play
bdCloudVideoView.start();
Pause play
bdCloudVideoView.pause(); //You can continue to call start() to resume playback later.
Stop play
The function releases the internal object of the current BDCloudMediaPlayer
bdCloudVideoView.stopPlayback();
Release all resources
The function releases the objects including the internal object and the rendering target of the current BDCloudMediaPlayer. Must be called when the bdCloudVideoView object is no longer used.
bdCloudVideoView.release();
Set parameters related to play
BDCloudVideoView
packs nearly all the parameter setting interfaces of BDCloudMediaPlayer
with the same names.
Note
Please call the following interface before
bdCloudVideoView.setVideoPath
because you need to use the settings of these parameters when initializingBDCloudMediaPlayer
inBDCloudMediaPlayer
.
For example:
bdCloudVideoView.setLogEnabled(boolean enabled); //Whether to display logs
bdCloudVideoView.setDecodeMode(int decodeMode); //Set decoding mode
bdCloudVideoView.setInitPlayPosition(long milliSeconds); //Set the initial playback position
bdCloudVideoView.setUseApmDetect(boolean useApmDetect); //Whether to enable APM detection; if enable, APM SDK should be additionally inserted
bdCloudVideoView.setMaxProbeTime(int maxProbeTimeInMs); //Set the maximum detection duration
bdCloudVideoView.setMaxProbeSize(int maxProbeSizeInBytes); //Set the data size of the maximum detection
bdCloudVideoView.setMaxCacheSizeInBytes(int sizeInBytes); //Set maximum size of the cache data
bdCloudVideoView.setLooping(boolean isLoop); //Loop playback or not
bdCloudVideoView.setBufferTimeInMs(int milliSeconds); //Set the duration of the data cache when “loading” is triggered
In addition, you can also set the display mode for BDCloudVideoView
:
// VIDEO_SCALING_MODE_SCALE_TO_FITFill, comply with width-height proportion, with black borders
// VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPINGCropping, follow the aspect ratio and no black edges, but the video edges may be cropped.
// VIDEO_SCALING_MODE_SCALE_TO_MATCH_PARENTBespreading, bespread the display area without respect to width-to-height ratio
bdVideoView.setVideoScalingMode(BDCloudVideoView.VIDEO_SCALING_MODE_SCALE_TO_FIT);
Specify to use TextureView or SurfaceView
- TextureView is introduced in Android 4.0 and can actually be used in Android 4.1 (setSurfaceTexture interface is added in 4.1 to avoid blank screen). The control supports features such as animation, rotation and screenshots. If your player is in ListView or ScrollView, TextureView is the best choice, otherwise there may be edge flashing when scrolling. However, the shortcoming lies in that its performance is inferior to SurfaceView.
- SurfaceView has been introduced in Android 1.0, but its disadvantage is that rotation of animation is not supported, which is not suitable for the viewgroup with a scroll bar. But the advantage is to have better performance, and SurfaceView is the first choice when playing the video for a long time.
BDCloudVideoView uses TextureView in priority by default (meaning: TextureView is used in 4.1 and above versions, while SurfaceView is used in the previous versions).
If you want to set the SurfaceView for the best performance and power consumption regardless of the system version, the second parameter of the following constructor function may be set as false:
bdCloudVideoView = new BDCloudVideoView(Context context, boolean useTextureViewFirst);
Customize the interface prompt of “Loading”
The default loading prompt is “a loading circle” + “buffering...”.
If you want to custom the [Loading] interface and prompt information, you need to call the following codes:
bdCloudVideoView.showCacheInfo(false);
Then, the user can write relevant controls at App level, and controls the display and hidden of the controls by themselves.
Use BDCloudVideoView Instance in the Project
Initialize to set play source and activate
BDCloudVideoView.setAK(ak);
bdVideoView = new BDCloudVideoView(this);
/**
*Register listener
*/
bdVideoView.setOnPreparedListener(this);
bdVideoView.setOnCompletionListener(this);
bdVideoView.setOnErrorListener(this);
bdVideoView.setOnInfoListener(this);
bdVideoView.setOnBufferingUpdateListener(this);
bdVideoView.setOnPlayerStateListener(this);
bdVideoView.setVideoScalingMode(BDCloudVideoView.VIDEO_SCALING_MODE_SCALE_TO_FIT);
RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(-1, -1);
rllp.addRule(RelativeLayout.CENTER_IN_PARENT);
mViewHolder.addView(bdVideoView, rllp);
//Please refer to AdvancedMediaController in demo
mediaController.setMediaPlayerControl(bdVideoView);
bdVideoView.setVideoPath(info.getUrl());
//No interfaces related to prepare in BDCloudVideoView, so immediately call start after allowing setVideoPath.
bdVideoView.start();
Stop and release all the resources of BDCloudVideoView
Note: Please call this interface for sure when you do not use VideoView.
bdVideoView.stopPlayback();
bdVideoView.release();
Play the next new video source in the same interface
bdVideoView.stopPlayback(); //Release the last video source
bdVideoView.reSetRender(); //Clean the remained frame of the last play source
bdVideoView.setVideoPath(info.getUrl());
bdVideoView.start();
The link to the playing of the same video with different resolutions on the same interface
It applies to when your video with different resolution has different playback links. If videos with different definition are in the same link (i.e. Master m3u8 file), please see “Multi-code rate switch" in Advanced.
bdVideoView.stopPlayback(); //Release the last video source
bdVideoView.setInitPlayPosition(1 *1000); //Specify the initial playback position in milliseconds.
bdVideoView.setVideoPath(info.getUrl());
bdVideoView.start();
See AdvancedPlayActivity.java or SimplePlayActivity.java in demo for code details