Quick Advancement
Play Control Bar
Simple play control bar can see SimpleMediaController class of demo. The class consists of play button and play progress bar. The related interfaces are:
long getDuration()
Get video duration, with the unit of millisecondlong getCurrentPosition
Get the current play position, with the unit of millisecondvoid seekTo(long milliSecond)
Switch to playback somewherestart()
Start playpause()
Pause playOnBufferingUpdateListener
Return the percentage of cached duration to the video playback duration during monitoring and callback, and update the secondary progress bar (cache progress) according to this value.boolean isPlaying()
Whether it is playing
Advanced Setting Interface
BDCloudMediaPlayerProvide multiple advanced setting interfaces:
setBufferSizeInBytes(int size)
The starting play data length during the process of setting buffering; meaning when the [loading] buffering byte number reaches this size, callback BUFFERING_END; the default value is 1*1024*1024 (1M) and the upper limit is 4M;setBufferTimeInMs(int time)
The starting play data duration during the process of setting buffering, with the unit of millisecond; meaning when the [loading] buffering data playable duration reaches this time, callback BUFFERING_END; related to the above setting function, subject to the first arrival one; the default value is 1*1000 (1 second), and the upper limit is 4 seconds.setDecodeMode(int mode)
Set the decoding mode, which can be set as DECODE_AUTO (hardware decoding in priority, and then software decoding) or DECODE_SW (software decoding), with DECODE_AUTO by default;setLogEnabled(boolean enable)
Set to activate the player Logcat output, which is false by default; when releasing the application, the value should be false;setMaxCacheSizeInBytes(int size)
Set the maximum cache zone size; 5*1024*1024 (5M) by default, and the upper limit is 5M. The downloaded data is ahead of the size of the played data.setMaxProbeSize(int maxProbeSize)
Set the size of the maximum video detection probe; the default value is 1*1024*1024 (1M) and the upper limit is 4M;setMaxProbeTime(int maxProbeTime)
Set the length of the maximum video detection probe, with the unit of millisecond; the default value is 1*1000 (1 second) and the upper limit is 4 seconds.setVolume(float leftVolume, float rightVolume)
Set the volume of the left and right sound trackssetInitPlayPosition(long positionInMilliSeconds)
Set the initial playback positionsetTimeoutInUs(int timeout)
Set the timeout value when during the connection establishment and data download, in microsecondsetAudioDataCallback(OnReceiveAudioDataCallback callback)
Set the playing audio data callback
Multi-rate Switchover
When an HLS multi-code rate video is played, the player supports to switch the code rate in real time during the playback.
Get the index number of the multi-rate video (Master M3U8)
String[] getVariantInfo()
- The variantinfo takes value directly from Master m3u8 file, and based on (#EXT-X-STREAM-INF) different format, the value taken can be:
1920x1080,3541000
may also be, 232370
(Nonstandard video). Before the comma is resolution ratio and after the comma is code rate; - The array size is the multi-rate number;
Select multiple code rate
Select multi-rate when playing
mMediaPlayer.selectResolutionByIndex(index);
The function allows the changes inside the player stop --> prepareAsync --> prepared
.
BDCloudMediaPlayer also releases selectVariantByIndex interface. The interface doesn’t maintain the mediaplayer status If you want to use such interface, you shall make sure that it is called before prepareAsync.
Multi-rate Seamless Switchover
The player supports not only HLS multi-rate rapid switchover but also the seamless switchover feature of mainstream media formats such as HLS and MAP4. Specific steps are as follows:
HLS format multi-rate seamless switching of the Master playlist
- Set the input format mMediaPlayer.setMediaInputType(mode);
- After PlayerState.STATE_PREPARED status, call mMediaPlayer.getMediaItems() to get multi-rate information list and update UI.
- The user switches to certain code rate according to the corresponding index: mMediaPlayer.selectMediaByIndex(index).
- When you receive IMediaPlayer.MEDIA_INFO_MEDIA_CHANGE_START notification, it means the video begins switching.
- When receiving IMediaPlayer.MEDIA_INFO_MEDIA_CHANGE_END notification, it means the switchover is over and the extra value can be used for judging whether the switchover is successful. After playing the player cache, the new code rate display is rapidly switched.
- If you receive onVideoSizeChanged(...) callback, it means the resolution rate of the video has changed.
Seamless switching of the non-nested code rate as MP4
- Set the input format mMediaPlayer.setMediaInputType(mode).
- Set multi-rate video link (please notice the difference here from HLS) mMediaPlayer.setMediaItems(mediaItems).
- After the PlayerState.STATE_PREPARED status, call mVideoView.getMutilMediasDsc() to access the list of multiple code information, and update UI.
- The user switches to certain code rate according to the corresponding index mMediaPlayer.selectMediaByIndex(index).
- When you receive IMediaPlayer.MEDIA_INFO_MEDIA_CHANGE_START notification, it means the video begins switching.
- When receiving IMediaPlayer.MEDIA_INFO_MEDIA_CHANGE_END notification, it means the switchover is over and the extra value can be used for judging whether the switchover is successful. After playing the player cache, the new code rate display is rapidly switched.
- If you receive onVideoSizeChanged(...) callback, it means the resolution rate of the video has changed.
Play the HLS Encrypted Video
Baidu Media Cloud MCT service and VOD service support decoding to be HLS encrypted videos.
With different encryption methods, the method for playing is slightly different:
- PlayerBinding encryption mode Play as a common video, and Baidu player must be used(Web, Android, iOS).
-
Token mode:(Recommended)
Token mode is the updated version of PlayerBinding mode, which can play safer according to the appointed token. <br/> Token is generated by the cooperation of your server and Baidu AI Cloud, and you just set the player after your App getting the token from your server. Set `token` and then prepare:
mMediaPlayer.setDecryptTokenForHLS(mDrmToken);
HLS Video Download
The player supports download and management for M3U8 videos.
Download manager
Download manager is provided as the form of singe instance:
globalVideoDownloadManager = VideoDownloadManager.getInstance(context, userName);
The username is only used for dividing the download records, which is not necessarily the actual username and can be the encryption string or signature string of an actual username.
Download steps
1.Call globalVideoDownloadManager = VideoDownloadManager.getInstance(context, userName)
to get the download management single instance.
2.Call globalVideoDownloadManager.startOrResumeDownloader(url, observer)
to start the download tsk.
Function prototype:
public void startOrResumeDownloader(String url, DownloadObserver observer)
public void startOrResumeDownloaderWithToken(String url, String token, DownloadObserver observer) //Download the encrypted videos
Later, you can also call pause ( `pauseDownloader `), resume ( `startOrResumeDownloader `), delete ( `deleteDownloader `), stop all ( `stopAll `) and other interfaces to manage download tasks.
Among them, while enabling the download, if you want to monitor the download progress and status in real time, you can inherit `DownloadObserver` class and realize `update` methods, and transmit the instances of such class to `startOrResumeDownloader` to realize real-time monitoring. If you don’t want real-time monitoring, make the second parameter of `startOrResumeDownloader` method as null.
public void update(DownloadableVideoItem downloader)
3.Access download task information in real time
During the process of task downloading, call back `update(DownloadableVideoItem downloader)` to get real-time download status.
Downloader object can get the following methods:
Method | Description |
---|---|
getUrl() | Get single download url |
getLocalAbsolutePath() | Get the local absolute path of the downloaded file |
getProgress() | Get download status. |
getStatus() | Get download status(Enumeration type DownloadStatus) |
getErrorCode() | When the download status is Error, get the error code through the interface |
getFailReason() | Get the simple description of download failure or discontinuity |
Note: Error code such as DownloadableVideoItem.ERROR _CODE _INVALID _URL
Whereas, the error code when an error occurs is shown in the table below:
Error code | Description |
---|---|
ERROR_CODE_NO_ERROR | Error free |
ERROR_CODE_INVALID_URL | Address invalid |
ERROR_CODE_NETWORK_FAILED | Network problems |
ERROR_CODE_SDCARD_UNMOUNTED | Local storage problem |
ERROR_CODE_M3U8_\INVALID_FORMAT | m3u8 format problem |
ERROR_CODE_M3U8_SAVE_FAILED | m3u8 storage problem |
ERROR_CODE_M3U8_DRM_INVALID | Invalid key or token related to drm protection |
ERROR_CODE_TS_SAVE_FAILED | Fail to save ts files |
4.Look up the downloading task through url
Get the download task item through `downloadableVideoItem = globalVideoDownloadManager.getDownloadableVideoItemByUrl(url) `, so as to access the information in the item. <br/>
Get all the download units through `globalVideoDownloadManager.getAllDownloadableVideoItems()`, select the unfinished tasks and start resuming download (`startOrResumeDownloader(oneItem.getUrl(), null)`) to realize breakpoint resume.
5.After downloading successfully, DownloadableVideoItem#getLocalAbsolutePath
method gets the local video full path to send to the player for local play.
Caching While Playing
Agent buffer supports HLS and MP4 video cache
Set the video caching while playing steps
mProxyCacheManager = ProxyCacheManager.getInstance()
;Get the cache proxy manager.
String proxyPath = mProxyCacheManager.getProxyUrl(mAppContext, path)
;Get the play agent link.
mProxyCacheManager.setCacheAvailableListener(cacheListener)
;Set the cache progress listener.
setVideoURI(proxyPath)
; Set the agent link to the player
mProxyCacheManager.release()
;Unregister cache monitor to release resource.
For detailed code, please see ‘BDCloudVideoView.java’ in demo.
Record while playing
mRecordController = new RecordController();
Create the record controller.
mRecordController.init(mNewSurfaceTexture,RecordConstants.VIDEO_ENCODE_FRAME_RATE,RecordConstants.DEFAULT_BIT_RATE_GTE_API18);
Initialize the record controller.
mVideoRender.setRenderList(mRecordController.getRenderCallbackList());
Set the render callback.
mRecordController.setRecordSize(mWidth, mHeight);
Set the record size.
mRecordController.startRecord(filePath);
Start recording.
mRecordController.setRecordListener(listener);
Set the record listener.
mRecordController.stopRecord();
Stop recording.
For the detailed code, please see BDCloudView.java , TextureRenderView.java
。