溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android仿搜狐視頻、微視等列表播放視頻功能的詳細(xì)解析

發(fā)布時間:2020-07-21 15:55:20 來源:億速云 閱讀:182 作者:小豬 欄目:移動開發(fā)

這篇文章主要講解了Android仿搜狐視頻、微視等列表播放視頻功能的詳細(xì)解析,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

最近項目中需要是實現(xiàn)在列表中自動播放視頻,中間遇到了些問題,終于解決,特來跟大家分享一下:

列表使用的RecyclerView 播放視頻使用MediaPlayer+TextureView。

主要思路:

1、監(jiān)聽RecyclerView的滑動,開始滑動時停止正在播放的item。

2、通過LinearLayoutManager 獲取當(dāng)前顯示的第一個item及最后一個item

3、RecyclerView停止滑動后,選擇item進(jìn)行播放。如果當(dāng)前界面只有一個item,播放當(dāng)前。如果item數(shù)量大于2個,播放第二個。如當(dāng)前界面有兩個item則判定哪一個顯示的區(qū)域比較大。播放item并記錄當(dāng)前position。

附上主要實現(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(); 
} 

看完上述內(nèi)容,是不是對Android仿搜狐視頻、微視等列表播放視頻功能的詳細(xì)解析有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI