请尊重分享成果,转载请注明出处: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
  • 直播节目源
  • 播放器
  • 播放页处理

主页面:

  1. /*
  2. * Copyright (C) 2016 hejunlin <hejunlin2013@gmail.com>
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. public class MainActivity extends Activity {
  16. private MetroViewBorderImpl mMetroViewBorderImpl;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. mMetroViewBorderImpl = new MetroViewBorderImpl(this);
  22. mMetroViewBorderImpl.setBackgroundResource(R.drawable.border_color);
  23. loadRecyclerViewMenuItem();
  24. }
  25. private void loadRecyclerViewMenuItem() {
  26. RecyclerView recyclerView = (RecyclerView) findViewById(R.id.ry_menu_item);
  27. GridLayoutManager layoutManager = new GridLayoutManager(this, 1);
  28. layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
  29. recyclerView.setLayoutManager(layoutManager);
  30. recyclerView.setFocusable(false);
  31. mMetroViewBorderImpl.attachTo(recyclerView);
  32. createOptionItemData(recyclerView, R.layout.detail_menu_item);
  33. }
  34. private void createOptionItemData(RecyclerView recyclerView, int id) {
  35. OptionItemAdapter adapter = new OptionItemAdapter(this, id);
  36. recyclerView.setAdapter(adapter);
  37. recyclerView.scrollToPosition(0);
  38. }
  39. }

播放页:

  1. /*
  2. * Copyright (C) 2016 hejunlin <hejunlin2013@gmail.com>
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. public class LiveActivity extends Activity {
  16. private IjkVideoView mVideoView;
  17. private RelativeLayout mVideoViewLayout;
  18. private RelativeLayout mLoadingLayout;
  19. private TextView mLoadingText;
  20. private TextView mTextClock;
  21. private String mVideoUrl = "";
  22. private int mRetryTimes = 0;
  23. private static final int CONNECTION_TIMES = 5;
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_live);
  28. mVideoUrl = getIntent().getStringExtra("url");
  29. mVideoView = (IjkVideoView) findViewById(R.id.videoview);
  30. mVideoViewLayout = (RelativeLayout) findViewById(R.id.fl_videoview);
  31. mLoadingLayout = (RelativeLayout) findViewById(R.id.rl_loading);
  32. mLoadingText = (TextView) findViewById(R.id.tv_load_msg);
  33. mTextClock = (TextView)findViewById(R.id.tv_time);
  34. mTextClock.setText(getDateFormate());
  35. mLoadingText.setText("节目加载中...");
  36. initVideo();
  37. }
  38. private String getDateFormate(){
  39. Calendar c = Calendar.getInstance();
  40. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  41. String formattedDate = df.format(c.getTime());
  42. return formattedDate;
  43. }
  44. public void initVideo() {
  45. // init player
  46. IjkMediaPlayer.loadLibrariesOnce(null);
  47. IjkMediaPlayer.native_profileBegin("libijkplayer.so");
  48. mVideoView.setVideoURI(Uri.parse(mVideoUrl));
  49. mVideoView.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
  50. @Override
  51. public void onPrepared(IMediaPlayer mp) {
  52. mVideoView.start();
  53. }
  54. });
  55. mVideoView.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
  56. @Override
  57. public boolean onInfo(IMediaPlayer mp, int what, int extra) {
  58. switch (what) {
  59. case IjkMediaPlayer.MEDIA_INFO_BUFFERING_START:
  60. mLoadingLayout.setVisibility(View.VISIBLE);
  61. break;
  62. case IjkMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
  63. case IjkMediaPlayer.MEDIA_INFO_BUFFERING_END:
  64. mLoadingLayout.setVisibility(View.GONE);
  65. break;
  66. }
  67. return false;
  68. }
  69. });
  70. mVideoView.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
  71. @Override
  72. public void onCompletion(IMediaPlayer mp) {
  73. mLoadingLayout.setVisibility(View.VISIBLE);
  74. mVideoView.stopPlayback();
  75. mVideoView.release(true);
  76. mVideoView.setVideoURI(Uri.parse(mVideoUrl));
  77. }
  78. });
  79. mVideoView.setOnErrorListener(new IMediaPlayer.OnErrorListener() {
  80. @Override
  81. public boolean onError(IMediaPlayer mp, int what, int extra) {
  82. if (mRetryTimes > CONNECTION_TIMES) {
  83. new AlertDialog.Builder(LiveActivity.this)
  84. .setMessage("节目暂时不能播放")
  85. .setPositiveButton(R.string.VideoView_error_button,
  86. new DialogInterface.OnClickListener() {
  87. public void onClick(DialogInterface dialog, int whichButton) {
  88. LiveActivity.this.finish();
  89. }
  90. })
  91. .setCancelable(false)
  92. .show();
  93. } else {
  94. mVideoView.stopPlayback();
  95. mVideoView.release(true);
  96. mVideoView.setVideoURI(Uri.parse(mVideoUrl));
  97. }
  98. return false;
  99. }
  100. });
  101. }
  102. @Override
  103. protected void onResume() {
  104. super.onResume();
  105. }
  106. @Override
  107. protected void onPause() {
  108. super.onPause();
  109. }
  110. @Override
  111. protected void onStop() {
  112. super.onStop();
  113. if (!mVideoView.isBackgroundPlayEnabled()) {
  114. mVideoView.stopPlayback();
  115. mVideoView.release(true);
  116. mVideoView.stopBackgroundPlay();
  117. }
  118. IjkMediaPlayer.native_profileEnd();
  119. }
  120. public static void activityStart(Context context, String url) {
  121. Intent intent = new Intent(context, LiveActivity.class);
  122. intent.putExtra("url", url);
  123. context.startActivity(intent);
  124. }
  125. @Override
  126. public void onConfigurationChanged(Configuration newConfig) {
  127. super.onConfigurationChanged(newConfig);
  128. }
  129. }

播放器是用二次封装的ijkplayer,从主页面传url到播放页面,关才mediaplayer相关,之前专门写了专题分析,mediaplayer的状态可参考《Android Multimedia框架总结(一)MediaPlayer介绍之状态图及生命周期》

第三方播放器典型特点就是另起一个mediaplayerservice,注意这是另外一个进程,为什么是另一个进程,可参见我的文章:MediaPlayer的C/S模型。对于ijkplayer这个框架,因为做实例,才引入,不做评价,也不会去深究,满足基本播放需求就ok。市场上有很多第三方播放框架,ijkplayer,vitamio,百度云播放等。

再看下播放页的播放panel:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#22000000"
  6. android:orientation="vertical">
  7. <RelativeLayout
  8. android:id="@+id/fl_videoview"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:background="@color/colorBlack">
  12. <com.hejunlin.liveplayback.ijkplayer.media.IjkVideoView
  13. android:id="@+id/videoview"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:layout_centerInParent="true"
  17. android:background="@color/colorBlack">
  18. </com.hejunlin.liveplayback.ijkplayer.media.IjkVideoView>
  19. <RelativeLayout
  20. android:id="@+id/rl_loading"
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent"
  23. android:background="#de262a3b">
  24. <TextView
  25. android:id="@+id/tv_load_msg"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_below="@+id/pb_loading"
  29. android:layout_centerInParent="true"
  30. android:layout_marginTop="6dp"
  31. android:textColor="#ffffff"
  32. android:textSize="16sp" />
  33. <ProgressBar
  34. android:id="@+id/pb_loading"
  35. android:layout_width="60dp"
  36. android:layout_height="60dp"
  37. android:layout_centerInParent="true"
  38. android:layout_marginTop="60dp"
  39. android:indeterminate="false" android:indeterminateDrawable="@drawable/video_loading"
  40. android:padding="5dp" />
  41. </RelativeLayout>
  42. <LinearLayout
  43. android:layout_width="match_parent"
  44. android:layout_height="wrap_content"
  45. android:background="@color/player_panel_background_color">
  46. <TextView
  47. android:id="@+id/tv_title"
  48. android:layout_width="wrap_content"
  49. android:layout_height="60dp"
  50. android:textSize="24dp"
  51. android:text="Android TV开发总结(六)构建一个TV app的直播节目实例"
  52. android:layout_centerVertical="true"
  53. android:layout_marginTop="18dp"
  54. android:textColor="@color/white"/>
  55. <TextView
  56. android:id="@+id/tv_time"
  57. android:layout_width="wrap_content"
  58. android:layout_height="60dp"
  59. android:textSize="20dp"
  60. android:layout_toRightOf="@id/tv_title"
  61. android:layout_alignParentRight="true"
  62. android:layout_centerVertical="true"
  63. android:layout_marginLeft="60dp"
  64. android:layout_marginTop="20dp"
  65. android:textColor="@color/white"/>
  66. </LinearLayout>
  67. </RelativeLayout>
  68. </RelativeLayout>

这里有几个点要注意 :

  • 为演示,并未对层级进行使用FrameLayout,及viewstub,include等性能优化相关的,在实际商用项目中,建议写xml文件,尽可能遵循过少的层级,高级标签及FrameLayout等技巧。
  • 所有的size切勿直接写死,用 android:layout_marginTop=”@dimen/dimen_20dp”表示,string值统一写到string.xml中,这些基本的规范,会让你提高不少效率。

第一时间获得博客更新提醒,以及更多android干货,源码分析,欢迎关注我的微信公众号,扫一扫下方二维码或者长按识别二维码,即可关注。





如果你觉得好,随手点赞,也是对笔者的肯定,也可以分享此公众号给你更多的人,原创不易

Android TV开发总结(六)构建一个TV app的直播节目实例的更多相关文章

  1. Android TV开发总结(三)构建一个TV app的焦点控制及遇到的坑

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52835829 前言:上篇中,&l ...

  2. Android TV开发总结(七)构建一个TV app中的剧集列表控件

    原文:Android TV开发总结(七)构建一个TV app中的剧集列表控件 版权声明:我已委托"维权骑士"(rightknights.com)为我的文章进行维权行动.转载务必转载 ...

  3. 《Android Studio开发实战 从零基础到App上线》资源下载和内容勘误

    转载于:https://blog.csdn.net/aqi00/article/details/73065392 资源下载 下面是<Android Studio开发实战 从零基础到App上线&g ...

  4. Android TV开发总结(四)通过RecycleView构建一个TV app列表页(仿腾讯视频TV版)

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52854131 前言:昨晚看锤子手 ...

  5. Android TV开发总结(一)构建一个TV app前要知道的事儿

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52792562 前言:近年来,智能 ...

  6. Android TV开发总结(五)TV上屏幕适配总结

    前言:前面几篇总结一些TV上的小Sample,开源到GitHub:https://github.com/hejunlin2013/TVSample, 点击链接,可以持续关注.今天总结下TV上屏幕适配. ...

  7. 安卓TV开发(六) 移动智能终端UI之实现类似GridView的焦点控制FocusView框架

    转载请标明出处:http://blog.csdn.net/sk719887916/article/details/40045089,作者:skay 前言 安卓TV开发(五) 移动智能终端UI之实现主流 ...

  8. 【Android Developers Training】 3. 构建一个简单UI

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  9. 【Android开发】找乐,一个笑话App的制作过程记录

    缘起 想做一个笑话App的原因是因为在知乎上看过一个帖子,做Android可以有哪些数据可以练手,里面推荐了几个数据开放平台.在这些平台中无一不是有公共的笑话接口,当时心想这个可以拿来练手啊,还挺有意 ...

随机推荐

  1. 两个activity的3D翻转动画.md

    一.业务需求 这里在公司项目设计时,用到了一个小的需求,就是点击一个按钮然后整个activity的页面进行3d翻转; 二.设计思路 由于是2个activity的之间的翻转动画,就意味着前90度是A页面 ...

  2. windows平台安装maven

    Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 一.安装maven3.5.3 安装环境(条件):Windows10.jdk1.7+ 1.下载m ...

  3. [SCOI2016]背单词

    题目描述 Lweb 面对如山的英语单词,陷入了深深的沉思,”我怎么样才能快点学完,然后去玩三国杀呢?“.这时候睿智的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他的计划册是长这样的: ...

  4. C++primer学习——左值和右值

    定义: 左值:用的是对象的身份 右值:用的是对象的值(内存) decltype: 当其作用于表达式时,如果求值结果是左值,那么返回一个引用 如果求值结果是右值,那么返回正常 int*p; declty ...

  5. NOIP2014-5-24模拟赛

    Problem 1 护花(flower.cpp/c/pas) [题目描述] 约翰留下他的N(N<=100000)只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时 ...

  6. ●BZOJ 3622 已经没有什么好害怕的了

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3622 题解: 容斥,dp1).可以求出需要多少对"糖果>药片"(K ...

  7. YOLO: 3 步实时目标检测安装运行教程 [你看那条狗,好像一条狗!]

    封面图是作者运行图,我在 ubuntu 环境下只有文字预测结果. Detection Using A Pre-Trained Model 使用训练好的模型来检测物体 运行一下命令来下载和编译模型 gi ...

  8. Redis设置Key的过期时间 – EXPIRE命令

    EXPIRE key seconds 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除. 操作key对生存时间的影响 生存时间可以通过使用 DEL 命令来删除整个 ...

  9. java 反射机制 观点

    反射,当时经常听他们说,自己也看过一些资料,也可能在设计模式中使用过,但是感觉对它没有一个较深入的了解,这次重新学习了一下,感觉还行吧! 一,先看一下反射的概念: 主要是指程序可以访问,检测和修改它本 ...

  10. Jupyter notebook 输出含中文的pdf 方法

    我电脑 OS 是 Ubuntu14.04, 可用的最简单方法是: 打开终端,输入 sudo find / -name article.tplx 用以查找 article.tplx 文件位置,我电脑的结 ...