提交 dba2125d 编写于 作者: G guoshuyu

增加模拟下载共用缓存例子

上级 6fd6762b
......@@ -207,6 +207,11 @@
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:theme="@style/Theme.AppCompat.Transitions"
android:screenOrientation="portrait" />
<activity
android:name=".DetailDownloadPlayer"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:theme="@style/Theme.AppCompat.Transitions"
android:screenOrientation="portrait" />
<meta-data
android:name="android.max_aspect"
......
package com.example.gsyvideoplayer;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;
import com.danikula.videocache.HttpProxyCacheServer;
import com.example.gsyvideoplayer.utils.MemoryCallBack;
import com.example.gsyvideoplayer.video.LandLayoutVideo;
import com.google.android.exoplayer2.SeekParameters;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.cache.ProxyCacheManager;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.listener.GSYVideoProgressListener;
import com.shuyu.gsyvideoplayer.listener.LockClickListener;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.request.RequestCall;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager;
public class DetailDownloadPlayer extends AppCompatActivity {
@BindView(R.id.post_detail_nested_scroll)
NestedScrollView postDetailNestedScroll;
@BindView(R.id.detail_player)
LandLayoutVideo detailPlayer;
@BindView(R.id.start_down)
Button startDownLoad;
@BindView(R.id.stop_down)
Button stopDownLoad;
@BindView(R.id.activity_detail_player)
RelativeLayout activityDetailPlayer;
private boolean isPlay;
private boolean isPause;
private OrientationUtils orientationUtils;
private HttpProxyCacheServer proxyCacheServer;
private RequestCall requestCall;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_download_player);
ButterKnife.bind(this);
String url = getUrl();
//增加封面
ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(R.mipmap.xxx1);
resolveNormalVideoUI();
//外部辅助的旋转,帮助全屏
orientationUtils = new OrientationUtils(this, detailPlayer);
//初始化不打开外部的旋转
orientationUtils.setEnable(false);
Map<String, String> header = new HashMap<>();
header.put("ee", "33");
header.put("allowCrossProtocolRedirects", "true");
GSYVideoOptionBuilder gsyVideoOption = new GSYVideoOptionBuilder();
gsyVideoOption.setThumbImageView(imageView)
.setIsTouchWiget(true)
.setRotateViewAuto(false)
.setLockLand(false)
.setAutoFullWithSize(false)
.setShowFullAnimation(false)
.setNeedLockFull(true)
.setUrl(url)
.setMapHeadData(header)
.setCacheWithPlay(true)
.setVideoTitle("测试视频")
.setVideoAllCallBack(new GSYSampleCallBack() {
@Override
public void onPrepared(String url, Object... objects) {
Debuger.printfError("***** onPrepared **** " + objects[0]);
Debuger.printfError("***** onPrepared **** " + objects[1]);
super.onPrepared(url, objects);
//开始播放了才能旋转和全屏
orientationUtils.setEnable(true);
isPlay = true;
//设置 seek 的临近帧。
if (detailPlayer.getGSYVideoManager().getPlayer() instanceof Exo2PlayerManager) {
((Exo2PlayerManager) detailPlayer.getGSYVideoManager().getPlayer()).setSeekParameter(SeekParameters.NEXT_SYNC);
Debuger.printfError("***** setSeekParameter **** ");
}
}
@Override
public void onEnterFullscreen(String url, Object... objects) {
super.onEnterFullscreen(url, objects);
Debuger.printfError("***** onEnterFullscreen **** " + objects[0]);//title
Debuger.printfError("***** onEnterFullscreen **** " + objects[1]);//当前全屏player
}
@Override
public void onAutoComplete(String url, Object... objects) {
super.onAutoComplete(url, objects);
}
@Override
public void onClickStartError(String url, Object... objects) {
super.onClickStartError(url, objects);
}
@Override
public void onQuitFullscreen(String url, Object... objects) {
super.onQuitFullscreen(url, objects);
Debuger.printfError("***** onQuitFullscreen **** " + objects[0]);//title
Debuger.printfError("***** onQuitFullscreen **** " + objects[1]);//当前非全屏player
if (orientationUtils != null) {
orientationUtils.backToProtVideo();
}
}
})
.setLockClickListener(new LockClickListener() {
@Override
public void onClick(View view, boolean lock) {
if (orientationUtils != null) {
//配合下方的onConfigurationChanged
orientationUtils.setEnable(!lock);
}
}
})
.setGSYVideoProgressListener(new GSYVideoProgressListener() {
@Override
public void onProgress(int progress, int secProgress, int currentPosition, int duration) {
Debuger.printfLog(" progress " + progress + " secProgress " + secProgress + " currentPosition " + currentPosition + " duration " + duration);
}
})
.build(detailPlayer);
detailPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//直接横屏
orientationUtils.resolveByClick();
//第一个true是否需要隐藏actionbar,第二个true是否需要隐藏statusbar
detailPlayer.startWindowFullscreen(DetailDownloadPlayer.this, true, true);
}
});
startDownLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = proxyCacheServer.getProxyUrl(getUrl());
startDownload(url);
}
});
stopDownLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopDownload();
}
});
proxyCacheServer = ProxyCacheManager.instance().newProxy(getApplicationContext());
}
@Override
public void onBackPressed() {
if (orientationUtils != null) {
orientationUtils.backToProtVideo();
}
if (GSYVideoManager.backFromWindowFull(this)) {
return;
}
super.onBackPressed();
}
@Override
protected void onPause() {
getCurPlay().onVideoPause();
super.onPause();
isPause = true;
}
@Override
protected void onResume() {
getCurPlay().onVideoResume(false);
super.onResume();
isPause = false;
}
@Override
protected void onDestroy() {
super.onDestroy();
if (isPlay) {
getCurPlay().release();
}
stopDownload();
if (orientationUtils != null)
orientationUtils.releaseListener();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//如果旋转了就全屏
if (isPlay && !isPause) {
detailPlayer.onConfigurationChanged(this, newConfig, orientationUtils, true, true);
}
}
private void resolveNormalVideoUI() {
//增加title
detailPlayer.getTitleTextView().setVisibility(View.GONE);
detailPlayer.getBackButton().setVisibility(View.GONE);
}
private GSYVideoPlayer getCurPlay() {
if (detailPlayer.getFullWindowPlayer() != null) {
return detailPlayer.getFullWindowPlayer();
}
return detailPlayer;
}
private String getUrl() {
String url = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4";
return url;
}
private void startDownload(String url) {
Debuger.printfError("###### url " + url);
//下载demo然后设置
requestCall = OkHttpUtils.get().url(url)
.build();
requestCall.execute(new MemoryCallBack() {
@Override
public void onError(Call call, Exception e, int id) {
}
@Override
public void onResponse(Boolean response, int id) {
stopDownload();
}
});
}
private void stopDownload() {
if (requestCall != null) {
requestCall.cancel();
requestCall = null;
}
if (proxyCacheServer != null) {
proxyCacheServer.shutdown();
}
}
}
\ No newline at end of file
......@@ -54,7 +54,7 @@ public class MainActivity extends AppCompatActivity {
@OnClick({R.id.open_btn, R.id.list_btn, R.id.list_btn_2, R.id.list_detail, R.id.clear_cache, R.id.recycler, R.id.recycler_2, R.id.list_detail_list, R.id.web_detail, R.id.danmaku_video, R.id.fragment_video,
R.id.more_type, R.id.input_type, R.id.open_btn_empty, R.id.open_control, R.id.open_filter, R.id.open_btn_pick, R.id.open_btn_auto, R.id.open_scroll, R.id.open_window, R.id.open_btn_ad,
R.id.open_btn_multi, R.id.open_btn_ad2, R.id.open_list_ad, R.id.open_custom_exo, R.id.open_simple, R.id.open_switch, R.id.media_codec, R.id.detail_normal_activity})
R.id.open_btn_multi, R.id.open_btn_ad2, R.id.open_list_ad, R.id.open_custom_exo, R.id.open_simple, R.id.open_switch, R.id.media_codec, R.id.detail_normal_activity, R.id.detail_download_activity})
public void onClick(View view) {
switch (view.getId()) {
case R.id.open_simple:
......@@ -162,6 +162,9 @@ public class MainActivity extends AppCompatActivity {
case R.id.detail_normal_activity:
JumpUtils.goToDetailNormalActivity(this);
break;
case R.id.detail_download_activity:
JumpUtils.goToDetailDownloadActivity(this);
break;
case R.id.clear_cache:
//清理缓存
GSYVideoManager.instance().clearAllDefaultCache(MainActivity.this);
......
......@@ -12,6 +12,7 @@ import com.example.gsyvideoplayer.DanmkuVideoActivity;
import com.example.gsyvideoplayer.DetailADPlayer;
import com.example.gsyvideoplayer.DetailADPlayer2;
import com.example.gsyvideoplayer.DetailControlActivity;
import com.example.gsyvideoplayer.DetailDownloadPlayer;
import com.example.gsyvideoplayer.DetailFilterActivity;
import com.example.gsyvideoplayer.DetailListPlayer;
import com.example.gsyvideoplayer.DetailMoreTypeActivity;
......@@ -254,6 +255,16 @@ public class JumpUtils {
Intent intent = new Intent(activity, DetailNormalActivityPlayer.class);
activity.startActivity(intent);
}
/**
* 跳转到详情播放
*
* @param activity
*/
public static void goToDetailDownloadActivity(Activity activity) {
Intent intent = new Intent(activity, DetailDownloadPlayer.class);
activity.startActivity(intent);
}
/**
* 跳转到详情播放
......
package com.example.gsyvideoplayer.utils;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.Response;
public abstract class MemoryCallBack extends Callback<Boolean> {
@Override
public Boolean parseNetworkResponse(Response response, int id) throws Exception {
return saveFile(response, id);
}
//// 模拟下载,这样可以和 proxy cache 公用一个本地cache
public boolean saveFile(Response response, final int id) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
try {
is = response.body().byteStream();
final long total = response.body().contentLength();
long sum = 0;
while ((len = is.read(buf)) != -1) {
sum += len;
final long finalSum = sum;
OkHttpUtils.getInstance().getDelivery().execute(new Runnable() {
@Override
public void run() {
Debuger.printfLog("######### inProgress" + finalSum * 1.0f / total);
inProgress(finalSum * 1.0f / total, total, id);
}
});
}
return true;
} finally {
try {
response.body().close();
if (is != null) is.close();
} catch (IOException e) {
}
}
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_detail_player"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:id="@+id/post_detail_nested_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/post_media_height" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/detail_text"
android:textSize="16sp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<com.example.gsyvideoplayer.video.LandLayoutVideo
android:id="@+id/detail_player"
android:layout_width="match_parent"
android:layout_height="@dimen/post_media_height" />
<RelativeLayout
android:id="@+id/detail_player_control_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/detail_player">
<Button
android:id="@+id/start_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="开始缓存" />
<Button
android:id="@+id/stop_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_toRightOf="@+id/start_down"
android:text="停止缓存,播放前必须停止哦" />
</RelativeLayout>
</RelativeLayout>
......@@ -202,8 +202,12 @@
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="硬解码支持" />
<Button
android:id="@+id/detail_download_activity"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="缓存下载支持" />
<Button
android:id="@+id/detail_normal_activity"
android:layout_width="240dp"
......
......@@ -50,14 +50,14 @@ dependencies {
//api "com.shuyu:gsyVideoPlayer-java:$gsyVideoVersion"
api "com.shuyu:GSYVideoPlayer-exo2:$gsyVideoVersion"
api "com.shuyu:gsyVideoPlayer-armv5:$gsyVideoVersion"
api "com.shuyu:gsyVideoPlayer-armv7a:$gsyVideoVersion"
api "com.shuyu:gsyVideoPlayer-arm64:$gsyVideoVersion"
api "com.shuyu:gsyVideoPlayer-x64:$gsyVideoVersion"
api "com.shuyu:gsyVideoPlayer-x86:$gsyVideoVersion"
//api "com.shuyu:gsyVideoPlayer-armv5:$gsyVideoVersion"
//api "com.shuyu:gsyVideoPlayer-armv7a:$gsyVideoVersion"
//api "com.shuyu:gsyVideoPlayer-arm64:$gsyVideoVersion"
//api "com.shuyu:gsyVideoPlayer-x64:$gsyVideoVersion"
//api "com.shuyu:gsyVideoPlayer-x86:$gsyVideoVersion"
//更多配置版so,增加了concat,rtsp,mpeg,crypto
//api "com.shuyu:gsyVideoPlayer-ex_so:$gsyVideoVersion"
api "com.shuyu:gsyVideoPlayer-ex_so:$gsyVideoVersion"
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册