Skip to content

Commit

Permalink
exo update
Browse files Browse the repository at this point in the history
  • Loading branch information
qiusunshine committed Apr 18, 2023
1 parent e76b41a commit fe11107
Show file tree
Hide file tree
Showing 20 changed files with 1,220 additions and 399 deletions.
3 changes: 3 additions & 0 deletions VideoPlayModule-Lite/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
implementation 'com.github.ctiao:DanmakuFlameMaster:0.9.25'
implementation 'com.github.ctiao:ndkbitmap-armv5:0.9.21'
//okhttp
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation 'com.squareup.okhttp3:okhttp-brotli:4.2.0'
}

Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package chuangyuan.ycj.videolibrary.factory;

import android.content.Context;
import android.net.Uri;

import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.util.Util;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import chuangyuan.ycj.videolibrary.upstream.DefaultDataSource;
import chuangyuan.ycj.videolibrary.upstream.DefaultDataSourceFactory;
import chuangyuan.ycj.videolibrary.upstream.DefaultHttpDataSource;
import chuangyuan.ycj.videolibrary.upstream.HttpsUtils;
import okhttp3.OkHttpClient;
import okhttp3.brotli.BrotliInterceptor;

import static chuangyuan.ycj.videolibrary.upstream.DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS;

/**
* 作者:By 15968
Expand All @@ -22,7 +28,7 @@ public class HttpDefaultDataSourceFactory implements DataSource.Factory {
private final Context context;
private final DataSource.Factory baseDataSourceFactory;

private final static String DEFAULT_UA = "Mozilla/5.0 (Linux; Android 11; Mi 10 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.152 Mobile Safari/537.36";
public static String DEFAULT_UA = "Mozilla/5.0 (Linux; Android 11; Mi 10 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.152 Mobile Safari/537.36";

/**
* Instantiates a new J default data source factory.
Expand All @@ -34,15 +40,15 @@ public HttpDefaultDataSourceFactory(Context context) {
Map<String, String> headers = new HashMap<>();
headers.put("User-Agent", userAgent);
this.context = context.getApplicationContext();
this.baseDataSourceFactory = init(context, headers);
this.baseDataSourceFactory = init(context, headers, null);
}

public HttpDefaultDataSourceFactory(Context context, Map<String, String> headers) {
public HttpDefaultDataSourceFactory(Context context, Map<String, String> headers, Uri uri) {
this.context = context.getApplicationContext();
this.baseDataSourceFactory = init(context, headers);
this.baseDataSourceFactory = init(context, headers, uri);
}

private DataSource.Factory init(Context context, Map<String, String> headers) {
private DataSource.Factory init(Context context, Map<String, String> headers, Uri uri) {
// String userAgent = Util.getUserAgent(context, context.getPackageName());
String userAgent = DEFAULT_UA;
if (headers != null) {
Expand All @@ -53,13 +59,36 @@ private DataSource.Factory init(Context context, Map<String, String> headers) {
} else if (headers.containsKey("user-Agent")) {
userAgent = headers.get("user-Agent");
}
headers = new HashMap<>(headers);
headers.remove("User-Agent");
}
DefaultHttpDataSource.Factory httpDataSourceFactory = new DefaultHttpDataSource.Factory()
.setUserAgent(userAgent)
.setAllowCrossProtocolRedirects(true);
if (headers != null) {
httpDataSourceFactory.setDefaultRequestProperties(headers);
if (headers == null) {
headers = new HashMap<>();
}
//方法一:信任所有证书,不安全有风险
HttpsUtils.SSLParams sslParams1 = HttpsUtils.getSslSocketFactory();
OkHttpClient okHttpClient = null;
if (uri != null) {
String url = uri.toString();
if (url.contains("://127.0.0.1") || url.contains("://192.168.") || url.contains("://0.0.0.") || url.contains("://10.")) {
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(BrotliInterceptor.INSTANCE)
.sslSocketFactory(sslParams1.sSLSocketFactory, HttpsUtils.UnSafeTrustManager)
.hostnameVerifier(HttpsUtils.UnSafeHostnameVerifier)
.readTimeout(DEFAULT_READ_TIMEOUT_MILLIS * 2, TimeUnit.MILLISECONDS)
.build();
}
}
if (okHttpClient == null) {
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(BrotliInterceptor.INSTANCE)
.sslSocketFactory(sslParams1.sSLSocketFactory, HttpsUtils.UnSafeTrustManager)
.hostnameVerifier(HttpsUtils.UnSafeHostnameVerifier)
.build();
}
OkHttpDataSource.Factory httpDataSourceFactory = new OkHttpDataSource.Factory(okHttpClient)
.setUserAgent(userAgent);
httpDataSourceFactory.setDefaultRequestProperties(headers);
return new DefaultDataSourceFactory(context, null, httpDataSourceFactory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@
*/
public class ExoUserPlayer {
private static final String TAG = ExoUserPlayer.class.getName();

private static int EXTENSION_MODE = DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON;

public static void setExtensionMode(@DefaultRenderersFactory.ExtensionRendererMode int mode) {
EXTENSION_MODE = mode;
}

public static int getExtensionMode(){
return EXTENSION_MODE;
}

public static void resetExtensionMode(){
EXTENSION_MODE = DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON;
}

/***当前活动*/
Activity activity;
/*** 播放view实例***/
Expand Down Expand Up @@ -249,6 +264,7 @@ public void onStop() {
*/
@CallSuper
public void onDestroy() {
resetExtensionMode();
releasePlayers();
}

Expand All @@ -266,6 +282,7 @@ public void releasePlayers() {
}
if (timer != null && !timer.isShutdown()) {
timer.shutdown();
timer = null;
}
if (activity == null || activity.isFinishing()) {
if (mediaSourceBuilder != null) {
Expand Down Expand Up @@ -335,7 +352,8 @@ public void startVideo() {
**/
public SimpleExoPlayer createFullPlayer() {
setDefaultLoadModel();
DefaultRenderersFactory rf = new DefaultRenderersFactory(activity, DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON);
// DefaultRenderersFactory rf = new MyDefaultRenderersFactory(activity, DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON);
DefaultRenderersFactory rf = new DefaultRenderersFactory(activity, EXTENSION_MODE);
SimpleExoPlayer player = new SimpleExoPlayer.Builder(activity, rf).build();
getPlayerViewListener().setPlayer(player);
return player;
Expand Down Expand Up @@ -419,6 +437,10 @@ public void setPlayUri(int index, @NonNull String[] videoUri, @NonNull String[]
setPlaySwitchUri(index, videoUri, name);
}

public void setAudioUrls(List<String> audioUrls) {
mediaSourceBuilder.setAudioUrls(audioUrls);
}

/****
* @param indexType 设置当前索引视频屏蔽进度
* @param firstVideoUri 预览的视频
Expand Down Expand Up @@ -552,6 +574,7 @@ public void seekTo(long positionMs) {
public void seekTo(int windowIndex, long positionMs) {
if (player != null) {
player.seekTo(windowIndex, positionMs);
videoPlayerView.seekFromPlayer(positionMs);
}
}

Expand Down Expand Up @@ -1192,7 +1215,19 @@ public void onRepeatModeChanged(int repeatMode) {

@Override
public void onPlayerError(PlaybackException e) {
Log.e(TAG, "onPlayerError:" + e.getMessage());
Log.e(TAG, "onPlayerError:" + e.getMessage() + ", " + e.getErrorCodeName());
if (("ERROR_CODE_IO_BAD_HTTP_STATUS".equals(e.getErrorCodeName()))
&& getDuration() > 90000
&& mediaSourceBuilder != null
&& mediaSourceBuilder.getMediaSource() != null
&& "com.google.android.exoplayer2.source.hls.HlsMediaSource".equals(mediaSourceBuilder.getMediaSource().getClass().getName())) {
Log.e(TAG, "onPlayerError: " + mediaSourceBuilder.getMediaSource().getClass());
if (getCurrentPosition() < 90000) {
setPosition(getCurrentPosition() + 10000);
startVideo();
return;
}
}
updateResumePosition();
if (e instanceof ExoPlaybackException) {
if (VideoPlayUtils.isBehindLiveWindow((ExoPlaybackException) e)) {
Expand Down
Loading

0 comments on commit fe11107

Please sign in to comment.