1.在生命周期里面的第一個方法,例如onCreate里面設置屏幕的樣式
//表示將無邊緣的填充整個播放界面
mCurrentRenderMode = TXLiveConstants.RENDER_MODE_FULL_FILL_SCREEN;
mCurrentRenderRotation = TXLiveConstants.RENDER_ROTATION_PORTRAIT;
2.完成對配置的初始化:
mPlayConfig = new TXLivePlayConfig();
if (mLivePlayer == null) {
mLivePlayer = new TXLivePlayer(LiveActivity.this);
}
3.完成緩存策略
public void setCacheStrategy(int nCacheStrategy) {
if (mCacheStrategy == nCacheStrategy) return;
mCacheStrategy = nCacheStrategy;
switch (nCacheStrategy) {
case CACHE_STRATEGY_FAST:
mPlayConfig.setAutoAdjustCacheTime(true);
mPlayConfig.setMaxAutoAdjustCacheTime(CACHE_TIME_FAST);
mPlayConfig.setMinAutoAdjustCacheTime(CACHE_TIME_FAST);
mLivePlayer.setConfig(mPlayConfig);
break;
case CACHE_STRATEGY_SMOOTH:
mPlayConfig.setAutoAdjustCacheTime(false);
mPlayConfig.setCacheTime(CACHE_TIME_SMOOTH);
mLivePlayer.setConfig(mPlayConfig);
break;
case CACHE_STRATEGY_AUTO:
mPlayConfig.setAutoAdjustCacheTime(true);
mPlayConfig.setMaxAutoAdjustCacheTime(CACHE_TIME_SMOOTH);
mPlayConfig.setMinAutoAdjustCacheTime(CACHE_TIME_FAST);
mLivePlayer.setConfig(mPlayConfig);
break;
default:
break;
}
}
4.進行播放的基本設置:
mPlayerView = (TXCloudVideoView) findViewById(R.id.video_view);
mPlayerView.disableLog(true);
mLivePlayer.setConfig(mPlayConfig);
// 硬件加速在1080p解碼場景下效果顯著,但細節之處并不如想象的那么美好:
// (1) 只有 4.3 以上android系統才支持
// (2) 兼容性我們目前還僅過了小米華為等常見機型,故這里的返回值您先不要太當真
mLivePlayer.enableHardwareDecode(mHWDecode);
mLivePlayer.setRenderRotation(mCurrentRenderRotation);
mLivePlayer.setRenderMode(mCurrentRenderMode);
5.開始播放:
private boolean startPlayRtmp() {
if (!checkPlayUrl(playUrl)) {
return false;
}
mLivePlayer.setPlayerView(mPlayerView);
mLivePlayer.setPlayListener(this);
// 硬件加速在1080p解碼場景下效果顯著,但細節之處并不如想象的那么美好:
// (1) 只有 4.3 以上android系統才支持
// (2) 兼容性我們目前還僅過了小米華為等常見機型,故這里的返回值您先不要太當真
mLivePlayer.enableHardwareDecode(mHWDecode);
mLivePlayer.setRenderRotation(mCurrentRenderRotation);
mLivePlayer.setRenderMode(mCurrentRenderMode);
//設置播放器緩存策略
//這里將播放器的策略設置為自動調整,調整的范圍設定為1到4s,您也可以通過setCacheTime將播放器策略設置為采用
//固定緩存時間。如果您什么都不調用,播放器將采用默認的策略(默認策略為自動調整,調整范圍為1到4s)
//mLivePlayer.setCacheTime(5);
mLivePlayer.setConfig(mPlayConfig);
int result = mLivePlayer.startPlay(playUrl, mPlayType); // result返回值:0 success; -1 empty url; -2 invalid url; -3 invalid playType;
if (result == -2) {
Toast.makeText(this, "非騰訊云鏈接地址,若要放開限制,請聯系騰訊云商務團隊", Toast.LENGTH_SHORT).show();
}
if (result != 0) {
return false;
}
mVideoPlay=true;
return true;
}
播放前校驗播放地址:
private boolean checkPlayUrl(final String playUrl) {
if (TextUtils.isEmpty(playUrl) || (!playUrl.startsWith("http://") && !playUrl.startsWith("https://") && !playUrl.startsWith("rtmp://") && !playUrl.startsWith("/"))) {
Toast.makeText(this, "播放地址不合法,目前僅支持rtmp,flv,hls,mp4播放方式和本地播放方式(絕對路徑,如\"/sdcard/test.mp4\")!", Toast.LENGTH_SHORT).show();
return false;
}
if (playUrl.startsWith("rtmp://")) {
mPlayType = TXLivePlayer.PLAY_TYPE_LIVE_RTMP;
} else if ((playUrl.startsWith("http://") || playUrl.startsWith("https://")) && playUrl.contains(".flv")) {
mPlayType = TXLivePlayer.PLAY_TYPE_LIVE_FLV;
} else {
Toast.makeText(this, "播放地址不合法,直播目前僅支持rtmp,flv播放方式!", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
6.進行播放必須實現ITXLivePlayListener接口,里面會有兩個回調方法,onPlayEvent是管理播放進度和狀態,onNetStatus是監聽網絡狀態的
@Override
public void onPlayEvent(int event, Bundle param) {
if (event == TXLiveConstants.PLAY_EVT_PLAY_BEGIN) {
stopLoadingAnimation();
}
else if (event == TXLiveConstants.PLAY_ERR_NET_DISCONNECT) {
stopPlayRtmp();
}else if (event == TXLiveConstants.PLAY_EVT_PLAY_END) {
stopPlayRtmp();
}else if (event == TXLiveConstants.PLAY_EVT_PLAY_LOADING){
startLoadingAnimation();
}
String msg = param.getString(TXLiveConstants.EVT_DESCRIPTION);
appendEventLog(event, msg);
if (event < 0) {
Toast.makeText(this, param.getString(TXLiveConstants.EVT_DESCRIPTION), Toast.LENGTH_SHORT).show();
}
else if (event == TXLiveConstants.PLAY_EVT_PLAY_BEGIN) {
stopLoadingAnimation();
}
}
@Override
public void onNetStatus(Bundle bundle) {
}
注意demo中的usersigner需要你自己生成,該usersigner有效期到2017年10月,播放playurl需要根據騰訊云視頻的推流地址進行修改。具體可以參考云通信接入文檔,或我的上一篇文章《騰訊云視頻接入初探》,地址http://www.lxweimin.com/p/449205fbde8b?utm_campaign=haruki&utm_content=note&utm_medium=reader_share&utm_source=weixin_timeline&from=timeline
demo下載
具體效果如圖:
