Commit 6743ccd8 authored by 委座-江's avatar 委座-江

性能优化

parent b395cc46
......@@ -10,7 +10,6 @@ import android.widget.TextView;
import com.widget.imagevideobanner.bean.MediaBean;
import com.widget.imagevideobanner.view.ImageVideoBanner;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
......@@ -38,8 +37,8 @@ public class MainActivity extends AppCompatActivity {
mediaBean2.setResType(TYPE_IMAGE);
list.add(mediaBean2);
String basePath = "http://miya-hz.oss-cn-shanghai.aliyuncs.com/huihua-test/face-pay/archive/";
// String basePath = "http://miya-hz.oss-cn-shanghai.aliyuncs.com/huihua-test/face-pay/archive/";
String basePath = Environment.getExternalStorageDirectory().getPath()+"/";
MediaBean mediaBean3 = new MediaBean();
mediaBean3.setUrl(basePath+"shu.mp4");
......
......@@ -2,6 +2,7 @@ package com.widget.imagevideobanner.player;
import android.app.Application;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import com.google.android.exoplayer2.DefaultLoadControl;
......@@ -23,43 +24,28 @@ import com.google.android.exoplayer2.util.Util;
import com.widget.imagevideobanner.BaseApplication;
import com.widget.imagevideobanner.R;
import java.io.File;
import java.util.HashMap;
public class PlayerManager {
private final ExtractorMediaSource.Factory mediaSourceFactory;
private ExtractorMediaSource.Factory networkMediaSourceFactory;
private ExtractorMediaSource.Factory localMediaSourceFactory;
public SimpleExoPlayer exoPlayer;
public PlayerView playerView;
public String playUrl;
private static PlayerManager mInstance;
private HashMap<String,MediaSource> mediaSourceHashMap = new HashMap<>();
public PlayerManager() {
Application application = BaseApplication.getApplication();
//创建http视频资源如何加载的工厂对象
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(application, application.getPackageName()));
//创建缓存,指定缓存位置,和缓存策略,为最近最少使用原则,最大为200m
Cache cache = new SimpleCache(application.getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 200));
//把缓存对象cache和负责缓存数据读取、写入的工厂类CacheDataSinkFactory 相关联
CacheDataSinkFactory cacheDataSinkFactory = new CacheDataSinkFactory(cache, Long.MAX_VALUE);
/**创建能够 边播放边缓存的 本地资源加载和http网络数据写入的工厂类
* public CacheDataSourceFactory(
* Cache cache, 缓存写入策略和缓存写入位置的对象
* DataSource.Factory upstreamFactory,http视频资源如何加载的工厂对象
* DataSource.Factory cacheReadDataSourceFactory,本地缓存数据如何读取的工厂对象
* @Nullable DataSink.Factory cacheWriteDataSinkFactory,http网络数据如何写入本地缓存的工厂对象
* @CacheDataSource.Flags int flags,加载本地缓存数据进行播放时的策略,如果遇到该文件正在被写入数据,或读取缓存数据发生错误时的策略
* @Nullable CacheDataSource.EventListener eventListener 缓存数据读取的回调
*/
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(cache,
dataSourceFactory,
new FileDataSourceFactory(),
cacheDataSinkFactory,
CacheDataSource.FLAG_BLOCK_ON_CACHE,
null);
//最后 还需要创建一个 MediaSource 媒体资源 加载的工厂类
//因为由它创建的MediaSource 能够实现边缓冲边播放的效果,
mediaSourceFactory = new ExtractorMediaSource.Factory(cacheDataSourceFactory);
buildLocalMediaSourceFactory(application);
buildNetworkMediaSourceFactory(application);
buildPlayerAndView(application);
}
private void buildPlayerAndView(Application application){
//创建exoplayer播放器实例
exoPlayer = ExoPlayerFactory.newSimpleInstance(application,
//视频的音视频轨道如何加载,使用默认的轨道选择器
......@@ -73,6 +59,46 @@ public class PlayerManager {
playerView.setPlayer(exoPlayer);
}
private void buildNetworkMediaSourceFactory(Application application){
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(application, application.getPackageName())); //创建缓存,指定缓存位置,和缓存策略,为最近最少使用原则,最大为200m
Cache cache = new SimpleCache(application.getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 200));
//把缓存对象cache和负责缓存数据读取、写入的工厂类CacheDataSinkFactory 相关联
CacheDataSinkFactory cacheDataSinkFactory = new CacheDataSinkFactory(cache, Long.MAX_VALUE);
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(cache,
dataSourceFactory,
new FileDataSourceFactory(),
cacheDataSinkFactory,
CacheDataSource.FLAG_BLOCK_ON_CACHE,
null);
networkMediaSourceFactory = new ExtractorMediaSource.Factory(cacheDataSourceFactory);
}
private void buildLocalMediaSourceFactory(Application application){
FileDataSourceFactory fileDataSourceFactory = new FileDataSourceFactory();
//创建缓存,指定缓存位置,和缓存策略,为最近最少使用原则,最大为200m
Cache cache = new SimpleCache(application.getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 200));
//把缓存对象cache和负责缓存数据读取、写入的工厂类CacheDataSinkFactory 相关联
CacheDataSinkFactory cacheDataSinkFactory = new CacheDataSinkFactory(cache, Long.MAX_VALUE);
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(cache,
fileDataSourceFactory,
new FileDataSourceFactory(),
cacheDataSinkFactory,
CacheDataSource.FLAG_BLOCK_ON_CACHE,
new CacheDataSource.EventListener() {
@Override
public void onCachedBytesRead(long cacheSizeBytes, long cachedBytesRead) {
Log.e("###视频缓存 ","cachedBytesRead ="+cachedBytesRead + "cacheSizeBytes = "+cacheSizeBytes);
}
});
localMediaSourceFactory = new ExtractorMediaSource.Factory(cacheDataSourceFactory);
}
public static PlayerManager getInstance(){
if (mInstance == null) {
synchronized (PlayerManager.class) {
......@@ -94,8 +120,17 @@ public class PlayerManager {
newPlayerView.setPlayer(attach ? exoPlayer : null);
}
//todo 可以缓存起来
public MediaSource createMediaSource(String url) {
return mediaSourceFactory.createMediaSource(Uri.parse(url));
if(mediaSourceHashMap.containsKey(url)){
return mediaSourceHashMap.get(url);
}
MediaSource mediaSource = null;
if(url.contains("http")){
return networkMediaSourceFactory.createMediaSource(Uri.parse(url));
}else{
mediaSource = localMediaSourceFactory.createMediaSource(Uri.fromFile(new File(url)));
}
mediaSourceHashMap.put(url,mediaSource);
return mediaSource;
}
}
\ No newline at end of file
......@@ -38,6 +38,7 @@ public class VpImageView extends AppCompatImageView implements IPlayTarget {
}
};
public VpImageView(Context context) {
super(context);
}
......@@ -80,7 +81,4 @@ public class VpImageView extends AppCompatImageView implements IPlayTarget {
handler.removeCallbacks(pageChangeRunable);
}
}
......@@ -51,7 +51,11 @@ public class VpVideoView extends FrameLayout implements IPlayTarget, PlayerContr
private MediaBean mediaBean;
private boolean isVideoEnded = false;
public VpVideoView(@NonNull Context context,MediaBean bean, final OnActionFinishListener onPageChangedListener) {
public VpVideoView(@NonNull Context context) {
super(context);
}
public VpVideoView(@NonNull Context context, MediaBean bean, final OnActionFinishListener onPageChangedListener) {
super(context);
LayoutInflater.from(context).inflate(R.layout.layout_player_view, this, true);
//封面view
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment