- 資源介紹
- 歷史當天
傳統電視直播節目,在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. 本站資源售價只是贊助,收取費用僅維持本站的日常運營所需!
耘藝源碼網 » Android TV直播電視節目,包含各央視頻道及衛視頻道