Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
I
ImageVideoBanner
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jiangjiantao
ImageVideoBanner
Commits
6743ccd8
Commit
6743ccd8
authored
May 10, 2021
by
委座-江
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
性能优化
parent
b395cc46
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
72 additions
and
36 deletions
+72
-36
MainActivity.java
...c/main/java/com/widget/imagevideobanner/MainActivity.java
+2
-3
PlayerManager.java
...ava/com/widget/imagevideobanner/player/PlayerManager.java
+64
-29
VpImageView.java
...in/java/com/widget/imagevideobanner/view/VpImageView.java
+1
-3
VpVideoView.java
...in/java/com/widget/imagevideobanner/view/VpVideoView.java
+5
-1
No files found.
imageVideoBanner/src/main/java/com/widget/imagevideobanner/MainActivity.java
View file @
6743ccd8
...
@@ -10,7 +10,6 @@ import android.widget.TextView;
...
@@ -10,7 +10,6 @@ import android.widget.TextView;
import
com.widget.imagevideobanner.bean.MediaBean
;
import
com.widget.imagevideobanner.bean.MediaBean
;
import
com.widget.imagevideobanner.view.ImageVideoBanner
;
import
com.widget.imagevideobanner.view.ImageVideoBanner
;
import
java.io.File
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
...
@@ -38,8 +37,8 @@ public class MainActivity extends AppCompatActivity {
...
@@ -38,8 +37,8 @@ public class MainActivity extends AppCompatActivity {
mediaBean2
.
setResType
(
TYPE_IMAGE
);
mediaBean2
.
setResType
(
TYPE_IMAGE
);
list
.
add
(
mediaBean2
);
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
();
MediaBean
mediaBean3
=
new
MediaBean
();
mediaBean3
.
setUrl
(
basePath
+
"shu.mp4"
);
mediaBean3
.
setUrl
(
basePath
+
"shu.mp4"
);
...
...
imageVideoBanner/src/main/java/com/widget/imagevideobanner/player/PlayerManager.java
View file @
6743ccd8
...
@@ -2,6 +2,7 @@ package com.widget.imagevideobanner.player;
...
@@ -2,6 +2,7 @@ package com.widget.imagevideobanner.player;
import
android.app.Application
;
import
android.app.Application
;
import
android.net.Uri
;
import
android.net.Uri
;
import
android.util.Log
;
import
android.view.LayoutInflater
;
import
android.view.LayoutInflater
;
import
com.google.android.exoplayer2.DefaultLoadControl
;
import
com.google.android.exoplayer2.DefaultLoadControl
;
...
@@ -23,43 +24,28 @@ import com.google.android.exoplayer2.util.Util;
...
@@ -23,43 +24,28 @@ import com.google.android.exoplayer2.util.Util;
import
com.widget.imagevideobanner.BaseApplication
;
import
com.widget.imagevideobanner.BaseApplication
;
import
com.widget.imagevideobanner.R
;
import
com.widget.imagevideobanner.R
;
import
java.io.File
;
import
java.util.HashMap
;
public
class
PlayerManager
{
public
class
PlayerManager
{
private
final
ExtractorMediaSource
.
Factory
mediaSourceFactory
;
private
ExtractorMediaSource
.
Factory
networkMediaSourceFactory
;
private
ExtractorMediaSource
.
Factory
localMediaSourceFactory
;
public
SimpleExoPlayer
exoPlayer
;
public
SimpleExoPlayer
exoPlayer
;
public
PlayerView
playerView
;
public
PlayerView
playerView
;
public
String
playUrl
;
public
String
playUrl
;
private
static
PlayerManager
mInstance
;
private
static
PlayerManager
mInstance
;
private
HashMap
<
String
,
MediaSource
>
mediaSourceHashMap
=
new
HashMap
<>();
public
PlayerManager
()
{
public
PlayerManager
()
{
Application
application
=
BaseApplication
.
getApplication
();
Application
application
=
BaseApplication
.
getApplication
();
//创建http视频资源如何加载的工厂对象
buildLocalMediaSourceFactory
(
application
);
DefaultHttpDataSourceFactory
dataSourceFactory
=
new
DefaultHttpDataSourceFactory
(
Util
.
getUserAgent
(
application
,
application
.
getPackageName
()));
buildNetworkMediaSourceFactory
(
application
);
//创建缓存,指定缓存位置,和缓存策略,为最近最少使用原则,最大为200m
buildPlayerAndView
(
application
);
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
);
private
void
buildPlayerAndView
(
Application
application
){
//创建exoplayer播放器实例
//创建exoplayer播放器实例
exoPlayer
=
ExoPlayerFactory
.
newSimpleInstance
(
application
,
exoPlayer
=
ExoPlayerFactory
.
newSimpleInstance
(
application
,
//视频的音视频轨道如何加载,使用默认的轨道选择器
//视频的音视频轨道如何加载,使用默认的轨道选择器
...
@@ -73,6 +59,46 @@ public class PlayerManager {
...
@@ -73,6 +59,46 @@ public class PlayerManager {
playerView
.
setPlayer
(
exoPlayer
);
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
(){
public
static
PlayerManager
getInstance
(){
if
(
mInstance
==
null
)
{
if
(
mInstance
==
null
)
{
synchronized
(
PlayerManager
.
class
)
{
synchronized
(
PlayerManager
.
class
)
{
...
@@ -94,8 +120,17 @@ public class PlayerManager {
...
@@ -94,8 +120,17 @@ public class PlayerManager {
newPlayerView
.
setPlayer
(
attach
?
exoPlayer
:
null
);
newPlayerView
.
setPlayer
(
attach
?
exoPlayer
:
null
);
}
}
//todo 可以缓存起来
public
MediaSource
createMediaSource
(
String
url
)
{
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
imageVideoBanner/src/main/java/com/widget/imagevideobanner/view/VpImageView.java
View file @
6743ccd8
...
@@ -38,6 +38,7 @@ public class VpImageView extends AppCompatImageView implements IPlayTarget {
...
@@ -38,6 +38,7 @@ public class VpImageView extends AppCompatImageView implements IPlayTarget {
}
}
};
};
public
VpImageView
(
Context
context
)
{
public
VpImageView
(
Context
context
)
{
super
(
context
);
super
(
context
);
}
}
...
@@ -80,7 +81,4 @@ public class VpImageView extends AppCompatImageView implements IPlayTarget {
...
@@ -80,7 +81,4 @@ public class VpImageView extends AppCompatImageView implements IPlayTarget {
handler
.
removeCallbacks
(
pageChangeRunable
);
handler
.
removeCallbacks
(
pageChangeRunable
);
}
}
}
}
imageVideoBanner/src/main/java/com/widget/imagevideobanner/view/VpVideoView.java
View file @
6743ccd8
...
@@ -51,7 +51,11 @@ public class VpVideoView extends FrameLayout implements IPlayTarget, PlayerContr
...
@@ -51,7 +51,11 @@ public class VpVideoView extends FrameLayout implements IPlayTarget, PlayerContr
private
MediaBean
mediaBean
;
private
MediaBean
mediaBean
;
private
boolean
isVideoEnded
=
false
;
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
);
super
(
context
);
LayoutInflater
.
from
(
context
).
inflate
(
R
.
layout
.
layout_player_view
,
this
,
true
);
LayoutInflater
.
from
(
context
).
inflate
(
R
.
layout
.
layout_player_view
,
this
,
true
);
//封面view
//封面view
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment