Android TV开发总结(六)构建一个TV app的直播节目实例
请尊重分享成果,转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52966319
近年来,Android TV的迅速发展,传统的有线电视受到较大的冲击,在TV上用户同样也可以看到各个有线电视的直播频道,相对于手机,这种直播节目,体验效果更佳,尤其是一样赛事节目,大屏幕看得才够痛快,还可以邀几好友一起欣赏。今天将介绍构建一个TV app的直播节目实例,此实例上传到Github: https://github.com/hejunlin2013/LivePlayback 喜欢可以star。Agenda如下:
- 效果图
- 代码实现:
- 主页面:Recycleview对应Adapater
- 直播节目源
- 播放器
- 播放页处理
- 播放页的播放panel:
先看下效果图:
主界面:
CCTV-1:
湖南卫视:
CCTV-第一剧场:
CCTV-15(音乐):
CCTV-14(少儿):
CCTV-13(新闻):
CCTV-12(社会与法):
CCTV-11(戏曲):
CCTV-10(科教):
CCTV-9(纪录):
CCTV-8(电视剧):
CCTV-第一剧场:
CCTV-15:
请尊重分享成果,转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52966319
代码实现:
- 主页面:Recycleview对应adapater
- 直播节目源
- 播放器
- 播放页处理
主页面:
/*
* 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("节目加载中...");
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("节目暂时不能播放")
.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,从主页面传url到播放页面,关才mediaplayer相关,之前专门写了专题分析,mediaplayer的状态可参考《Android Multimedia框架总结(一)MediaPlayer介绍之状态图及生命周期》
第三方播放器典型特点就是另起一个mediaplayerservice,注意这是另外一个进程,为什么是另一个进程,可参见我的文章: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开发总结(六)构建一个TV app的直播节目实例"
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>
这里有几个点要注意 :
- 为演示,并未对层级进行使用FrameLayout,及viewstub,include等性能优化相关的,在实际商用项目中,建议写xml文件,尽可能遵循过少的层级,高级标签及FrameLayout等技巧。
- 所有的size切勿直接写死,用 android:layout_marginTop=”@dimen/dimen_20dp”表示,string值统一写到string.xml中,这些基本的规范,会让你提高不少效率。
第一时间获得博客更新提醒,以及更多android干货,源码分析,欢迎关注我的微信公众号,扫一扫下方二维码或者长按识别二维码,即可关注。
如果你觉得好,随手点赞,也是对笔者的肯定,也可以分享此公众号给你更多的人,原创不易
Android TV开发总结(六)构建一个TV app的直播节目实例的更多相关文章
- Android TV开发总结(三)构建一个TV app的焦点控制及遇到的坑
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52835829 前言:上篇中,&l ...
- Android TV开发总结(七)构建一个TV app中的剧集列表控件
原文:Android TV开发总结(七)构建一个TV app中的剧集列表控件 版权声明:我已委托"维权骑士"(rightknights.com)为我的文章进行维权行动.转载务必转载 ...
- 《Android Studio开发实战 从零基础到App上线》资源下载和内容勘误
转载于:https://blog.csdn.net/aqi00/article/details/73065392 资源下载 下面是<Android Studio开发实战 从零基础到App上线&g ...
- Android TV开发总结(四)通过RecycleView构建一个TV app列表页(仿腾讯视频TV版)
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52854131 前言:昨晚看锤子手 ...
- Android TV开发总结(一)构建一个TV app前要知道的事儿
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52792562 前言:近年来,智能 ...
- Android TV开发总结(五)TV上屏幕适配总结
前言:前面几篇总结一些TV上的小Sample,开源到GitHub:https://github.com/hejunlin2013/TVSample, 点击链接,可以持续关注.今天总结下TV上屏幕适配. ...
- 安卓TV开发(六) 移动智能终端UI之实现类似GridView的焦点控制FocusView框架
转载请标明出处:http://blog.csdn.net/sk719887916/article/details/40045089,作者:skay 前言 安卓TV开发(五) 移动智能终端UI之实现主流 ...
- 【Android Developers Training】 3. 构建一个简单UI
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android开发】找乐,一个笑话App的制作过程记录
缘起 想做一个笑话App的原因是因为在知乎上看过一个帖子,做Android可以有哪些数据可以练手,里面推荐了几个数据开放平台.在这些平台中无一不是有公共的笑话接口,当时心想这个可以拿来练手啊,还挺有意 ...
随机推荐
- socket编程实例
TCP/IP层次模型 当然这里我们只讨论重要的四层 01,应用层(Application):应用层是个很广泛的概念,有一些基本相同的系统级TCP/IP应用以及应用协议,也有许多的企业应用和互联网应用. ...
- [ABP]浅谈模块系统与 ABP 框架初始化
在 ABP 框架当中所有库以及项目都是以模块的形式存在,所有模块都是继承自AbpModule 这个抽象基类,每个模块都拥有四个生命周期.分别是: PreInitialze(); Initialize( ...
- [LeetCode] Find Bottom Left Tree Value 寻找最左下树结点的值
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- Caffe+VS2015+python3的安装(基于windows)
在网上找了许多安装Caffe的教程 感觉全都是杂乱无章的 而且也没有详细的 只能自己当小白鼠来实验一次了 本次配置:CUDA 8.0+ CUDNN +VS 2015 +Python 3.5 + Ca ...
- bzoj4830 hnoi2017 抛硬币
题目描述 小 A 和小 B 是一对好朋友,他们经常一起愉快的玩耍.最近小 B 沉迷于**师手游,天天刷本,根本无心搞学习.但是已经入坑了几个月,却一次都没有抽到 SSR,让他非常怀疑人生.勤勉的小 A ...
- BZOJ4727 [POI2017]Turysta
这题太神了还是去看刺儿神题解吧. http://www.cnblogs.com/neighthorn/p/6538364.html #include <cstdio> #include & ...
- 基于Mapxtreme for JAVA的电子地图设计与实现
基于Mapxtreme for JAVA的电子地图设计与实现学生毕业设计,适合测绘类专业研究目标: 开发一个基于MapXtreme for JAVA的校园电子地图项目,使用MapInfo ...
- A TensorBoard plugin for visualizing arbitrary tensors in a video as your network trains.Beholder是一个TensorBoard插件,用于在模型训练时查看视频帧。
Beholder is a TensorBoard plugin for viewing frames of a video while your model trains. It comes wit ...
- 值得珍藏的HTTP协议详解
转自:http://www.cnblogs.com/li0803/archive/2008/11/03/1324746.html 引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式 ...
- JSTL标签四种判断语句的用法
一.条件运算符 ${user.gender==1?'男':'女'} 二.if() <c:if test="${2>1}">code..</c:if> ...