Skip to content

Commit

Permalink
motion photo 的播放-解决轮播图里视频播放报错问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Sep 20, 2024
1 parent 3001489 commit 7ed6f13
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,17 @@ private void loadMotionVideo(String uri) {
LargeImageViewer.fadeToGone(jpgView,300);
//jpgView.setVisibility(GONE);
File file = new File(motionVideoPath);
if(player ==null){

if(player !=null){
if(player.isPlaying()){
player.stop();
}else if(player.isLoading()){
player.stop();
}
player.release();
player = null;
}

player = new ExoPlayer.Builder(getContext())
.build();
playerView.setPlayer(player);
Expand All @@ -475,39 +485,44 @@ public void onPlayerError(PlaybackException error) {
}
};
player.addListener(listener);

player.setPlayWhenReady(true);
player.setRepeatMode(Player.REPEAT_MODE_OFF);
}else{
player.removeListener(listener);
listener = new Player.Listener() {
@Override
public void onPlaybackStateChanged(int playbackState) {
Player.Listener.super.onPlaybackStateChanged(playbackState);
if(playbackState == Player.STATE_ENDED ){
loadLocal(uri,false,false);
}
}

@Override
public void onPlayerError(PlaybackException error) {
Player.Listener.super.onPlayerError(error);
LogUtils.w(error);
loadLocal(uri,false,false);
}
};
player.addListener(listener);

//sending message to a Handler on a dead thread
try{
//IllegalStateException: Handler (android.os.Handler) {674e76a} sending message to a Handler on a dead thread
player.setMediaItem(MediaItem.fromUri(Uri.fromFile(file)));
player.prepare();
}catch (Throwable throwable){
LogUtils.w(throwable);
player.release();
player = null;
loadLocal(uri,false,false);
}

}

public void pausePlayer(){
if(player !=null){
if(player.isPlaying() || player.isLoading()){
player.stop();
}
player.release();
player = null;
}
player.setMediaItem(MediaItem.fromUri(Uri.fromFile(file)));
player.prepare();
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
LogUtils.w("onDetachedFromWindow-"+this);
try {
if(player !=null){
// player.release();
if(player.isPlaying()){
player.stop();
}
player.release();
}
}catch (Throwable throwable){
LogUtils.w(throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,25 @@ public void onPageSelected(int position) {
showInfo(position, uris);
LogUtils.d(position,"onPageSelected");
MyLargeImageView imageView = (MyLargeImageView) viewMap.get(position);
imageView.loadUri(uris.get(position),true);
if(imageView !=null){
imageView.loadUri(uris.get(position),true);
}else {
LogUtils.w("imageView ==null",position);
}
//前后两个播放器停止
int pre = position-1;
MyLargeImageView imageViewPre = (MyLargeImageView) viewMap.get(pre);
if(imageViewPre !=null){
imageViewPre.pausePlayer();
}

int next = position+1;
MyLargeImageView imageViewNext= (MyLargeImageView) viewMap.get(next);
if(imageViewNext !=null){
imageViewNext.pausePlayer();
}


}

@Override
Expand All @@ -125,6 +143,8 @@ public void onPageScrollStateChanged(int state) {
viewPager.setAdapter(pagerAdapter);
pagerAdapter.notifyDataSetChanged();

//一般来说是先instantiateItem后onPageSelected,
// 但setCurrentItem这个方法会先触发onPageSelected,后触发instantiateItem
viewPager.setCurrentItem(pair.first);

showInfo(pair.first,uris);
Expand Down

0 comments on commit 7ed6f13

Please sign in to comment.