- 资源介绍
- 历史当天
传统电视直播节目,在Android TV上起着越来越重要的作用,央视,各地卫视,满足观众日益增长的多元化需求看下效果图:代码实现思路:如图1所示,通过RecycleView为对应的节目项,遥控器按键,可触发跳到对应的直播节目2,用对IjkPlayer进行二次封装,并能用于播放视频源。3,视频源M3U8,可能存在失效,目前获取了一个比较稳定的视频源代码实现:主页面:Recycleview对应adapater直播节目源播放器播放页处理主页面:/* * Copyright (C) 2016 hejunlin <[email][email protected][> * 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 * * [url]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 <[email][email protected][> * 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 * * [url]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多媒体框架总结(一)MediaPlayer介绍之状态图及生命周期“ 第三方播放器典型特点就是另起一个mediaplayerservice,注意这是另外一个进程,为什么是另一个进程,可参见我的文章:MediaPlayer的的C / S模型对于ijkplayer这个框架,因为做实例,才引入,不做评价,也不会去深究,满足基本播放需求就ok了。市场上有很多第三方播放框架,ijkplayer,vitamio,百度云播放等。再看下播放页的播放面板:
这里有几个点要注意:为演示,并未对层级进行使用的FrameLayout,及viewstub,包括等性能优化相关的,在实际商用项目中,建议写的XML文件,尽可能遵循过少的层级,高级标签及的FrameLayout等技巧。所有的大小切勿直接写死,用android:layout_marginTop =“@ dimen / dimen_20dp”表示,string值统一写到string.xml中,这些基本的规范,会让你提高不少效率。
2. 分享目的仅供大家学习和交流,不能确保资源完整性和时效性,请在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
Yunyiwl.com » Android TV直播电视节目,包含各央视频道及卫视频道