我正在尝试在作为列表视图项目的 viewpager 中显示视频或图像。当图像正确显示时,视频只是播放声音,没有视频。我试过使用 VideoView 和 SurfaceView 都没有运气。我宁愿不为此使用片段。
在列表视图中,出于测试原因,我正在加载一堆图像和一个视频。图像加载正常,但视频只播放声音。
我认为这与没有正确设置其表面纹理有关,但我无法弄清楚。
任何帮助,将不胜感激!
这是我的代码:
public class MediaSliderPagerAdapter2 extends PagerAdapter {
private int forListItem = -1;
private LayoutInflater inflater;
private MediaPlayer mMediaPlayer;
private SurfaceTexture mVideoSurfaceTexture;
public MediaSliderPagerAdapter2(int forListItem, LayoutInflater inflater) {
super();
this.forListItem = forListItem;
this.inflater = inflater;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
boolean isVideo = false;
String url = "";
try {
isVideo = mMediaUrls.get(forListItem).get(position).isVideo;
url = mMediaUrls.get(forListItem).get(position).url;
}
catch (Exception e) {
Log.e("getUrlType", e.getMessage());
}
if(!url.equals("")){
if(isVideo) {
final View layoutAll = inflater.inflate(R.layout.fragment_pager_video, container, false);
layoutAll.setId(R.id.feedlist_viewpager_layout_id_base + (position+1)*(1000*forListItem+1));
container.addView(layoutAll);
final TextureView layout = (TextureView) layoutAll.findViewById(R.id.videoView_layout);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
layout.setBackgroundResource(android.R.color.transparent);
layoutAll.setBackgroundResource(android.R.color.transparent);
layout.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Surface s = new Surface(surface);
mVideoSurfaceTexture = surface;
Log.i("height-width:", height+"-"+width);
try {
mMediaPlayer.setSurface(s);
mMediaPlayer.setDataSource("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
mMediaPlayer.prepareAsync();
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
MediaController mediaController = new MediaController(mContext);
mediaController.setAnchorView(layout);
mediaController.setMediaPlayer(new MediaController.MediaPlayerControl() {
@Override
public void start() {
mMediaPlayer.start();
}
@Override
public void pause() {
mMediaPlayer.pause();
}
@Override
public int getDuration() {
return mMediaPlayer.getDuration();
}
@Override
public int getCurrentPosition() {
return mMediaPlayer.getCurrentPosition();
}
@Override
public void seekTo(int pos) {
mMediaPlayer.seekTo(pos);
}
@Override
public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}
@Override
public int getBufferPercentage() {
return 0;
}
@Override
public boolean canPause() {
return true;
}
@Override
public boolean canSeekBackward() {
return true;
}
@Override
public boolean canSeekForward() {
return true;
}
@Override
public int getAudioSessionId() {
return mMediaPlayer.getAudioSessionId();
}
});
mediaController.show(99999);
Log.i("VideoView", "Duration = " + mMediaPlayer.getDuration() + " height-width:" + layout.getHeight() + "-" + layout.getWidth());
mMediaPlayer.start();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
Log.e("SurfaceSizeChanged", width+"/"+height);
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
boolean isNull = surface==null;
Log.e("onSurfaceTextureUpdated", "isNull: "+ isNull);
}
});
return layoutAll.getId();
}
else {
final ImageView layout = (ImageView) inflater.inflate(R.layout.fragment_pager_image, null);
layout.setId(R.id.feedlist_viewpager_layout_id_base + (position+1)*(1000*forListItem+1));
layout.setImageBitmap(null);
layout.clearAnimation();
Ion.with(mContext)
.load(url)
.withBitmap()
.smartSize(true)
.disableFadeIn()
.animateIn(R.anim.fade_in_)
.error(R.drawable.ic_launcher)
.placeholder(R.drawable.loading)
.animateLoad(R.anim.spin_animation)
.intoImageView(layout);
container.addView(layout);
return layout.getId();
}
}
return null;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = container.findViewById((Integer) object);
if(view instanceof ImageView) {
((ImageView) view).setImageBitmap(null);
view.clearAnimation();
}
else if(view instanceof FrameLayout) {
}
container.removeView(view);
}
@Override
public int getCount() {
int ret = 0;
if(mMediaUrls.get(forListItem)!= null)
ret = mMediaUrls.get(forListItem).size();
return ret;
}
@Override
public boolean isViewFromObject(View view, Object obj) {
boolean isEqual = view.getId() == (Integer) obj;
return isEqual;
}
}
此外,xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000">
<com.camboindustries.picabet.adapters.SquareTextureView
android:id="@+id/videoView_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000">
</com.camboindustries.picabet.adapters.SquareTextureView>
</FrameLayout>