android tv 云播放器,Android TV开发总结(六)构建一个TV app的直播节目实例
近年來(lái),Android TV的迅速發(fā)展,傳統(tǒng)的有線電視受到較大的沖擊,在TV上用戶同樣也可以看到各個(gè)有線電視的直播頻道,相對(duì)于手機(jī),這種直播節(jié)目,體驗(yàn)效果更佳,尤其是一樣賽事節(jié)目,大屏幕看得才夠痛快,還可以邀幾好友一起欣賞。今天將介紹構(gòu)建一個(gè)TV app的直播節(jié)目實(shí)例,此實(shí)例上傳到Github: https://github.com/hejunlin2013/LivePlayback 喜歡可以star。Agenda如下:
效果圖
代碼實(shí)現(xiàn):
主頁(yè)面:Recycleview對(duì)應(yīng)Adapater
直播節(jié)目源
播放器
播放頁(yè)處理
播放頁(yè)的播放panel:
先看下效果圖:主界面:
這里寫圖片描述
這里寫圖片描述
CCTV-1:
這里寫圖片描述湖南衛(wèi)視:
這里寫圖片描述CCTV-第一劇場(chǎng):
這里寫圖片描述CCTV-15(音樂(lè)):
這里寫圖片描述
CCTV-14(少兒):
這里寫圖片描述CCTV-13(新聞):
這里寫圖片描述CCTV-12(社會(huì)與法):
這里寫圖片描述CCTV-11(戲曲):
這里寫圖片描述CCTV-10(科教):
這里寫圖片描述CCTV-9(紀(jì)錄):
這里寫圖片描述CCTV-8(電視劇):
這里寫圖片描述CCTV-第一劇場(chǎng):
這里寫圖片描述CCTV-15:
這里寫圖片描述
代碼實(shí)現(xiàn):
主頁(yè)面:Recycleview對(duì)應(yīng)adapater
直播節(jié)目源
播放器
播放頁(yè)處理主頁(yè)面:
/*
* Copyright (C) 2016 hejunlin
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class MainActivity extends Activity {
private MetroViewBorderImpl mMetroViewBorderImpl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMetroViewBorderImpl = new MetroViewBorderImpl(this);
mMetroViewBorderImpl.setBackgroundResource(R.drawable.border_color);
loadRecyclerViewMenuItem();
}
private void loadRecyclerViewMenuItem() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.ry_menu_item);
GridLayoutManager layoutManager = new GridLayoutManager(this, 1);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setFocusable(false);
mMetroViewBorderImpl.attachTo(recyclerView);
createOptionItemData(recyclerView, R.layout.detail_menu_item);
}
private void createOptionItemData(RecyclerView recyclerView, int id) {
OptionItemAdapter adapter = new OptionItemAdapter(this, id);
recyclerView.setAdapter(adapter);
recyclerView.scrollToPosition(0);
}
}
播放頁(yè):
/*
* Copyright (C) 2016 hejunlin
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class LiveActivity extends Activity {
private IjkVideoView mVideoView;
private RelativeLayout mVideoViewLayout;
private RelativeLayout mLoadingLayout;
private TextView mLoadingText;
private TextView mTextClock;
private String mVideoUrl = "";
private int mRetryTimes = 0;
private static final int CONNECTION_TIMES = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live);
mVideoUrl = getIntent().getStringExtra("url");
mVideoView = (IjkVideoView) findViewById(R.id.videoview);
mVideoViewLayout = (RelativeLayout) findViewById(R.id.fl_videoview);
mLoadingLayout = (RelativeLayout) findViewById(R.id.rl_loading);
mLoadingText = (TextView) findViewById(R.id.tv_load_msg);
mTextClock = (TextView)findViewById(R.id.tv_time);
mTextClock.setText(getDateFormate());
mLoadingText.setText("節(jié)目加載中...");
initVideo();
}
private String getDateFormate(){
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
return formattedDate;
}
public void initVideo() {
// init player
IjkMediaPlayer.loadLibrariesOnce(null);
IjkMediaPlayer.native_profileBegin("libijkplayer.so");
mVideoView.setVideoURI(Uri.parse(mVideoUrl));
mVideoView.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(IMediaPlayer mp) {
mVideoView.start();
}
});
mVideoView.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(IMediaPlayer mp, int what, int extra) {
switch (what) {
case IjkMediaPlayer.MEDIA_INFO_BUFFERING_START:
mLoadingLayout.setVisibility(View.VISIBLE);
break;
case IjkMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
case IjkMediaPlayer.MEDIA_INFO_BUFFERING_END:
mLoadingLayout.setVisibility(View.GONE);
break;
}
return false;
}
});
mVideoView.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(IMediaPlayer mp) {
mLoadingLayout.setVisibility(View.VISIBLE);
mVideoView.stopPlayback();
mVideoView.release(true);
mVideoView.setVideoURI(Uri.parse(mVideoUrl));
}
});
mVideoView.setOnErrorListener(new IMediaPlayer.OnErrorListener() {
@Override
public boolean onError(IMediaPlayer mp, int what, int extra) {
if (mRetryTimes > CONNECTION_TIMES) {
new AlertDialog.Builder(LiveActivity.this)
.setMessage("節(jié)目暫時(shí)不能播放")
.setPositiveButton(R.string.VideoView_error_button,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
LiveActivity.this.finish();
}
})
.setCancelable(false)
.show();
} else {
mVideoView.stopPlayback();
mVideoView.release(true);
mVideoView.setVideoURI(Uri.parse(mVideoUrl));
}
return false;
}
});
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
if (!mVideoView.isBackgroundPlayEnabled()) {
mVideoView.stopPlayback();
mVideoView.release(true);
mVideoView.stopBackgroundPlay();
}
IjkMediaPlayer.native_profileEnd();
}
public static void activityStart(Context context, String url) {
Intent intent = new Intent(context, LiveActivity.class);
intent.putExtra("url", url);
context.startActivity(intent);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}
播放器是用二次封裝的ijkplayer,從主頁(yè)面?zhèn)鱱rl到播放頁(yè)面,關(guān)才mediaplayer相關(guān),之前專門寫了專題分析,mediaplayer的狀態(tài)可參考《Android Multimedia框架總結(jié)(一)MediaPlayer介紹之狀態(tài)圖及生命周期》
第三方播放器典型特點(diǎn)就是另起一個(gè)mediaplayerservice,注意這是另外一個(gè)進(jìn)程,為什么是另一個(gè)進(jìn)程,可參見(jiàn)我的文章:MediaPlayer的C/S模型。對(duì)于ijkplayer這個(gè)框架,因?yàn)樽鰧?shí)例,才引入,不做評(píng)價(jià),也不會(huì)去深究,滿足基本播放需求就ok。市場(chǎng)上有很多第三方播放框架,ijkplayer,vitamio,百度云播放等。
再看下播放頁(yè)的播放panel:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#22000000"
android:orientation="vertical">
android:id="@+id/fl_videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlack">
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@color/colorBlack">
android:id="@+id/rl_loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#de262a3b">
android:id="@+id/tv_load_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pb_loading"
android:layout_centerInParent="true"
android:layout_marginTop="6dp"
android:textColor="#ffffff"
android:textSize="16sp" />
android:id="@+id/pb_loading"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:layout_marginTop="60dp"
android:indeterminate="false"
android:indeterminateDrawable="@drawable/video_loading"
android:padding="5dp" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/player_panel_background_color">
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:textSize="24dp"
android:text="Android TV開(kāi)發(fā)總結(jié)(六)構(gòu)建一個(gè)TV app的直播節(jié)目實(shí)例"
android:layout_centerVertical="true"
android:layout_marginTop="18dp"
android:textColor="@color/white"/>
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:textSize="20dp"
android:layout_toRightOf="@id/tv_title"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:textColor="@color/white"/>
這里有幾個(gè)點(diǎn)要注意:
為演示,并未對(duì)層級(jí)進(jìn)行使用FrameLayout,及viewstub,include等性能優(yōu)化相關(guān)的,在實(shí)際商用項(xiàng)目中,建議寫xml文件,盡可能遵循過(guò)少的層級(jí),高級(jí)標(biāo)簽及FrameLayout等技巧。
所有的size切勿直接寫死,用 android:layout_marginTop="@dimen/dimen_20dp"表示,string值統(tǒng)一寫到string.xml中,這些基本的規(guī)范,會(huì)讓你提高不少效率。
總結(jié)
以上是生活随笔為你收集整理的android tv 云播放器,Android TV开发总结(六)构建一个TV app的直播节目实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php实现验证码正确输入_PHP实现验证
- 下一篇: 情人节程序员用HTML网页表白【粒子告白