近年來,Android TV的迅速發(fā)展跟衅,傳統(tǒng)的有線電視受到較大的沖擊孵睬,在TV上用戶同樣也可以看到各個有線電視的直播頻道,相對于手機(jī)伶跷,這種直播節(jié)目掰读,體驗效果更佳,尤其是一樣賽事節(jié)目叭莫,大屏幕看得才夠痛快蹈集,還可以邀幾好友一起欣賞。今天將介紹構(gòu)建一個TV app的直播節(jié)目實例,此實例上傳到Github: https://github.com/hejunlin2013/LivePlayback 喜歡可以star雇初。Agenda如下:
- 效果圖
- 代碼實現(xiàn):
- 主頁面:Recycleview對應(yīng)Adapater
- 直播節(jié)目源
- 播放器
- 播放頁處理
- 播放頁的播放panel:
代碼實現(xiàn):
- 主頁面:Recycleview對應(yīng)adapater
- 直播節(jié)目源
- 播放器
- 播放頁處理主頁面:
/*
* Copyright (C) 2016 hejunlin <hejunlin2013@gmail.com>
* 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);
}
}
播放頁:
/*
* Copyright (C) 2016 hejunlin <hejunlin2013@gmail.com>
* 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é)目暫時不能播放")
.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,從主頁面?zhèn)鱱rl到播放頁面抵皱,關(guān)才mediaplayer相關(guān)善榛,之前專門寫了專題分析,mediaplayer的狀態(tài)可參考《Android Multimedia框架總結(jié)(一)MediaPlayer介紹之狀態(tài)圖及生命周期》
第三方播放器典型特點就是另起一個mediaplayerservice呻畸,注意這是另外一個進(jìn)程米辐,為什么是另一個進(jìn)程,可參見我的文章:MediaPlayer的C/S模型侠坎。對于ijkplayer這個框架杀餐,因為做實例据途,才引入,不做評價叙甸,也不會去深究颖医,滿足基本播放需求就ok。市場上有很多第三方播放框架裆蒸,ijkplayer,vitamio,百度云播放等熔萧。
再看下播放頁的播放panel:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#22000000"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/fl_videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlack">
<com.hejunlin.liveplayback.ijkplayer.media.IjkVideoView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@color/colorBlack">
</com.hejunlin.liveplayback.ijkplayer.media.IjkVideoView>
<RelativeLayout
android:id="@+id/rl_loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#de262a3b">
<TextView
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" />
<ProgressBar
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" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/player_panel_background_color">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:textSize="24dp"
android:text="Android TV開發(fā)總結(jié)(六)構(gòu)建一個TV app的直播節(jié)目實例"
android:layout_centerVertical="true"
android:layout_marginTop="18dp"
android:textColor="@color/white"/>
<TextView
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"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
這里有幾個點要注意:
- 為演示,并未對層級進(jìn)行使用FrameLayout,及viewstub,include等性能優(yōu)化相關(guān)的僚祷,在實際商用項目中佛致,建議寫xml文件,盡可能遵循過少的層級辙谜,高級標(biāo)簽及FrameLayout等技巧俺榆。
- 所有的size切勿直接寫死,用 android:layout_marginTop="@dimen/dimen_20dp"表示装哆,string值統(tǒng)一寫到string.xml中罐脊,這些基本的規(guī)范,會讓你提高不少效率蜕琴。