Android仿搜狐視頻、微視等列表播放視頻功能
最近項(xiàng)目中需要是實(shí)現(xiàn)在列表中自動(dòng)播放視頻,中間遇到了些問題,終于解決,特來(lái)跟大家分享一下:
列表使用的RecyclerView 播放視頻使用MediaPlayer+TextureView。
主要思路:
1、監(jiān)聽RecyclerView的滑動(dòng),開始滑動(dòng)時(shí)停止正在播放的item。
2、通過(guò)LinearLayoutManager 獲取當(dāng)前顯示的第一個(gè)item及最后一個(gè)item
3、RecyclerView停止滑動(dòng)后,選擇item進(jìn)行播放。如果當(dāng)前界面只有一個(gè)item,播放當(dāng)前。如果item數(shù)量大于2個(gè),播放第二個(gè)。如當(dāng)前界面有兩個(gè)item則判定哪一個(gè)顯示的區(qū)域比較大。播放item并記錄當(dāng)前position。
附上主要實(shí)現(xiàn)邏輯:
try { int fristPos = layoutManager.findFirstVisibleItemPosition(); int lastPos = layoutManager.findLastVisibleItemPosition(); ViewHolder holder = null; if (recyclerView.getChildCount() == 2) { View fristView = recyclerView.getChildAt(0); if (fristView != null) { int[] location = new int[2]; fristView.getLocationInWindow(location); if (location[1] > 0) { holder = (ViewHolder) recyclerView.findViewHolderForPosition(fristPos); lastPlayPosition = fristPos; } } if (holder == null) { View lastView = recyclerView.getChildAt(1); if (lastView != null) { int[] lastViewLocation = new int[2]; lastView.getLocationInWindow(lastViewLocation); if ((lastViewLocation[1] + videoHeight) < screenHeight) { holder = (ViewHolder) recyclerView.findViewHolderForPosition(lastPos); lastPlayPosition = lastPos; } } } } else if (recyclerView.getChildCount() == 1) { holder = (ViewHolder) recyclerView.findViewHolderForPosition(fristPos); lastPlayPosition = fristPos; } else { holder = (ViewHolder) recyclerView.findViewHolderForPosition(fristPos + 1); lastPlayPosition = fristPos + 1; } if (holder != null) { holder.play(); } } catch (Exception e) { e.printStackTrace(); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android使用surfaceview+MediaPlayer播放視頻
- Android ViewPager中顯示圖片與播放視頻的填坑記錄
- Android中使用TextureView播放視頻
- Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法
- Android編程實(shí)現(xiàn)播放視頻的方法示例
- Android多媒體教程之播放視頻的四種方法
- Android 播放視頻常見問題小結(jié)
- Android DragVideo實(shí)現(xiàn)播放視頻時(shí)任意拖拽的方法
- Android仿新浪微博/QQ空間滑動(dòng)自動(dòng)播放視頻功能
- android surfaceView實(shí)現(xiàn)播放視頻功能
相關(guān)文章
Android導(dǎo)航欄功能項(xiàng)的顯示與屏蔽介紹
大家好,本篇文章主要講的是Android導(dǎo)航欄功能項(xiàng)的顯示與屏蔽介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12android 設(shè)置圓角圖片實(shí)現(xiàn)代碼
在android應(yīng)用開發(fā)中,可能是美化需要,圖片需要處理成圓角,本文將給出實(shí)現(xiàn)代碼,開發(fā)中的遇到此問題的朋友可以參考下2012-11-11Android 使用jQuery實(shí)現(xiàn)item點(diǎn)擊顯示或隱藏的特效的示例
本篇文章主要介紹了Android 使用jQuery實(shí)現(xiàn)item點(diǎn)擊顯示或隱藏的特效的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Android基礎(chǔ)控件(EditView、SeekBar等)的使用方法
這篇文章主要介紹了Android基礎(chǔ)控件的屬性及使用方法,介紹了基礎(chǔ)控件有TextView、ImageView、Button、EditView等,感興趣的小伙伴們可以參考一下2016-03-03Android自定義View之漸變色折線圖的實(shí)現(xiàn)
折線圖的實(shí)現(xiàn)方法在github上有很多開源的程序,但是對(duì)于初學(xué)者來(lái)講,簡(jiǎn)單一點(diǎn)的教程可能更容易入門,下面這篇文章主要給大家介紹了關(guān)于Android自定義View之漸變色折線圖的相關(guān)資料,需要的朋友可以參考下2022-04-04Android實(shí)現(xiàn)Recycleview懸浮粘性頭部外加右側(cè)字母導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)Recycleview懸浮粘性頭部外加右側(cè)字母導(dǎo)航,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06