最近App改版新版详情包含商品视频介绍,视频源都是youtube的链接,找了很久没找到合适的方案,在墙外搜索才找到一个合适的,再次记录下.仅供参考.
第三方控件参考Git:https://github.com/PierfrancescoSoffritti/android-youtube-player
我们实现是以列表形式加载多个视频的,所以以列表形式实现.
就不贴全部代码了,贴些关键代码;
//详情视频适配器
private DetailsVideoAdapter detailsVideoAdapter;
//数据源参考:"videoUrlList": ["https:\\www.youtube.com\watch?v=S0Q4gqBUs7c","https:\\www.youtube.com\watch?v=xxxxxx"]
if (detailsVideoAdapter == null) {
detailsVideoAdapter = new DetailsVideoAdapter(this,
videoUrlList,this.getLifecycle());
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
binding.rvVideo.setLayoutManager(layoutManager);
binding.rvVideo.setAdapter(detailsVideoAdapter);
} else {
detailsVideoAdapter.setList(videoUrlList);
}
适配器:
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView;
import java.util.List;
/**
* create by
* on 2022/6/1
* explain${详情视频适配器}
*
* @author
*/
public class DetailsVideoAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
private Context mComtext;
private YouTubePlayerView youTubePlayerView;
private Lifecycle lifecycle;
public DetailsVideoAdapter(@Nullable Activity context, @Nullable List<String> data, Lifecycle lifecycle) {
super(R.layout.video_item, data);
this.mComtext = context;
this.lifecycle = lifecycle;
}
@SuppressLint("SetTextI18n")
@Override
protected void convert(@NonNull BaseViewHolder baseViewHolder, String productInfosBean) {
youTubePlayerView = baseViewHolder.getView(R.id.youtube_player_view);
lifecycle.addObserver(youTubePlayerView);
youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady(@NonNull YouTubePlayer youTubePlayer) {
String videoId = parseIDfromVideoUrl(productInfosBean);
Log.e("Alex", "videoId:" + videoId);
youTubePlayer.cueVideo(videoId, 0);
}
});
}
//解析视频的格式
private static final String VIDEO_ID_START_EMBED = "embed/";
private static final String VIDEO_ID_START_NORMAL = "?v=";
private static final String VIDEO_ID_START_SHORT = "youtu.be/";
/**
* 在youtobe的连接中截取视频的ID
*/
public static String parseIDfromVideoUrl(String videoUrl) {
if (TextUtils.isEmpty(videoUrl)) {
Log.i("Alex", "videoUrl is null");
return "";
}
int startIndex = videoUrl.indexOf(VIDEO_ID_START_NORMAL);
int prefixLength = VIDEO_ID_START_NORMAL.length();
if (startIndex <= 0) {
startIndex = videoUrl.indexOf(VIDEO_ID_START_EMBED);
prefixLength = VIDEO_ID_START_EMBED.length();
}
if (startIndex <= 0) {
startIndex = videoUrl.indexOf(VIDEO_ID_START_SHORT);
prefixLength = VIDEO_ID_START_SHORT.length();
}
Log.i("Alex", "startIndex==" + startIndex);
if (startIndex != -1) {
startIndex = startIndex + prefixLength;
//有些url后面会带参数,不能把参数当id
int endIndex = 0;
//如果当前是普通类型的url
if (prefixLength == VIDEO_ID_START_NORMAL.length()) {
endIndex = videoUrl.indexOf("&");
} else {
endIndex = videoUrl.indexOf("?");
}
if (endIndex == -1) {
endIndex = videoUrl.length();
}
Log.i("Alex", "startIndex::" + startIndex + " end==" + endIndex);
if (startIndex < endIndex) {
return videoUrl.substring(startIndex, endIndex);
}
} else {
Log.i("Alex", "不能解析视频的ID");
}
return "";
}
}
界面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- app:videoId="2vUqG-lxFx0"
app:autoPlay="false"-->
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="@+id/youtube_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dp_10"
android:layout_marginEnd="@dimen/dp_10"
/>
</LinearLayout>